How to call a VBA function

jbunn

New Member
Joined
Feb 6, 2013
Messages
30
Hi There -

I have the following repetitive code which I use to calculate differences between various columns. Any ideas how I can simply the code or setup a function and call it within my code?
Code:
'Add Column and Difference
    Range("E:E").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveSheet.Range("E3").Select
    Selection.FormulaR1C1 = "Difference"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]-RC[-1]"
    ActiveCell.Select
    Selection.AutoFill Destination:=ActiveCell.Range("A1:A25000")
    Range("F:F").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    
'Add Column and Difference
    Range("I:I").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveSheet.Range("I3").Select
    Selection.FormulaR1C1 = "Difference"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]-RC[-1]"
    ActiveCell.Select
    Selection.AutoFill Destination:=ActiveCell.Range("A1:A25000")
    Range("J:J").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
 
Last edited by a moderator:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi jbunn,

Working with your existing code, you could do something like this....
Code:
Sub Main()
 Call InsertCopy(lColumn:=5)
 Call InsertCopy(lColumn:=8)

'---the keyword "Call" is optional and used for clarity
End Sub

Private Sub InsertCopy(lColumn As Long)
 Columns(lColumn).Select
 Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
 ActiveSheet.Cells(3, lColumn).Select
 Selection.FormulaR1C1 = "Difference"
 ActiveCell.Offset(1, 0).Range("A1").Select
 ActiveCell.FormulaR1C1 = "=RC[-2]-RC[-1]"
 ActiveCell.Select
 Selection.AutoFill Destination:=ActiveCell.Range("A1:A25000")
 Columns(lColumn + 1).Select
 Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub


However, it's much more efficient to just reference the objects directly without Selecting them.
Here's code that will arrive at the same result without Selecting any Cells...

Code:
Private Sub InsertCopy(lColumn As Long)
 Columns(lColumn).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
 Cells(3, lColumn).Formula = "Difference"
 Cells(4, lColumn).Resize(25000).FormulaR1C1 = "=RC[-2]-RC[-1]"
 Columns(lColumn + 1).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
 
Upvote 0
thanks Jerry -

I ended up doing a For Loop to simplify my code
Code:
For i = 5 To 105 Step 4

Columns(i).Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbov
ActiveSheet.Cells(3, i).Select
Selection.FormulaR1C1 = "Difference"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=RC[-2]-RC[-1]"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A25000")
Columns(i + 1).Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbov


Next i
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,222
Members
448,877
Latest member
gb24

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