Insert blank column after finding cell value

B-radK

Board Regular
Joined
Apr 1, 2010
Messages
96
Office Version
  1. 365
Platform
  1. Windows
Hello team,

I would like to know a vba code to find cell values, then insert a blank columns immediately after those cells.

For example, find a cell called "Bank" then insert a blank column afterwards. At the same time, find cells with "Transaction" and "Account" and insert blank columns after those too.

Any help would be greatly appreciated.

Thanks,
Brad.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
find a cell called "Bank" then insert a blank column afterwards. At the same time, find cells with "Transaction" and "Account" a
1. Will those values only be found once each on the sheet?

2. Where will they be found (eg 'always in row 1' or 'they could be anywhere' or ...)?

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version.
 
Upvote 0
Hello,

Thanks, I will update my details. I’m using Office 365.

Yes, those values will only occur once in the sheet.

The values will always be in Row 4.

Thanks,
Brad
 
Upvote 0
I will update my details.
Thanks for getting that done & the additional information.

Give this a try with a copy of your workbook.

VBA Code:
Sub Insert_Columns()
  Dim AllHdrs As Variant, Hdr As Variant
  Dim rFound As Range
  
  AllHdrs = Split("Bank|Transaction|Account", "|")
  For Each Hdr In AllHdrs
    Set rFound = Rows(4).Find(What:=Hdr, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
    If Not rFound Is Nothing Then Columns(rFound.Column + 1).Insert
  Next Hdr
End Sub
 
Upvote 0
Hello Peter,

Thank you very much, it works very well. (y)

I did try it with dates, however it didn't work. It did work when I changed the date format to general. I included in the code to add a new row above the date column, copy and paste the date, change the format to general, execute your code, then hide the row. A bit of a work around, but it's fine.

This is what I tried, but obviously failed:
VBA Code:
AllHdrs = Split("31-Mar-20|30-Apr-20|30-Jun-20", "|")

Thanks again, Peter, I really appreciate it.

Brad.
 
Upvote 0
Thank you very much, it works very well. (y)
You're welcome. :)

I did try it with dates, however it didn't work.
That is because Dates are actually Numbers, not Text like "31-Mar-20"
For example, 31-Mar-2020 is actually 43921

So, for actual dates, try like this

VBA Code:
Sub Insert_Columns_Dates()
  Dim AllHdrs As Variant, Hdr As Variant
  Dim rFound As Range
  
  AllHdrs = Split("31-Mar-20|30-Apr-20|30-Jun-20", "|")
  For Each Hdr In AllHdrs
    Set rFound = Rows(4).Find(What:=DateValue(Hdr), LookIn:=xlValues, LookAt:=xlWhole)
    If Not rFound Is Nothing Then Columns(rFound.Column + 1).Insert
  Next Hdr
End Sub
 
Upvote 0
G'day Peter,

That didn't work unfortunately mate. My work-around is working just fine though.

I tried swapping to "30/04/2020|31/05/2020|30/06/2020" as this is what's in the formula bar when I click on "30-Apr-20" but it didn't work.

I'm happy to keep trying, but I'm happy with my work around.

Thanks again,
Brad.
 
Upvote 0
That didn't work unfortunately
Suppose one of your heading dates is in cell D4, what does this formula return if you place it in a vacant cell?

=ISNUMBER(D4)

IF that formula returns True, then what does this formula return?
=INT(D4)=D4
 
Upvote 0
Hello Peter,

Terribly sorry for the delay. Preparing to work at home with the COVID-19 situation.

They both return TRUE.

Thanks,
Brad.
 
Upvote 0
They both return TRUE.
Then I don't know why the code from post # 6 does not work.

Here is my sheet before the code is run

20 04 01.xlsm
ABCDEFGHIJ
3
431/01/202029/02/202031/03/202030/04/202031/05/202030/06/202031/07/202031/08/202030/09/2020
5TRUETRUETRUETRUETRUETRUETRUETRUETRUE
6TRUETRUETRUETRUETRUETRUETRUETRUETRUE
Insert Columns (3)
Cell Formulas
RangeFormula
B5:J5B5=ISNUMBER(B4)
B6:J6B6=INT(B4)=B4


and here it is after running the post # 6 code

20 04 01.xlsm
ABCDEFGHIJKLM
3
431/01/202029/02/202031/03/202030/04/202031/05/202030/06/202031/07/202031/08/202030/09/2020
5TRUETRUETRUETRUETRUETRUETRUETRUETRUE
6TRUETRUETRUETRUETRUETRUETRUETRUETRUE
Insert Columns (3)
Cell Formulas
RangeFormula
B5:D5,F5,K5:M5,H5:I5B5=ISNUMBER(B4)
B6:D6,F6,K6:M6,H6:I6B6=INT(B4)=B4
 
Upvote 0

Forum statistics

Threads
1,216,371
Messages
6,130,218
Members
449,567
Latest member
ashsweety

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