This code runs but I need a way to define variables x
, n
and c
only once, as say module-wide variables. The example provided is a condensed version of a bigger macro that ‘Calls’ many procedures.
Presently I have to put the variables into each Sub
for it to run as required. I tried declaring them upfront using Public
, Private
, Dim
etc but the n
and c
variables don't reset to 0
after each Sub
is run. Thus the variable count continues on to the following procedure from the previous & I need the counter to reset after each sub has run. The x = "Minimum"
outside the Subs (i.e. with declared variables) doesn't work either but would be useful for efficiency.
Also:
- How to simplify the
Main
macro that usesCall
. Sometimes I have 50 Subs. - Can one consolidate this particular type of
Sub
(the 2 here are identical in that I send data to the same worksheet 'Rs' but just from 2 different worksheets, 'Ts' & 'BR', in the same excel file)
Dim module As Object
Sub Main()
Call Stic1
Call Stic2
End Sub
Sub Stic1()
Dim x As String
Dim n As Integer, c As Integer
x = "Minimum"
Sheets("Ts").Select
Range("A2").Offset(0, 0).Select
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = x Then
ActiveCell.Offset(0, 1).Copy
Application.Goto (ActiveWorkbook.Sheets("Rs").Range("G2").Offset(n, 0))
ActiveSheet.Paste
Application.CutCopyMode = False
n = n + 1
Application.Goto (ActiveWorkbook.Sheets("Ts").Range("A2").Offset(c, 0))
c = c + 1
Else
Application.Goto (ActiveWorkbook.Sheets("Ts").Range("A2").Offset(c, 0))
c = c + 1
End If
Application.Goto (ActiveWorkbook.Sheets("Ts").Range("A2").Offset(c, 0))
Loop
End Sub
Sub Stic2()
Dim x As String
Dim n As Integer, c As Integer
x = "Minimum"
Sheets("BR").Select
Range("A2").Offset(0, 0).Select
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = x Then
ActiveCell.Offset(0, 1).Copy
Application.Goto (ActiveWorkbook.Sheets("Rs").Range("I2").Offset(n, 0))
ActiveSheet.Paste
Application.CutCopyMode = False
n = n + 1
Application.Goto (ActiveWorkbook.Sheets("BR").Range("A2").Offset(c, 0))
c = c + 1
Else
Application.Goto (ActiveWorkbook.Sheets("BR").Range("A2").Offset(c, 0))
c = c + 1
End If
Application.Goto (ActiveWorkbook.Sheets("BR").Range("A2").Offset(c, 0))
Loop
End Sub