Trying to pass a string variable inside Column instruction

Ricardo Caicedo

New Member
Joined
Aug 21, 2014
Messages
43
I try to pass a string variable in order to select the column where I need to insert a new column (after the header “Email Address”) but it fails run error 1004 as it looks like the object is not defined. I think VBA does not have the concept COLUMNS so it cannot accept a variable inside Columns instruction …..
The think when I did manually the macro, the instruction to select the column (i.e. column H) Columns("H:H"), BUT IT DID NOT WORK IN MY MACRO code.
I will apreciate your help, please see the code down here
Sub FillData()
'
' Creates the columns and compares the data
Dim NColumn As Integer
Dim FindColumn As Range
Dim RColumn As String
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Cells.Find(what:="Email address", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Set FindColumn = Cells.Find(what:="Email address", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
NColumn = FindColumn.Column
NColumn = NColumn + 1
RColumn = NColumn & ":" & NColumn
Columns(RColumn).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Ricardo Caicedo,

Try this...

Code:
Sub FillData()
' Creates the columns and compares the data
Dim NColumn As Integer
Dim FindColumn As Range
'Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Cells.Find(what:="Email address", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Set FindColumn = Cells.Find(what:="Email address", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
NColumn = FindColumn.Column + 1
Columns(NColumn).Insert
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub

Hope that helps.
 
Upvote 0
You are welcome.

Just noticed that I left an unnecessary 'find' in there, from your original code.

So can reduce to the below if you wish.

Code:
Sub FillData()
' Creates the columns and compares the data
Dim NColumn As Integer
Dim FindColumn As Range
'Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Set FindColumn = Cells.Find(what:="Email address", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
NColumn = FindColumn.Column + 1
Columns(NColumn).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,312
Messages
6,165,276
Members
451,949
Latest member
bovacik

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