Home » excel » vba – Excel: how to get column index number for the last column when the last column changes number

vba – Excel: how to get column index number for the last column when the last column changes number

Posted by: admin May 14, 2020 Leave a comment


I’m trying to automate my VLOOKUPs for a data sheet that gets updated every week. At the moment my VLOOKUP looks like this:


Every week two new columns are inserted to the table (before the last column). The new columns are populated with new data and I have to re-write my VLOOKUP.

The columns I’m looking up are always the third last, second last, and last columns.

Is there a way I can assign column index number with a function that looks up third last, second last, and last columns? I think that way I can add a macro that does the job for me.

How to&Answers:

For the last column, you may use below code and decrement it by 3

Dim lastcolumn As Long
dim ws as worksheet

set ws = activesheet

lastcolumn = ws.UsedRange.Columns.Count

for formula solution, you may use counta function in order to find the last column if your dataset has no blank headers.

if you find any problem, we can modify the code later


Turn your data into an Excel table and then use the .Range.columns.count. You can turn into a table using Ctrl+ T with a cell in the range selected. Ensure prompt includes all required columns. This is a robust method.

Option Explicit
Public Sub test()
    Dim totalColumns As Long
    With ThisWorkbook.Worksheets("All").ListObjects(1)     
        totalColumns = .Range.Columns.Count
        Debug.Print totalColumns                 '<==All columns
        If totalColumns < 4 Then Exit Sub
        Debug.Print totalColumns - 3             '<== etc
    End With
End Sub


With text column header labels in row 1 use,

=VLOOKUP(A3, '[Report.xlsm]All'!$I:$zz, match("zzz", '[Report.xlsm]All'!$1:$1)-8, false)

For dates or numbers in row 1, use 1e99 instead of “zzz”.


So I solved this by using the MIN and COLUMN(S) functions:

For the last column: MIN(COLUMN(range)+COLUMNS(range)-1)

For the second last column: MIN(COLUMN(range)+COLUMNS(range)-2)

This is the code in full:

Dim Rng As Range
Dim DataWorkbook As String
Dim DataWorksheet As String
Dim Data As String
On Error Resume Next

'Input box asking for a range of cells for subsequent VLOOKUPs'
Set Rng = Application.InputBox(Prompt:="Please select data range:", Type:=8)
If Rng Is Nothing Then
MsgBox "Macro Stopped"
Exit Sub
End If

Application.ScreenUpdating = False

DataWorkbook = Rng.Worksheet.Parent.Name
DataWorksheet = Rng.Worksheet.Name

Data = "'[" & DataWorkbook & "]" & DataWorksheet & "'!" & Rng.Address

    'Column Index for VLOOKUP is MIN(COLUMN(range)+COLUMNS(range)-4) where -4 is the number of columns before the desired column'

    'Third last column'
    ActiveCell.Formula = "=VLOOKUP(A3, " & "'[" & DataWorkbook & "]" & DataWorksheet & "'!" & Rng.Address & ", MIN(COLUMN(" & "'[" & DataWorkbook & "]" & DataWorksheet & "'!" & Rng.Address & ")+COLUMNS(" & "'[" & DataWorkbook & "]" & DataWorksheet & "'!" & Rng.Address & ")-4),0)"
    Selection.AutoFill Destination:=Range("D3:D116")