vba Insert Column and add values to empty cells

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
684
Office Version
  1. 2019
Platform
  1. Windows
Hello,
Need to insert a new column before Row(3:3) has a value (date).
Then insert "44" to1st cell in new column, and the occupied value (date) in 2nd cell.
There are several columns (A:AUI). Add the Cell color to Identify the change.
Thank you.

Here's how the outcome will look:
Before:
NBA.xlsm
ABCDEFGHIJKLMNOPQRS
1PHILALORLWASHOUNOCHICLENYKOKCCHADENDALPORMILLACNOSANCHI
2BOSGSDETINDATLBKNMIATORMEMMINSANUTAPHXSACPHILALCHAINDWAS
310/18/2210/19/2210/20/2210/21/22
Raw


After:
NBA.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
144PHILAL44ORLWASHOUNOCHICLENYKOKCCHADENDALPOR44MILLAC44NOSANCHI
210/18/22BOSGS10/19/22DETINDATLBKNMIATORMEMMINSANUTAPHXSAC10/20/22PHILAL10/21/22CHAINDWAS
Raw
 

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 this:
VBA Code:
Sub move_date()
Dim i as Long
Dim LastCol as Long
'finds the last column
:astCol = Cells(1, Columns.Count).End(xlToLeft).Column
'loops through the columns
For i = 1 To LastCol
    'checks for something in the 3rd row
    If Cells(3, i) > "" Then
        'insert the column before
        Cells(3, i).EntireColumn.Insert
        'add the 44 in row 1
        Cells(1, i) = "44"
        'copy the date into row 2
        Cells(2, i) = Format(Cells(3, i + 1), "mm/dd/yyyy")
        'change the color
        Range(Cells(1, i), Cells(2, i)).Interior.ColorIndex = 36
        'clear the original date
        Cells(3, i + 1).ClearContents
    End If
Next i
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub move_date()
Dim i as Long
Dim LastCol as Long
'finds the last column
:astCol = Cells(1, Columns.Count).End(xlToLeft).Column
'loops through the columns
For i = 1 To LastCol
    'checks for something in the 3rd row
    If Cells(3, i) > "" Then
        'insert the column before
        Cells(3, i).EntireColumn.Insert
        'add the 44 in row 1
        Cells(1, i) = "44"
        'copy the date into row 2
        Cells(2, i) = Format(Cells(3, i + 1), "mm/dd/yyyy")
        'change the color
        Range(Cells(1, i), Cells(2, i)).Interior.ColorIndex = 36
        'clear the original date
        Cells(3, i + 1).ClearContents
    End If
Next i
End Sub
it does nothing. it goes straight to End Sub
 
Upvote 0
Try this:
VBA Code:
Sub move_date()
Dim i as Long
Dim LastCol as Long
'finds the last column
:astCol = Cells(1, Columns.Count).End(xlToLeft).Column
'loops through the columns
For i = 1 To LastCol
    'checks for something in the 3rd row
    If Cells(3, i) > "" Then
        'insert the column before
        Cells(3, i).EntireColumn.Insert
        'add the 44 in row 1
        Cells(1, i) = "44"
        'copy the date into row 2
        Cells(2, i) = Format(Cells(3, i + 1), "mm/dd/yyyy")
        'change the color
        Range(Cells(1, i), Cells(2, i)).Interior.ColorIndex = 36
        'clear the original date
        Cells(3, i + 1).ClearContents
    End If
Next i
End Sub
now I see there might be a typo.
: should be L
 
Upvote 0
Try this:
VBA Code:
Sub move_date()
Dim i as Long
Dim LastCol as Long
'finds the last column
:astCol = Cells(1, Columns.Count).End(xlToLeft).Column
'loops through the columns
For i = 1 To LastCol
    'checks for something in the 3rd row
    If Cells(3, i) > "" Then
        'insert the column before
        Cells(3, i).EntireColumn.Insert
        'add the 44 in row 1
        Cells(1, i) = "44"
        'copy the date into row 2
        Cells(2, i) = Format(Cells(3, i + 1), "mm/dd/yyyy")
        'change the color
        Range(Cells(1, i), Cells(2, i)).Interior.ColorIndex = 36
        'clear the original date
        Cells(3, i + 1).ClearContents
    End If
Next i
End Sub
I corrected the typo.
works perfectly.
Thank you.
 
Upvote 0
Try this:
VBA Code:
Sub move_date()
Dim i as Long
Dim LastCol as Long
'finds the last column
:astCol = Cells(1, Columns.Count).End(xlToLeft).Column
'loops through the columns
For i = 1 To LastCol
    'checks for something in the 3rd row
    If Cells(3, i) > "" Then
        'insert the column before
        Cells(3, i).EntireColumn.Insert
        'add the 44 in row 1
        Cells(1, i) = "44"
        'copy the date into row 2
        Cells(2, i) = Format(Cells(3, i + 1), "mm/dd/yyyy")
        'change the color
        Range(Cells(1, i), Cells(2, i)).Interior.ColorIndex = 36
        'clear the original date
        Cells(3, i + 1).ClearContents
    End If
Next i
End Sub

[/QUOTE]

Try this:
VBA Code:
Sub move_date()
Dim i as Long
Dim LastCol as Long
'finds the last column
:astCol = Cells(1, Columns.Count).End(xlToLeft).Column
'loops through the columns
For i = 1 To LastCol
    'checks for something in the 3rd row
    If Cells(3, i) > "" Then
        'insert the column before
        Cells(3, i).EntireColumn.Insert
        'add the 44 in row 1
        Cells(1, i) = "44"
        'copy the date into row 2
        Cells(2, i) = Format(Cells(3, i + 1), "mm/dd/yyyy")
        'change the color
        Range(Cells(1, i), Cells(2, i)).Interior.ColorIndex = 36
        'clear the original date
        Cells(3, i + 1).ClearContents
    End If
Next i
End Sub
Your code works, but it stops inserting at column AUI, which is the last column at the beginning of code.
As code inserts columns, its adding columns. Therefore, maybe columns have to be recalculated after each insert?
Or when to code hits column(AUI) or (1231)column number, then recalculate?
 
Upvote 0
Good point. You might be able to get away with adding a line just before the end if statement:
VBA Code:
LastCol =LastCol+1
I'm not near her machine with Excel on it right now, so it's not tested, but try that out.
 
Upvote 0
Good point. You might be able to get away with adding a line just before the end if statement:
VBA Code:
LastCol =LastCol+1
I'm not near her machine with Excel on it right now, so it's not tested, but try that out.
It wasn't adding enough to LastCol, so I changed the '1 to LastCol' to '1 to 1405'.
Giving it more than enough Col count to calculate.
And it worked.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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