VBA Column Finder

Soiklug

New Member
Joined
May 12, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm new to this forum so I apologize if the question has already been answered. I am working on a spreadsheet that fills out shipping details to a customer. What I am looking to do is execute a VBA code that will locate the column and perform the vlookup. Typically, I would just create a macro of me filling it out and calling it good, but with the number of customers slightly changing the order of their columns it would be nice if I had a code that looked for the appropriate column and ran from there. Any ideas on how I should approach this or does someone have a code they have had success with?

Thank you
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi All,

I'm new to this forum so I apologize if the question has already been answered. I am working on a spreadsheet that fills out shipping details to a customer. What I am looking to do is execute a VBA code that will locate the column and perform the vlookup. Typically, I would just create a macro of me filling it out and calling it good, but with the number of customers slightly changing the order of their columns it would be nice if I had a code that looked for the appropriate column and ran from there. Any ideas on how I should approach this or does someone have a code they have had success with?

Thank you

This function will return the column number based upon the column header value.

VBA Code:
Public Function fncFindColumnNumber(strWorksheet As String, strColumnHeader As String) As Long
Dim rngFound As Range

    Set rngFound = Worksheets(strWorksheet).Range("1:1").Find(strColumnHeader, LookIn:=xlValues)
    
    If Not rngFound Is Nothing Then
        fncFindColumnNumber = rngFound.Column
    Else
        fncFindColumnNumber = 0
    End If
    
End Function

Call it like this where 'ShippingDetails' is the worksheet name and 'PhoneNumber' is the column header

VBA Code:
  intColumn = fncFindColumnNumber("ShippingDetails", "PhoneNumber")

    If intColumn <> 0 Then
        ' Put Vlookup code here.
    Else
        ' Put code to execute when column not found here.
    End If
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top