enter image description here
将弹出一个消息框,供用户输入日期并将Money值复制粘贴到新单元格。每当我输入日期。总是找不到该值。下面是代码:
`your text`Macro10 Macro
'
Sub get_money()
a_col_last_row = ThisWorkbook.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
d_col_last_row = ThisWorkbook.Worksheets(1).Cells(Rows.Count, 4).End(xlUp).Row
data_a = InputBox("Enter 1st date")
data_d = InputBox("Enter 2st date")
lookup_val_a = ThisWorkbook.Worksheets(1).Cells(3, 8)
Dim refRng_a As Range
Dim refRng_d As Range
Set refRng_a = Range("A2:B" & a_col_last_row)
'data_a = Cells(3, 8).Value
Set refRng_d = Range("D2:E" & d_col_last_row)
'data_d = Cells(3, 9).Value
On Error GoTo errhandler1:
Cells(3, 8) = WorksheetFunction.VLookup(data_a, refRng_a, 2, 0)
controlback:
On Error GoTo errhandler2:
Cells(3, 9) = WorksheetFunction.VLookup(data_d, refRng_d, 2, 0)
End
errhandler1:
MsgBox "1st Value not found"
Err.Number = 0
Resume controlback
errhandler2:
MsgBox " 2nd Value not found"
Err.Number = 0
End Sub
用户需要输入日期,日期旁边的值将复制并粘贴到新单元格。
1条答案
按热度按时间tf7tbtn21#
问题是InputBox返回String数据类型。您需要a)将日期变量声明为Date,例如,
B)将数据转换为日期,例如
data_a = CDate(InputBox("Enter 1st date"))