简体   繁体   English

如何在Excel VBA中根据给定的日期复制单元格值

[英]How to copy cell value based on the given Date in Excel VBA

I attached my table. 我附上桌子了。 I want to get date(In inputbox) from Column A and D and copy the adjacent cell value (Column B and E). 我想从A列和D列获取日期(在输入框中)并复制相邻的单元格值(B列和E列)。 Then paste in different cell. 然后粘贴到其他单元格中。

For eg. 例如。 if i enter date 15.10.2016 and 25.10.2017. 如果我输入日期15.10.2016和25.10.2017。 Then output must be as shown in figure. 然后输出必须如图所示。 If the date is not present in the column then Msgbox as invalid date. 如果该列中没有日期,则Msgbox为无效日期。 Help me 帮我

在此处输入图片说明

This quick method would work for you. 这种快速方法将为您工作。

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

However please note that I have used the same Range (H3, I3) for input as well as output. 但是请注意,我在输入和输出中都使用了相同的范围(H3,I3)。 I thought you want it that way. 我以为你想要那样。 You may change it according to your need. 您可以根据需要进行更改。

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

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