Internet Explorer will not close after completing my module

I have this macro that retrieves data from a website. I get data from the user. It works without errors, but for the reason that I do not see, IE does not close or suck all my RAM. Is anything else needed, and not IE.Quit?

This is sub. As you can see, I am closing 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 did not have this problem when using the Input-Box, so I guessed it, and now I'm sure this has something to do with the user form. And this only happens when the 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 cancels, he will not even open IE and after that will exit sub.

IE Cancel True, .

: -Exchange , .

-2: IE .

:

enter image description here

+4
2

, , :

  • , . , .
  • UserForm , . , .

( / )

, , . .

:

USGD . GetData IE .. GetData , "" . "X" / .

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

UserForm :

"", GetData. Select Case, , - , 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
+1

, . , , .

, . Cancel True

Cancel = True

OK, false. :

Private Sub ToggleButton1_Click()

End Sub

Private Sub OK_Click()
    Me.Hide
    Cancel = False
End Sub

, ; . . , , . - ;

If sDate = "" Or eDate = "" Or StationID = "" Then GoTo 92
'. Rest of ...
'. My ... 
'. Code ...
92:
           Set IE = Nothing

End Sub
0

Source: https://habr.com/ru/post/1676399/


All Articles