从透视表中VBA到VLOOKUP的值,以返回输出表中多列中的值

时间:2018-02-26 14:49:58

标签: excel vba excel-vba

我有两张纸,一张是张Data Sheet,带有一个包含不同公司数据的数据透视表。我有另一张表Output Sheet,数据表中的列标题很少,我想使用公司ID vlookup不同的列标题。我找到了下面的代码,它适用于映射公司名称,但有以下问题

  1. 即使companyID仅在9555行可用,并且显示#N/A

  2. 之后,公式也会运行到100000行
  3. 如何使此公式查找其他列标题,如Segment,Sector Etc。

  4. 列标题需要映射:

    enter image description here

    Sub MakeFormulas()
        Dim SourceLastRow As Long
        Dim OutputLastRow As Long
        Dim sourceBook As Workbook
        Dim sourceSheet As Worksheet
        Dim outputSheet As Worksheet
        Application.ScreenUpdating = True
    
        'Where is the source workbook?
        Set sourceBook = Workbooks.Open("C:\Users\AAA\Desktop\NewFolder\Automation\07-Macro.xlsb")
    
        'what are the names of our worksheets?
        Set sourceSheet = sourceBook.Worksheets("TERFYTDPR")
        Set outputSheet = ThisWorkbook.Worksheets("All TMS-Data")
    
        'Determine last row of source
        With sourceSheet
            SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
        With outputSheet
            'Determine last row in col B
            OutputLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
            'Apply our formula
            .Range("B2:B10000" & OutputLastRow).Formula = _
                "=VLOOKUP(A2,'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)"
            OutputLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
        End With
    
        'Close the source workbook, don't save any changes
        sourceBook.Close False
        Application.ScreenUpdating = True
    End Sub
    

2 个答案:

答案 0 :(得分:0)

我们不知道您正在进行Vlookup的工作表上的数据布局,但我想我会解释Vlookup:

=VLOOKUP(A2,'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)"

上面代码行中的最后一个数字2是指如果A2的内容匹配您想要返回的列号,您可以改变它以获得另一个列号,例如第三列的3,但是还应该从$ A $ 2:$ B $更改范围以包括其他列。

因此,例如,如果您的Segment列位于C列中,您将改变您的vlookup:

=VLOOKUP(A2,'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$A$2:$C$" & SourceLastRow & ",3,0)"

我还会按如下方式重新编写代码:

Sub MakeFormulas()
Dim SourceLastRow As Long, OutputLastRow As Long, i As Long
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet, outputSheet As Worksheet

Application.ScreenUpdating = True
'Where is the source workbook?
Set sourceBook = Workbooks.Open("C:\Users\AAA\Desktop\NewFolder\Automation\07-Macro.xlsb")

'what are the names of our worksheets?
Set sourceSheet = sourceBook.Worksheets("TERFYTDPR")
Set outputSheet = ThisWorkbook.Worksheets("All TMS-Data")

'Determine last row of source
SourceLastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row

'Determine last row in col B
OutputLastRow = outputSheet.Cells(outputSheet.Rows.Count, "A").End(xlUp).Row
'Apply our formula
For i = 2 To OutputLastRow
    outputSheet.Range("B" & i).Formula = "=VLOOKUP(A" & i & ",'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$A$2:$C$" & SourceLastRow & ",2,0)"
    outputSheet.Range("C" & i).Formula = "=VLOOKUP(A" & i & ",'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$A$2:$C$" & SourceLastRow & ",3,0)"
'if segment is found in column C, and you also want the results in column C the line above will return the desired value
Next i
'Close the source workbook, don't save any changes
sourceBook.Close False
Application.ScreenUpdating = True
End Sub

<强>更新

在没有循环的情况下做同样的事情:

Sub MakeFormulas()
Dim SourceLastRow As Long, OutputLastRow As Long, i As Long
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet, outputSheet As Worksheet

Application.ScreenUpdating = True
'Where is the source workbook?
Set sourceBook = Workbooks.Open("C:\Users\AAA\Desktop\NewFolder\Automation\07-Macro.xlsb")

'what are the names of our worksheets?
Set sourceSheet = sourceBook.Worksheets("TERFYTDPR")
Set outputSheet = ThisWorkbook.Worksheets("All TMS-Data")

'Determine last row of source
SourceLastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row

'Determine last row in col B
OutputLastRow = outputSheet.Cells(outputSheet.Rows.Count, "A").End(xlUp).Row
'Apply our formula
outputSheet.Range("B2:B:" & OutputLastRow).Formula = "=VLOOKUP(A2,'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$A$2:$C$" & SourceLastRow & ",2,0)"
outputSheet.Range("C2:C" & OutputLastRow).Formula = "=VLOOKUP(A2,'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$A$2:$C$" & SourceLastRow & ",3,0)"
'if segment is found in column C, and you also want the results in column C the line above will return the desired value

'Close the source workbook, don't save any changes
sourceBook.Close False
Application.ScreenUpdating = True
End Sub

答案 1 :(得分:0)

据我了解,您希望将VLOOKUP公式填充到表中,但仅限于数据行?

假设列&#34; A&#34; - CompanyID和&#34; B&#34; - 公司名称始终填充,您可以制作excel的记录之间没有间隙为您拖动公式

以下是您的代码

Sub test()

Dim maxRow As Long
Dim DataWS As Worksheet
Dim lookupWS As Worksheet

'Set your data sheet
Set DataWS = Worksheets("Sheet1")
'Set your lookup sheet
Set lookupWS = Worksheets("Sheet2")

maxRow = lookupWS.Range("A1").End(xlDown).Row

'Populate formula
'Company Name
lookupWS.Range("B2").Formula = "=VLOOKUP($A2," & DataWS.Name & "!$A:$F,2,FALSE)" 'Make sure you change "2" to correct value in your case
'Segment
lookupWS.Range("C2").Formula = "=VLOOKUP($A2," & DataWS.Name & "!$A:$F,3,FALSE)" 'Make sure you change "3" to correct value in your case
'Sector
lookupWS.Range("D2").Formula = "=VLOOKUP($A2," & DataWS.Name & "!$A:$F,4,FALSE)" 'Make sure you change "4" to correct value in your case
'Channel
lookupWS.Range("E2").Formula = "=VLOOKUP($A2," & DataWS.Name & "!$A:$F,5,FALSE)" 'Make sure you change "5" to correct value in your case
'Account Type
lookupWS.Range("F2").Formula = "=VLOOKUP($A2," & DataWS.Name & "!$A:$F,6,FALSE)" 'Make sure you change "6" to correct value in your case

'Drag formula
Range("B2:F2").AutoFill Destination:=Range("B2:F" & maxRow), Type:=xlFillCopy


End Sub

确保正确更改range数组,它应该适合您。