Need to add column header values to this code

Hailee994

New Member
Joined
Oct 23, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am working on a code that adds 2 columns to the right of the specified header column as below - the below code works but I need to have the value for the first new column reflect "STATE MW" and the header for the second reflect "MW * REG HRS". Can someone please help

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."
Else
MsgBox "STATE CD 1 column not found!"
End If
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try as below, please.

VBA Code:
stateCDColumn.Offset(0, 1).Resize(1, 2).EntireColumn.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'add the line below
stateCDColumn.Offset(, 1).Resize(, 2) = Array("STATE MW", "MW * REG HRS")
MsgBox "Two columns inserted to the right of STATE CD 1."
 
Upvote 1

Forum statistics

Threads
1,223,840
Messages
6,174,956
Members
452,593
Latest member
Jason5710

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