forked from farishadi/Excel_Macro_References
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddNewCol
More file actions
26 lines (21 loc) · 958 Bytes
/
AddNewCol
File metadata and controls
26 lines (21 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'Alternative single line dynamic code
'Code will add column to the left of the address, taking right cells as format
'A1 is the address of the cell to start adding columns to
'3 is the number of columns to add
[A1].Resize(, 3).EntireColumn.Insert
Cells(1, 1).Resize(, 3).EntireColumn.Insert
'Call Method :
a = addNewCol("A1", True)
'where a returns the address of the newly added column
Public Function addNewCol(ByVal targAdd As String, Optional bLeftAdd As Boolean = False) As String
'this function adds a column to the left or right the target address provided, depending on the boolean flag indicator
If bLeftAdd <> True Then
'insert a column to the right of the cell
ActiveSheet.Range(targAdd).EntireColumn.Offset(0, 1).Insert
addNewCol = Range(targAdd).Offset(0, 1).Address
Else
'insert a column to the left of the cell
ActiveSheet.Range(targAdd).EntireColumn.Insert
addNewCol = Range(targAdd).Address
End If
End Function