Insert new columns, with headers - Macro/VBA help

carefreeant88

New Member
Joined
Nov 27, 2024
Messages
15
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

I have two separate/concurrent issues that I am hoping for help with.

For the first issue, I basically have a workbook with 4 separate worksheets/tabs on it. This automatically updates everyday based on latest Sales Data, and so - as such - the exact layout/order of the columns and headers changes.

I am basically therefore after some help, with writing a Macro/VBA which achieves the following. I am really struggling with it (bit of a newbie with Macros), so any help would be welcome:

  1. Go to the tab called 'Sales Analysis'
  2. Search for the word 'Text' on the top row (column headers)
  3. When you find the column called 'Text', inserts 5 Columns to the left of it.
  4. Call the first column that has been added 'Sales Data 1'
    Call the second column that has been added 'Sales Data 2'
    Call the third column that has been added 'Sales Data 3'
    Call the fourth column hat has been added 'Sales Data 4'
    Call the fifth column that has been added 'Sales Data 5'

Hopefully it's do-able. As I say, I am new to Macros/VBAs and so any help at all would be most welcome.

Regards
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try the code below the, please note that the code will error if there isn't a header called Text.

VBA Code:
Sub carefreeant1()
    Dim iCol As Integer
    Application.ScreenUpdating = False

    With Sheets("Sales Analysis")

        iCol = .Rows(1).Find("Text", , xlValues, xlWhole, xlByColumns, xlNext).Column
        .Columns(iCol).Resize(, 5).Insert
        .Cells(1, iCol).Resize(, 5).Value = Array("Sales Data 1", "Sales Data 2", "Sales Data 3", "Sales Data 4", "Sales Data 5")

    End With
  
End Sub
 
Last edited:
Upvote 0
Solution
@MARK858 , don't you have to turn ScreenUdating back on? Or does it reset when the Sub ends?
 
Upvote 0
Try the code below the, please note that the code will error if there isn't a header called Text.

VBA Code:
Sub carefreeant1()
    Dim iCol As Integer
    Application.ScreenUpdating = False

    With Sheets("Sales Analysis")

        iCol = .Rows(1).Find("Text", , xlValues, xlWhole, xlByColumns, xlNext).Column
        .Columns(iCol).Resize(, 5).Insert
        .Cells(1, iCol).Resize(, 5).Value = Array("Sales Data 1", "Sales Data 2", "Sales Data 3", "Sales Data 4", "Sales Data 5")

    End With
 
End Sub

Thanks a million! This has been a HUGE help.

Thanks again
 
Upvote 0
@MARK858 , don't you have to turn ScreenUdating back on? Or does it reset when the Sub ends?
It resets when the sub ends (one of the few that does), there used to be a quirk with it not resetting within userforms and UDF's, and I have seen posts where this is still appears to be true on other forums in recent years.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,296
Messages
6,177,741
Members
452,797
Latest member
prophet4see

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