VBA: Insert Missing Columns in a Series

byehp

New Member
Joined
Mar 31, 2011
Messages
10
Hi VBA Gurus,

First of all, great website!
It really helps newbie Excel-VBA users like me.

I am currently using MS Excel 2007.
Is there a way in VBA to automatically insert missing columns based on a defined series or range of values?

For example, we have a report that displays columns of: Year 2004 to Year 2012 per column (9 columns in total).
Sometimes, it displays missing a year or years (thus; our report has less columns). For example, it only shows 2008 to 2012 (missing 2004 to 2007). At times, it misses some years in between. What we do is manually add those missing years by manually inserting columns then inputting the missing year as their column headers.

Is it posisble for VBA to check if the columns contain all of the defined Year range (for example, you've initially defined: Year 2004 to Year 2012)? Afterwards, all missing columns are automoatically inserted (in chronological order or based on your defined values or range).

Kindly excuse me if this has been raised before (I can't seem to find any related post).

Appreciate your time,
- byehp
 

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.
byehp.

Maybe something like the below but no doubt needs some work. I approached a similar senario in this way:
Test one by one to see if each word in a list exists in a range.
If a word does not exist, go to the last empty cell on that row.
Enter the missing word in the cell.
Continue testing words in the list.
After all needed words are present, sort the columns.

Best of luck,

GL



'Are required fields present?

For Each Word In Array("2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012")
Set aRange = Range("a1:iv1").Find(What:=Word, LookAt:=xlWhole, MatchCase:=False)
If aRange Is Nothing Then
Cells(1, Columns.Count).End(xlToLeft)=Word
End If
Next Word


'Sort columns
Cells.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, _
DataOption1:=xlSortNormal
 
Upvote 0
Hi GL,

Thanks for your reply.
I still need your help though.

Kindly excuse me, I forgot to tell you that the year is not at the end of the report (there are two more columns after them). Unfortunately, I can't use "Nothing".
For example, Cell AH2 is year 2004 to Cell AP2 for year 2012. And then there're two other columns for totals (columns AQ2 and AR2).

Is there a way to search for missing year(s) then insert a whole column by moving the other columns to the right? Then continue on searching for other years chronologically.
In this way, the year will all be sequentially arranged.

Cheers,
- bye
 
Upvote 0
bye,

In Excel, all things are possible. Sometimes you just need to approach a problem from a different side.

It sounds like you need a routine that flows like this:

If AH2<>"2004" then
ActivateCell.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
AH2 = "2004"
End If

If AI2<>"2005" then
ActivateCell.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
AI2 = "2005"
End If

and so on....

End sub
 
Upvote 0
Hi GL,

Thanks for the two ideas.
Kindly excuse me for asking like a beginner though. :biggrin:

I'll try the arrays and also the IF conditions tomorrow.

Thanks again and season's greetings,
- bye
 
Upvote 0
bye,

I just noticed typos: ActivateCell should be ActiveCell

I am sure the VBA can be more efficiently written but you have just a few items you are looking for so hopefully it will run quickly.

To speed up execution of VBA, put this at the beginning: Application.ScreenUpdate=False
This tells Excel not to display updates during the running of the macro on your screen. Only show the final results on screen when the code is done running.

GL
 
Upvote 0
Hi GL,

I'll take note of the ActiveCell tomorrow.

Currently, I am just adding this feature to our current macro.
I've included the toggling of ScreenUpdate but thanks for reminding that becaus it surely helps performance.

Cheers, GL.
- bye
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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