简体   繁体   English

为什么 VLookUp 不断返回错误 2042?

[英]Why does VLookUp keep returning Error 2042?

Probably that´s not the smartest way to convert "Jan" to "1" from a date like Jan 01, 2021", but why does VLookUp keep returning Error 2042?可能这不是从 2021 年 1 月 1 日这样的日期将“Jan”转换为“1”的最聪明方法,但为什么 VLookUp 不断返回错误 2042?

Option Explicit

Dim MonthMatrix(0 To 1, 0 To 11 As Variant

MonthMatrix(0, 0) = "Jan"
MonthMatrix(1, 0) = 1
MonthMatrix(0, 1) = "Feb"
MonthMatrix(1, 1) = 2
MonthMatrix(0, 2) = "Mar"
MonthMatrix(1, 2) = 3
MonthMatrix(0, 3) = "Apr"
MonthMatrix(1, 3) = 4
MonthMatrix(0, 4) = "May"
MonthMatrix(1, 4) = 5
MonthMatrix(0, 5) = "Jun"
MonthMatrix(1, 5) = 6
MonthMatrix(0, 6) = "Jul"
MonthMatrix(1, 6) = 7
MonthMatrix(0, 7) = "Aug"
MonthMatrix(1, 7) = 8
MonthMatrix(0, 8) = "Sep"
MonthMatrix(1, 8) = 9
MonthMatrix(0, 9) = "Oct"
MonthMatrix(1, 9) = 10
MonthMatrix(0, 10) = "Nov"
MonthMatrix(1, 10) = 11
MonthMatrix(0, 11) = "Dec"
MonthMatrix(1, 11) = 12

month = "Sep"
month_number = Application.VLookup(month, MonthMatrix, 1, False)

There are so many things wrong with this code that it's hard to list them all.这段代码有很多问题,很难一一列举。

  • Option Explicit must be outside the procedure选项显式必须在程序之外
  • there is no procedure defined没有定义程序
  • the opening bracket for the first Dim doesn't have a matching closing bracket第一个 Dim 的左括号没有匹配的右括号
  • the matrix sets up 2 rows and 12 columns矩阵设置 2 行 12 列
  • with Option Explicit, month and month_number need to be defined使用Option Explicit,需要定义monthmonth_number
  • you're returning the value that you are looking up你正在返回你正在查找的值

This just doesn't make sense.这只是没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM