Consolidating two Worksheets

。_饼干妹妹 提交于 2019-12-11 08:48:45

问题


I want to consolidate two worksheets on the basis of a "Register No." in a third worksheet.

Workbook:
Tabelle1: Consolidated Worksheet //
Tabelle2: Input Data1 //
Tabelle3: Input Data2

Notes:

  • At first the "Register No." can only be found in Tabelle2 & Tabelle3 in column A.

  • Because Tabelle1 has also a different column sequence than Tabelle2 & Tabelle3 I am using vLookup to paste the data to the right columns in Tabelle1.

Idea:

1. Step Pasting Tabelle2 Data, including "Register No.", to the right columns in Tabelle1 via vLookup. Note: This means "Register No." to Tabelle 1 column A.

2. Step Pasting Tabelle3 Data to right rows and columns in Tabelle1 via vLookup. As Tabelle3 contains more "Register No." than Tabelle2, I want my code to check the "Register No." in Tabelle1 column A and copy the corresponding data from Tabelle3.

ERROR:

The 2. Step is not working.

Runtime-Error '1004'

For example:

For i = 2 To lastrow2

    Tabelle1.Cells(7 + i, 2) = Application.WorksheetFunction.VLookup(Tabelle1.Cells(i, 1), myrange2, 2, False)

Next i

Does anyone know what is wrong with my code? Thanks a lot :)

My Code:

Sub ConsolidateData()


Dim lastrow As Long
lastrow = Tabelle2.Range("A" & Rows.Count).End(xlUp).Row
Set myrange = Tabelle2.UsedRange


For i = 2 To lastrow
    Tabelle1.Cells(7 + i, 1) = Application.WorksheetFunction.VLookup(Tabelle2.Cells(i, 1), myrange, 1, False)
Next i

For i = 2 To lastrow
    Tabelle1.Cells(7 + i, 3) = Application.WorksheetFunction.VLookup(Tabelle2.Cells(i, 1), myrange, 2, False)
Next i



For i = 2 To lastrow
Tabelle1.Cells(7 + i, 6) = Application.WorksheetFunction.VLookup(Tabelle2.Cells(i, 1), myrange, 3, False)
Next i


Dim lastrow2 As Long
lastrow2 = Tabelle3.Range("A" & Rows.Count).End(xlUp).Row
Set myrange2 = Tabelle3.UsedRange

For i = 2 To lastrow2
    Tabelle1.Cells(7 + i, 2) = Application.WorksheetFunction.VLookup(Tabelle1.Cells(i, 1), myrange2, 2, False)
Next i

For i = 2 To lastrow2
    Tabelle1.Cells(7 + i, 4) = Application.WorksheetFunction.VLookup(Tabelle1.Cells(i, 1), myrange2, 3, False)
Next i

For i = 2 To lastrow2
    Tabelle1.Cells(7 + i, 5) = Application.WorksheetFunction.VLookup(Tabelle1.Cells(i, 1), myrange2, 4, False)
Next i

For i = 2 To lastrow2
    Tabelle1.Cells(7 + i, 7) = Application.WorksheetFunction.VLookup(Tabelle1.Cells(i, 1), myrange2, 5, False)
Next i

End Sub

回答1:


I think the problem is the way you are referencing your worksheets. You are using the Worksheet.CodeName vs the Worksheet.Name of the worksheet.

Look at my example below and you will see that the Worksheet.CodeName and Worksheet.Name do not match.

Worksheet.CodeName is the 1st part of the name and Worksheet.Name is what's shown in parentheses. Therefore the Worksheet.CodeName for the second worksheet is Sheet5, whereas the Worksheet.Name is Sheet6.

This is because I deleted a worksheet and excel, behind the scenes, renamed the Worksheet.CodeName reference.

To use what you see when looking at the tabs in the workbook you need to reference it by Worksheet.Name, not Worksheet.CodeName.

Sub testPickingWorksheets()

' This code fails
    a = Sheet6.Range("A1").Value
    MsgBox (a)

' This code works
    a = Worksheets("Sheet6").Range("A1").Value
    MsgBox (a)

End Sub

As you can see from the code above, you need to use the Worksheets() Ojbect with the Worksheet.Name in "quotes" instead of directly referencing the Worksheet.CodeName.



来源:https://stackoverflow.com/questions/49260060/consolidating-two-worksheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!