Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internet-Explorer won't get closed after my module ends

I have this macro that pulls out data from a website. I get the inputs from a user-from. It works with no error but IE won't close and sucks up all of the memory. Is something else needed rather than IE.Quit?

This is the sub. As you can see I close IE at the end.

Public Cancel As Boolean

Sub USGD()

Dim IE As Object
Dim iWsh As Worksheet
Dim link As String
Dim sDate As String
Dim eDate As String
Dim StationID As String
    
Cancel = False

With USGS
    .StartUpPosition = 0
    .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
    .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
    .Show
End With

If Cancel = True Then
    Unload USGS
    Exit Sub
End If

With ActiveWorkbook
    Set iWsh = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With

iWsh.Activate
iWsh.Range("A1").Select 'I know this is not efficient but works fine


 StationID = USGS.TextBox1.Text
'StationID = InputBox("Please enter the station ID")
    
'sDate = InputBox("Please enter START date in this format: 'yyyy-mm-dd'")
'eDate = InputBox("Please enter END date in this format: 'yyyy-mm-dd'")
 sDate = Format(USGS.TextBox2.Text, "yyyy-mm-dd")
 eDate = Format(USGS.TextBox3.Text, "yyyy-mm-dd")
 
 
link = "https://waterdata.usgs.gov/ & _ 
StationID & sDate & eDate
 
 
 Unload USGS
 
Set IE = CreateObject("InternetExplorer.Application")
With IE
           .Visible = False
           .Navigate link 'URL
           
  Do Until .ReadyState = 4: DoEvents: Loop
  
           .ExecWB 17, 0 '// SelectAll
           .ExecWB 12, 2 '// Copy selection
End With
     

    iWsh.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
    Range("A1").Select
    
    IE.Quit
    Set IE = Nothing
    Set iWsh = Nothing
    
End Sub

And this is the user-form: I didn't have this problem while using Input-Box so I am guessing, it has something to do with the user-form. This only happens when user closes the user-form.

Private Sub ToggleButton1_Click()
Me.Hide
    Cancel = True
End Sub

Private Sub OK_Click()
Me.Hide
End Sub

Note: If the user cancel, it would not even open the IE and exits the sub right after.

But if user closes the form, it does open IE, and doesn't set the Cancel to be True which is the condition to exit the sub.

Update: Expert-Exchange covered the issue but never came up with an actual solution.

Update-2: Closing all instances of IE is not an option.

This is how user-form is set up now:

enter image description here

like image 861
M-- Avatar asked Nov 08 '22 22:11

M--


1 Answers

OK so I am unable to replicate the error, so there are two things you should try:

  1. Reboot your computer and verify the error continues. If not, problem solved.
  2. Recreate the UserForm & code in a new, blank workbook, and see if the error continues. If not, problem solved.

(Sometimes Workbooks, and/or UserForms become corrupted)

I also re-factored the code a little bit, which you might consider even if one of the above suggestions solves the problem. It just cleans it up a little bit and makes it more purposeful.

In a standard module, place the following code:

The USGD procedure displays the userform and unloads it. A separate procedure named GetData will do the work in IE and add the worksheet, etc. The GetData procedure is only executed if the user clicks on the "OK" button on the form. Therefore, the "X"/cancel button will allow the user to close the form.

Option Explicit

Sub USGD()
'Procedure displays the userform for the user
Dim USGSForm As New USGS
With USGSForm
    .StartUpPosition = 0
    .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
    .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
    .Show
End With
Unload USGSForm
End Sub

Sub GetData(StationID As String, sDate As String, eDate As String)
'This procedure queries the InternetExplorer for the values from UserForm
Dim iWsh As Worksheet
Dim link As String
Dim IE As Object

sDate = Format(sDate, "yyyy-mm-dd")
eDate = Format(eDate, "yyyy-mm-dd")

link = "https://waterdata.usgs.gov/nwis/dv?cb_00060=on&format=rdb&site_no=" & _
    StationID & "&referred_module=sw&period=&begin_date=" & sDate & "&end_date=" & eDate

Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = False
    .Navigate link 'URL
    Do Until .ReadyState = 4: DoEvents: Loop
    .ExecWB 17, 0 '// SelectAll
    .ExecWB 12, 2 '// Copy selection
    .Quit
End With

With ActiveWorkbook
    Set iWsh = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With
iWsh.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
Application.GoTo iWsh.Range("A1")

End Sub

In your UserForm module, place the following code:

This is the code for the "OK" button, which takes the values from TextBoxes on the form and sends those to the GetData procedure. Note the Select Case logic which will exit the procedure early if any of the parameters are empty, so it will not call GetData.

Private Sub OK_Click()
    Dim id As String, sDate As String, eDate As String
    'Get values from the form
    id = Me.TextBox1.Value
    sDate = Me.TextBox2.Value
    eDate = Me.TextBox3.Value
    'Hide the form
    Me.Hide

    'If ANY required parameter is blank, this results in malformed URL so exit the procedure
    Select Case vbNullString
        Case id, sDate, eDate
            MsgBox "You left some parameter blank, no query will be performed.", vbInformation
            GoTo EarlyExit
        Case Else
            'Send values to the procedure that queries IE
            Call GetData(id, sDate, eDate)
    End Select

EarlyExit:
End Sub
like image 189
David Zemens Avatar answered Nov 15 '22 07:11

David Zemens