2017-09-26 39 views
0

我有一个Sub它为Excel中的整个列执行vlookup如何使用VBA将名称分配给空的excel列标题?

Option Explicit 


Sub Vlookup() ' performs Vlookup of all entries in sheet1 with keys from sheet2 

Dim wbk1 As Workbook 

Set wbk1 = ActiveWorkbook ' wbk1 is master table 

    With wbk1.Sheets("sheet1") 'define sheet in master table 
     .Range("AA2:AA" & .Range("C" & .Rows.Count).End(xlUp).Row).Formula = _ 
     "=VLOOKUP(C2,sheet2!$A$2:$B$14,2,FALSE)" 'Actual vlookup 
    End With 

End Sub 

该vlookup本身运作良好。但是,结果列AA:AA缺少标题。

如何使用VBA将名称(字符串)分配给空的excel列标题?

+0

你怎么把一个值单元格?网络上有很多例子。请做一个搜索。 – jsotola

+0

你的意思是'.Range(“AA1”)。Value2 =“HeaderName”'? –

回答

1

请检查:

显式的选项

Sub Vlookup() ' performs Vlookup of all entries in sheet1 with keys from sheet2 

Dim wbk1 As Workbook 
Dim lastRow As Long 


Set wbk1 = ActiveWorkbook ' wbk1 is master table 

    With wbk1.Sheets("sheet1") 'define sheet in master table 

    lastRow = .Range("C" & .Rows.Count).End(xlUp).Row 


     .Range("AA2:AA" & lastRow).Formula = _ 
     "=VLOOKUP(C2,sheet2!$A$2:$B$14,2,FALSE)" 'Actual vlookup 
     .Range("AA1").Value = Sheet2.Range("b1").Value 


    End With 

End Sub 
相关问题