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.
 
For info only, in case someone needs it for the future

I have managed to fix the problem, should anyone ever need this in the future here is the work around. Also see by last post link and a big thanks to MikeRickson for his support.

I done it with a DoWhile Loop. This part of the code went in between browser (IE) is fully loaded and before the FOR EACH LOOP, then this code is triggered.

THE CODE
  • It places a 1hr wait time in Z1, then THE DoWhile loop runs.
  • When Button2 is clicked The time changes to under 3sec and the sheet recalclates
    column Z.
  • Then the if statement kicks in and as the DoWhile states the time should be more than 3sec and Z1 now is 2sec the GOto runs next and goes to the lable I
    created to tell the code to start here. That is just before the FOR
    EACH loop
VBA Code:
'#################### APPLICATION WAIT ################
Dim i As Integer
Dim Response As Integer
Sheets("Sheet3").Range("Z1").Value = "01:00:00" 'will place the 1hr wait time in Sheet3 Z1
t = CStr(Range("Z1"))
    WaitUntil = Now + TimeValue(t)

Do While Now > "00:00:03"
Sheets("Sheet3").Columns("Z").Calculate

If Sheets("Sheet3").Range("Z1").Value = "00:00:02" Then
        GoTo StartForLoop_Restart ' use GOTo command and label to reinitiate the sub
End If
DoEvents
Loop
'#################### APPLICATION WAIT ################

'Restart the code HERE, this is the key part
StartForLoop_Restart:

    'FOR LOOP
    For Each element In elements
      DoEvents

This is what I put in Button2
VBA Code:
Sheets("Sheet3").Range("Z1").Value = "00:00:02"
Sheets("Sheet3").Columns("Z").Calculate
I know its not the best code in the world and could be written better, but the workaround works and hopefully someone can do a much better job in writing it in the future.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,589
Messages
6,120,415
Members
448,960
Latest member
AKSMITH

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