Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Macro that will run 2 loop macros

Status
Not open for further replies.

Igor09

Instructor
Sep 22, 2009
4
0
0
US
Hi,
What I am trying to accomplish is to have one macro that will run 2 loop macros. I have macro 1 that runs in the loop until it hits blank cell and then exits but I would like to run macro2 after that. Is this something that can be done? I tried to do this by using declare Sub feature but having problems with if statement that will start macro2. Can somebody help me please.
 



Hi,
Code:
Sub Main()
  macro1
  macro2
end sub


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi,
Thank you for the quick response but it is still now working. Here is my code (I am using same macro twice here just to get this figured out):

Declare Sub Macro1()
Declare Sub Macro2()

Global g_HostSettleTime%

Option Explicit

Sub Main()
macro1
macro2
end sub


'------------------------------------------------------------------------------------------------------------------------------------
Sub Macro1
Dim Session As Object
Dim Sess0 As Object
Dim System As Object
Set System = CreateObject("Extra.system")
If (System Is Nothing) Then
MsgBox "Could not Create Session"
Stop
End If
Set Session = System.Sessions
Set Sess0 = System.Activesession
g_HostSettleTime = 0100
Dim ObjExcel As Object
Dim ObjWorkbook As Object
Set ObjExcel = CreateObject("Excel.Application")
ObjExcel.Visible = True
Set ObjWorkbook = ObjExcel.Workbooks.Open("C:\Documents and Settings\My Documents\Test Municipal_New_CP_ Strats_ID_VI.xlsx")
BegRw = 2
EndRw = 200
With ObjExcel.Worksheets("Sheet1")
For X = BegRw To EndRw
ID = .Range("A" & X)
OS = .Range("B" & X)
end with

If ID = "" Then
msgbox "Macro has been completed"
ObjWorkbook.Save
ObjExcel.Quit
End Sub : end if

Sess0.Screen.SendKeys ("<HOME>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys (ID)
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.Moveto 08, 15
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
Sess0.Screen.SendKeys ("X<ENTER>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys ("CP")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys (OS)
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys ("<ENTER>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
C2 = Sess0.Screen.GetString(11, 18, 8)
ObjWorkbook.Worksheets("Sheet1").Cells(X, 3).Value = C2
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys ("<PF12>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys ("<PF12>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)

Next X

End Sub
'------------------------------------------------------------------------------------------------------------------------------------
Sub Macro2
Dim Session As Object
Dim Sess0 As Object
Dim System As Object
Set System = CreateObject("Extra.system")
If (System Is Nothing) Then
MsgBox "Could not Create Session"
Stop
End If
Set Session = System.Sessions
Set Sess0 = System.Activesession
g_HostSettleTime = 0100
Dim ObjExcel As Object
Dim ObjWorkbook As Object
Set ObjExcel = CreateObject("Excel.Application")
ObjExcel.Visible = True
Set ObjWorkbook = ObjExcel.Workbooks.Open("C:\Documents and Settings\My Documents\Test Municipal_New_CP_ Strats_ID_VI.xlsx")
BegRw = 2
EndRw = 200
With ObjExcel.Worksheets("Sheet1")
For X = BegRw To EndRw
ID = .Range("A" & X)
OS = .Range("B" & X)
end with

If ID = "" Then
msgbox "Macro has been completed"
ObjWorkbook.Save
ObjExcel.Quit
End Sub : end if
Sess0.Screen.SendKeys ("<HOME>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys (ID)
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.Moveto 08, 15
Sess0.Screen.WaitHostQuiet(g_HostSettleTime)
Sess0.Screen.SendKeys ("X<ENTER>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys ("CP")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys (OS)
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys ("<ENTER>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
C2 = Sess0.Screen.GetString(11, 18, 8)
ObjWorkbook.Worksheets("Sheet1").Cells(X, 3).Value = C2
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys ("<PF12>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
Sess0.Screen.SendKeys ("<PF12>")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)

Next X

End Sub

 



So how are you trying to run this?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I am sorry but I don't understand your question. What I am trying to do is run a macro that contains 2 macros that will run one after the other. In my example I am using same look macro twic just so that I get this feature working and then I can modify the code to run other macros.
 


running the Main macro runs the other two.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Code:
Declare Public Sub Main()
Declare Private Sub Badloop()
Declare Private Sub Goodloop()

Public Sub Main()
    Call GoodLoop
    Call BadLoop
End Sub

Private Sub Badloop()
    dim lng as Long
    dim result as long

    lng = 20
    result = 1

    do
        if lng < 2 then exit do
        result = result * lng
        lng = lng - 1
    loop

    msgbox lng & "! = " & result
End Sub

Private Sub GoodLoop()
    dim lng as long
    dim result as long

    lng = 20
    result = 1

    do while lng > 1
        result = result * lng
        lng = lng - 1
    loop

    msgbox lng & "! = " & result
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top