sudonym
sudonym

Reputation: 4018

How to assign a name to an empty excel column header using VBA?

I have a Sub which performs a vlookup for a whole column in 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

The vlookup itself works well. However, the resulting column AA:AA lacks a header.

How to assign a name (string) to an empty excel column header using VBA?

Upvotes: 0

Views: 734

Answers (1)

Nikolaos Polygenis
Nikolaos Polygenis

Reputation: 691

Please Check this:

Option Explicit

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

Upvotes: 1

Related Questions