如何使用VBA将名称分配给空的excel列标题?
我有一个Sub
在Excel中执行整个列的vlookup
。
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将名称(string)分配给空的excel列标题?
请检查这个:
选项显式
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