How to split One code between Two Command Buttons

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
Office Version
  1. 2016
Platform
  1. Windows
Hi
Is there a way to run ONE code in two parts, so I split the code into two, I already have a code that I can use, with minor modification, I just don't know how to split it, so it recoganises that the second part of the code is part of the original code (First Part).

The user has to go online an logon to a website so. I need the following
  1. Command Button1 click = Open the url, user logs on to the site
  2. Command Button2 click = The second part of the code runs.
1586609522548.png


The second part would be a sub procedure of the first code. I was going to use webBrowser1, but the site does not display correctly in it so I am sticking with IE.

This is the first half, the second is a For Each Loop,

VBA Code:
Dim Html As HTMLDocument
Dim objIE As Object
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
Dim pageNumber As Long ' page no.
Dim nextPageElement As Object 'page element
Dim HtmlText As Variant ' for html data

Dim wsSheet As Worksheet ' WorkSheet
Dim wb As Workbook
    Set wb = ThisWorkbook
         Set wsSheet = wb.Sheets("Sheet3")
 y = 2 ' Start on row 2

'+++++ Internet Explorer ++++++
Set objIE = New InternetExplorer 'initiating a new instance of Internet Explorer and asigning it to objIE
 ' With objIE ' added by me
        objIE.Visible = True 'make IE browser visible
      objIE.navigate Sheets("Sheet3").Range("A2").Value & Replace(Worksheets("Sheet3").Range("B2") & Range("C2").Value, " ", "+") 'navigate IE to this web page

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 'wait here a few seconds while the browser is busy

 Set ie = CreateObject("InternetExplorer.Application")
   Set Html = objIE.document
Set elements = Html.getElementsByClassName("sresult lvresult clearfix li shic") ' parent CLASS

' From here on it is the FOR EACH LOOP in the code, which I took out and placed in the second command button.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi
I think this might be possible with a DoEvent, but im not 100% how to do it in my code. I am assuming it will go before the For Each Loop. See Link on DoEvent Link
 
Upvote 0
It would be cleanest to split your current Sub into two Subs and assign the new smaller routines to the appropriate button.

But, another alternative would be to use a branching flag

Current:
VBA Code:
Sub MyRoutine()
    ' part one

    ' part two
End Sub

Becomes:

VBA Code:
Public SkipFirstPart as Boolean

Sub MyAltertedRoutine()
    If Not SkipFirstPart Then
        ' part one
    Else
        SkipFirstPart = False
        ' part two
    End If
End Sub

Sub CallTheSecondPart()
    SkipFirstPart = True
    Call MyAlteredRoutine
End Sub
MyAlteredRoutine would be assigned to the first button and CallTheSecondPart would be assigned to the second button.

NOTE: that the variables (scoped to the procedure level) that were calculated in running MyAlteredRoutine (from 'part one) will lose their values between the buttons being pressed. To deal with this you would have to make those variables Static variable and make sure that your 'part one started by explicitly assigning the default values that you expect. (e.g. setting all numeric variables to 0).

(Edit: I just realized that putting END in the 'part two would also re-default the static variables as an alternative to setting them explicitly in 'part one)
 
Upvote 0
Thanks for this, I am not sure if I have done it correct as I keep getting this error message on Call MyAlteredRoutine.

I have tried to change the button names, still no luck also tried to create a Module1 with MyAlteredRoutine in it, still no luck, Also tried with change Module1 name to MyAlteredRoutine still no luck
1586627040949.png


Also I moved the Dim SkipFirstPart As Boolean to where it shows in read as I was getting an error message
1586627344088.png
 
Upvote 0
The SkipFirstPart has to be Public (edited since I first posted) Public module wide variable.

Also, check for typos, it looks like I spelt Altered two different ways
 
Upvote 0
I have made it a Public Module and change the typo.

The first half work fine, however i am having an issue on the second half.
CommandButton2 code
1586630446604.png


Above the RED line is the first half of the code. Below the red line is the second half which should run when commandbutton2 is clicked.
1586630345522.png


I get the first error message, I am not sure if I have split the code correct. As anything for the FOR Each loop I put in the second part. I left line of code Set elements in the first half. If I move that down to part two then i get , then I get the second error message Run time error91.
1586630535862.png
1586630731229.png
 
Upvote 0
As I said before, its way easier to take your existing code and split it into two different subs.
 
Upvote 0
I'm not sure how to do that, I will have to read up on it. I have never done that before
 
Upvote 0
I have tried to split the code in two subs, YouTube Link but I am still having the above second error message.

If splitting the code is not an answer what about pausing the code until the second command button is clicked. Could that be possible? if so how?

Thanks
 
Upvote 0
Also posted on Stackoverflow Link. Trying a different method
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,287
Members
449,149
Latest member
mwdbActuary

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