2017-09-26 61 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 
相關問題