VBA Vlookup不同工作表中的日期范围

时间:2019-02-13 16:51:37

标签: date vlookup

我的编码经验为零,并且是VBA的新手,所以我什至不了解基本知识,但请多多指教。我有一本工作簿,里面有多张纸。我关心的是2张纸,称为DG和Asp。 DG有一个按钮,可从服务器获取原始数据并填充工作表(多个日期列,相邻单元格中有数据值)。 Asp具有一个按钮,该按钮也可以获取数据,但是平均每月30天,因此每个月的每天(Asp中的A列)。 DG表的情况与此相同,但是DG具有一个月中不同日期的数据,因为它不是30天的中断时间。这样就为您设置了一个图像,现在我要做的是创建一个按钮,其代码可以通过DG中的日期列并将其与asp date中的日期进行匹配,如果有匹配项,则复制并将DG中的相邻单元格值粘贴到asp。

到目前为止,这是我在互联网上进行的搜索所显示的内容,仅显示了我要在Asp中填写的单个列的vlookup,但它不起作用

Private Sub CommandButton2_Click()
Dim results As Double
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lrow As Long
Dim i As Long

Set ws1 = Worksheets("DG")
Set ws2 = Worksheets("Asp")
lrow = Worksheets("Asp").Range("A5", ws2.Range("A5").End(xlUp)).Rows.Count
For i = 5 To lrow
On Error Resume Next
        result = Application.WorksheetFunction.VLookup((ws2.Range("A5" & i)), (ws1.Range("A11:B200")), 2, True)
              ws2.Range("AG5").Value = result
    If Err.Number = 0 Then
       End If
       On Error GoTo 0

       
Next i
 
End Sub

DG [1]:https://i.stack.imgur.com/ZrwfZ.jpg
 ASP [2]:https://i.stack.imgur.com/tTsl0.jpg

1 个答案:

答案 0 :(得分:0)

今天是星期五,这是您要去研究和研究的东西。 抱歉,我没有使用Vlookup,我花了太多时间来追赶鬼魂。 也许其他人取得了更好的成功,我想我不喜欢Vlookup,如果它无法完全匹配,有时会选择相邻的行并将所有内容置于混乱之中。

这里是:

Option Explicit

Private Sub CommandButton2_Click()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lrow2 As Long
Dim lrow1 As Long
Dim firstDataRow As Double

Dim matchRange As Range
Dim matchRow As Long

Dim i As Long

'Set up your Worksheet variables
Set ws1 = ThisWorkbook.Worksheets("DG")
Set ws2 = ThisWorkbook.Worksheets("Asp")

'You used A5 several times, so I will assume dates are in Col A and start at row 5
'Set your row with first data, maybe you need two, if they are the same on both sheets you don't
firstDataRow = 5

'find the last row on each sheet, using column A, the date col
lrow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lrow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row

'In your mind you now have two data ranges, one for each sheet, rows 5-last dat rows

'Pick one sheet, the one with less dates would be more efficient, you mention DG has less
'So this might be built backwards from what you are thinking, since we are iterating each row in the col
'You want to use the shrter call, IF you know its shorter (your comments)

'Loop through each row trying to find a match on your other sheet
For i = firstDataRow To lrow1

    If ws1.Cells(i, "A") <> "" Then 'Test for empty

        'Here is the premise of Find/Match over Vlookup
        Set matchRange = ws2.Range("A" & firstDataRow & ":A" & lrow2).Find(ws1.Cells(i, "A"))

        On Error Resume Next
        matchRow = matchRange.Row 'Returns row number or nothing

        If (Not matchRange Is Nothing) Then
            'we have a row number matched on Asp, for our search item on DG
            'perform the "Copy", this can be done differently but here I am going to introduce you to a way
            'that can later be used with offsets and col #s, so that you may skip columns, data is not always adjacent
            ws2.Cells(matchRow, "E") = ws1.Cells(i, "B")
            ws2.Cells(matchRow, "F") = ws1.Cells(i, "C")
            ws2.Cells(matchRow, "G") = ws1.Cells(i, "D")
        Else 'DO NOTHING
        End If
    Else 'DO NOTHING
    End If

Next i

MsgBox "Search and Copy is complete.", vbInformation, "Completed"

End Sub

即使是这个简单的项目,也有很多更多的要讨论的内容,以使它更具防弹性能。但这是您所处位置的良好起点。

enter image description here

enter image description here

干杯!编码愉快! -WWC

相关问题