PDA

View Full Version : Solved: Code wont run in certain modules



rhk001
08-11-2012, 01:36 AM
Hi guys,
I have this code to insert columns into a worksheet. (See below)
This works perfectly when it is in a module on its own (Module1) but
as soon as I add it into the module I have with my other routines (Rob) it returns the error below. I am sure it is something simple, anyone have any ideas ?

Thanks
Rob

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Variable not defined
---------------------------
OK Help
---------------------------




Sub cols()

col = Array("b", "c", "d", "e")
For i = UBound(col) To LBound(col) Step -1
Range(col(i) & ":" & col(i)).Insert Shift:=xlToLeft
Next i

End Sub

snb
08-11-2012, 01:08 PM
Remove 'Option Explicit'

Tinbendr
08-11-2012, 06:37 PM
Or learn how to define your variables.

Sub cols()
Dim Col As Variant
Dim I As Long

Col = Array("b", "c", "d", "e")
For I = UBound(Col) To LBound(Col) Step -1
Range(Col(I) & ":" & Col(I)).Insert Shift:=xlToLeft
Next I
End Sub

snb
08-12-2012, 01:27 AM
or to avoid them:

Sub snb()
Range("B1,C1,D1,E1").EntireColumn.Insert
End Sub

Bob Phillips
08-12-2012, 03:18 AM
Don't avoid them, that is terrible advice. Take David's advice.