VBA macro to add coloured and named columns between every column in sheet

Joined
Jan 30, 2022
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hi all, crossposted on Stack overflow:

I'm doing some data cleaning and am trying to reduce the load before I fully automated the process through macros. Currently using the following to add alternating rows:

Sub AddColumns
For colx= [X] to [Y] Step 2
Columns(colx).Insert Shift:=xlToRight
Next
EndSub

What I want to add in is changing the colour of these new columns as they are added, and also automatically renaming them based on the column header before - e.g. the column added after "Type" would be "Check Type", the column added after "Name" would be "Check Name".

I'm struggling to even figure out how to start - I'm guessing that it would be on a per-column basis to colour the column named Column 1, and then to change header name to "Check"&value of c-1?

All suggestions welcome!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi
What about as "how to start"
VBA Code:
Sub AddColumns()
    For colx = 8 To 2 Step -1
        Columns(colx).Insert Shift:=xlToRight
        Cells(1, colx) = "CHECK " & Cells(1, colx - 1)
        Cells(1, colx).Interior.Color = vbRed
    Next
End Sub
 
Upvote 0
Solution
Hi
What about as "how to start"
VBA Code:
Sub AddColumns()
    For colx = 8 To 2 Step -1
        Columns(colx).Insert Shift:=xlToRight
        Cells(1, colx) = "CHECK " & Cells(1, colx - 1)
        Cells(1, colx).Interior.Color = vbRed
    Next
End Sub
You are fanTASTIC. Thanks so much, I'm still learning VBA and am still working away from using Select.
 
Upvote 0
Hi
What about as "how to start"
VBA Code:
Sub AddColumns()
    For colx = 8 To 2 Step -1
        Columns(colx).Insert Shift:=xlToRight
        Cells(1, colx) = "CHECK " & Cells(1, colx - 1)
        Cells(1, colx).Interior.Color = vbRed
    Next
End Sub
Hi Mohadin, just wondering if you could help me tweak this. i have extendend it to add two columns instead of one using Columns(colx).Resize(,2), which works a treat for the adding and colouring but I'm struggling to add the name for the second column = "Revised " & Cells(1, colx-2) just gets me "Revised " added X number of loops.
 
Upvote 0
Hi
Do you mean some thing like:
VBA Code:
Sub AddColumns()
    For colx = 8 To 2 Step -1
        Columns(colx).Resize(, 2).Insert Shift:=xlToRight
        Cells(1, colx) = "CHECK " & Cells(1, colx - 1)
        Cells(1, colx + 1) = "REvised " & Cells(1, colx - 1)
        Cells(1, colx).Resize(, 2).Interior.Color = vbRed
    Next
End Sub

Another option same task
VBA Code:
Sub AddColumns()
Dim colx As Long
    For colx = 8 To 2 Step -1
        Columns(colx).Resize(, 2).Insert Shift:=xlToRight
        With Cells(1, colx)
        .Value = "CHECK " & .Offset(, -1)
        .Offset(, 1) = "REvised " & .Offset(, -1)
        .Resize(, 2).Interior.Color = vbRed
        End With
    Next
End Sub
 
Last edited:
Upvote 0
Hi Mohadin, just wondering if you could help me tweak this. i have extendend it to add two columns instead of one using Columns(colx).Resize(,2), which works a treat for the adding and colouring but I'm struggling to add the name for the second column = "Revised " & Cells(1, colx-2) just gets me "Revised " added X number of loops.
I
Hi
Do you mean some thing like:
VBA Code:
Sub AddColumns()
    For colx = 8 To 2 Step -1
        Columns(colx).Resize(, 2).Insert Shift:=xlToRight
        Cells(1, colx) = "CHECK " & Cells(1, colx - 1)
        Cells(1, colx + 1) = "REvised " & Cells(1, colx - 1)
        Cells(1, colx).Resize(, 2).Interior.Color = vbRed
    Next
End Sub

Another option same task
VBA Code:
Sub AddColumns()
Dim colx As Long
    For colx = 8 To 2 Step -1
        Columns(colx).Resize(, 2).Insert Shift:=xlToRight
        With Cells(1, colx)
        .Value = "CHECK " & .Offset(, -1)
        .Offset(, 1) = "REvised " & .Offset(, -1)
        .Resize(, 2).Interior.Color = vbRed
        End With
    Next
End Sub
I figured out the first option by myself! The second is great too, very informative, I will incorporate offset into my macros!

Thanks again ?
 
Upvote 0

Forum statistics

Threads
1,202,990
Messages
6,052,954
Members
444,621
Latest member
MIKOLAJ_R

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