Using XLOOKUP in VBA to lookup value underneath specific header name (columns can change depending on worksheet)

Hailee994

New Member
Joined
Oct 23, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to build an XLOOKUP into my code that will lookup the first value underneath the header "STATE CD 1", as my columns can vary depending on the workbook. I am not sure if I should use the lookup in this code, or if I should make it separately so I am open to feedback on this point.

Below is my current code and attached sample screenshot, I need to xlookup the state codes to my min wage table using the STATE MW column, then I also need to have the MW * HRS column multiply the "Reg Hours" * "STATE MW" and pull the value into MW * HRS column. The reg hours needs to be coded like I have my State CD 1 column due to the column numbers potentially changing (I do this for multiple companies so placement can vary).

Here is the current xlookup formula =XLOOKUP(BL2,'https://thyssenkrupp.sharepoint.com/teams/CentralizedPayroll/Freigegebene Dokumente/General/Audit Documents General/Gross Pay Compare/2024/[00. Min Wage Lookup Table.xlsx]Sheet1'!$A:$A,'https://thyssenkrupp.sharepoint.com/teams/CentralizedPayroll/Freigegebene Dokumente/General/Audit Documents General/Gross Pay Compare/2024/[00. Min Wage Lookup Table.xlsx]Sheet1'!$B:$B,FALSE)


Sub InsertTwoColumnsRightOfStateCD1()
Dim ws As Worksheet
Dim stateCDColumn As Range

' Set the worksheet
Set ws = ThisWorkbook.Sheets("CSV Earnings Statement Register")

' Find the "STATE CD 1" column
Set stateCDColumn = ws.Rows(1).Find(What:="STATE CD 1", LookIn:=xlValues, LookAt:=xlWhole)

' If the column is found, insert two columns to the right
If Not stateCDColumn Is Nothing Then
stateCDColumn.Offset(0, 1).Resize(1, 2).EntireColumn.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
MsgBox "Two columns inserted to the right of STATE CD 1."
' Set the new column ranges
Set newColumn1 = stateCDColumn.Offset(0, 1)
Set newColumn2 = stateCDColumn.Offset(0, 2)

' Name the new columns
newColumn1.Value = "STATE MW"
newColumn2.Value = "MW * HRS"
Else
MsgBox "STATE CD 1 column not found!"
End If
End Sub
 

Attachments

  • 2024-10-24 08_15_23-samplefile.xlsx - Excel.png
    2024-10-24 08_15_23-samplefile.xlsx - Excel.png
    43.9 KB · Views: 3

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,223,099
Messages
6,170,114
Members
452,302
Latest member
TaMere

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