VBA匹配并更新现有的并将新字段添加到下一行的“下一个空白单元格”中

时间:2018-06-26 08:22:58

标签: excel vba excel-vba match vlookup

我是这里的新手,我正在尝试执行VBA Vlookup函数。

我的目标是使用A列将Sheet 1中的Table1从Sheet2 VLookup到Table2中,如果存在A,则更新B和C列。

如果不存在A,则要在表1中添加下一个空白行,其中还包括列B和C。

请参考下图-具有更新结果的Sheet1的期望。

enter image description here

谢谢。

当前只能编写代码以更新现有字段,但不确定如何将不匹配的字段添加到Sheet1的下一个空白行中。

Sub getOpenExcel()

    '   Your daily report has a date in it's name
    '   to select an open workbook we must first know it's name
    '   AND - it must be already open
    '   Your examples are 2017-03-11-18875, 2017-03-12-18875, 2017-03-13-18875

    '   If the name is the current date then this would work to get the filename

    Dim fileName As String, monthNum As String, dayNum As String, wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet, rng1 As Range, rng2 As Range

    '   this adds a ZERO to the front of month numbers less than 10
    If Month(Date) < 10 Then
        monthNum = "0" & CStr(Month(Date))
    Else
        monthNum = CStr(Month(Date))
    End If


    '   You may or may not need this section
    '   it adds a ZERO to the front of day numbers less than 10
    If Day(Date) < 10 Then
        dayNum = "0" & CStr(Day(Date))
    Else
        dayNum = CStr(Day(Date))
    End If
    '   many cases the daily report will come from the previous day
    '   If your file has yesterday's date, then comment out the above code and
    'uncomment the following code
    '
    'If Day(DateAdd("d", -1, Date)) < 10 Then
    '    dayNum = "0" & Day(DateAdd("d", -1, Date))
    'Else
    '    dayNum = Day(DateAdd("d", -1, Date))
    'End If


    fileName = "GREENBILL_RECON_DETAILED_REPORT_" & CStr(Year(Date)) & monthNum & dayNum
    '   if today's date is 3/14/17 then "fileNem" = "2017-03-12-18875"

    '   If your daily report is an excel book, then we need to add the proper extension.
    '   It could be one of many, "xls", ".xlsx" , ".xlsm", etc....
    '   If your daily report is open - look at the top.  It should have the file name and extension.'
    '   Replace the below extension with the correct one.
    fileName = fileName & ".csv"
    '   Again, if today's date is 3/14/17 then "fileNem" =  "2017-03-12-18875.xlsx"


    '   This is where we set both workbooks to variables
    '
    Set wb1 = ThisWorkbook
    Set ws1 = wb1.Worksheets("Sheet1")

    On Error GoTo notOpen
    Set wb2 = Workbooks(fileName)                ' This is your daily report
    On Error GoTo 0
    Set ws2 = wb2.Worksheets("GREENBILL_RECON_DETAILED_REPORT")
    ws1.Activate
    '*************************************************************************************
    ' If successful this is the area where you put your code to copy and paste automatically '
    ' If you need this pasted to the first empty row at bottom of page then 'put code here to find the first empty row and use that varaible
    ' with range("a" & firstUnusedRow) intstead of A1 ...

    wb2.Activate
    Range("A1:Z500").Copy _
          Destination:=wb1.Worksheets("Sheet1").Range("A1") 'change A1 to A &
    firstUnusedRow
    '*************************************************************************************
    ' This is the clean up and exit code

    Set wb1 = Nothing
    Set wb2 = Nothing
    Exit Sub
notOpen:
    On Error GoTo 0
    Set wb1 = Nothing
    MsgBox "The file " & fileName & " is not open"
    Exit Sub

End Sub

Sub Rectangle3_Click()

    On Error Resume Next

    Dim Dept_Row As Long                         ' To Change to Billing_Acc
    Dim Dept_Clm As Long                         ' To Change to Org_Seqno

    Table1 = Sheet1.Range("A1:A10")              ' Input file name
    Table2 = Sheet2.Range("A1:B10")              ' Range of table

    Dept_Row = Sheet1.Range("B1").Row
    Dept_Clm = Sheet1.Range("B1").Column

    For Each cl In Table1
        Sheet1.Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl, Table2, 2, False)
        Dept_Row = Dept_Row + 1
    Next cl
    MsgBox "Done"

End Sub

1 个答案:

答案 0 :(得分:1)

下面的代码通过匹配两张纸的A列来遍历所有行。如果找不到,则会在sheet1中添加新行。

Dim lngRow1, lngRow2 As Long
lngRow1 = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lngRow2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

Dim isFound As Boolean
Dim lastRow As Long
Dim i, j As Long

lastRow = lngRow1
For i = 1 To lngRow2
    isFound = False
    For j = 1 To lngRow1
        If Sheets("Sheet1").Range("A" & i) = Sheets("Sheet2").Range("A" & j) Then
            Sheets("Sheet1").Range("B" & i) = Sheets("Sheet2").Range("B" & j)
            Sheets("Sheet1").Range("C" & i) = Sheets("Sheet2").Range("C" & j)
            isFound = True
        End If
    Next j
    If Not isFound Then
        lastRow = lastRow + 1
        Sheets("Sheet1").Range("A" & lastRow) = Sheets("Sheet2").Range("A" & i)
        Sheets("Sheet1").Range("B" & lastRow) = Sheets("Sheet2").Range("B" & i)
        Sheets("Sheet1").Range("C" & lastRow) = Sheets("Sheet2").Range("C" & i)
    End If
Next i

考虑以上示例图像编写的代码。如果列数与示例不同,请相应地修改代码。