Loop the same procedure until user interference

iqbal88

New Member
Joined
Dec 11, 2013
Messages
14
Hello Experts,

I new in VBA, I need your help.
How do I loop the procedure/macro until I cancel the InputBox or remain empty value InputBox??


Code:
Sub FundData()

Dim TikerName As String


    TikerName = Application.InputBox(Prompt:="Please enter Share Code", Type:=2)
    
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://dsebd.org/displayCompany.php?name=" & TikerName, Destination:=Range( _
        "$A$1"))
        '.CommandType = 0
        .Name = "displayCompany.php?name=" & TikerName
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = """company"""
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    ActiveSheet.Name = TikerName
    Sheets.Add After:=ActiveSheet
    ThisWorkbook.Save
    
End Sub

TIA,
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The code should not continue until you dismiss the input box. What is happening when you run your code?
 
Upvote 0
Thanks for your quick reply

One thing to remember, there have no loop condition in my code. But I need to loop the procedure.
And then I have to loop the procedure till InputBox ok with a correct string.
Should exit the macro, when value is empty or click cancel button

now if I cancel the InputBox, show me an error message like,

Runtime error '1004'
Application-defined or object-defined error

and also create a new sheet named "FALSE", with some incomplete data.
 
Last edited:
Upvote 0
Ok,

Code:
Dim TikerName As String

TikerName = "Name"     'I would use a Do Until ... Loop and use TikerName to control the continuation but to enter the loop TikerName must have a value 

Do Until TikerName=""    ' The Input returns a Null String if you hit cancel so we exit the loop if that occurs

TikerName = Application.InputBox(Prompt:="Please enter Share Code", Type:=2)  

If TikerName = "" Then                ' Because we redefine the TikerName value inside the loop we need to catch the cancel button before the rest of the loop executes 
Exit Sub
End if

'The rest of your code

Loop
 
Last edited:
Upvote 0
Thanks for your reply.
When I remain blank the inputbox, and click OK, then then the Macro (loop) stop run.
But if I click cancel button in input, it show an error

Run time error '1004'
That name is already taken. Try different one

the code is
Code:
Sub FundData()

Dim TikerName As String


    TikerName = "Name"     'I would use a Do Until ... Loop and use TikerName to control the continuation but to enter the loop TikerName must have a value
    Do Until TikerName = ""  ' The Input returns a Null String if you hit cancel so we exit the loop if that occurs
    TikerName = Application.InputBox(Prompt:="Please enter Share Code", Type:=2)
    If TikerName = "" Then                ' Because we redefine the TikerName value inside the loop we need to catch the cancel button before the rest of the loop executes
    Exit Sub
    End If
    
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://dsebd.org/displayCompany.php?name=" & TikerName, Destination:=Range( _
        "$A$1"))
        '.CommandType = 0
        .Name = "displayCompany.php?name=" & TikerName
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = """company"""
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    ActiveSheet.Name = TikerName
    Sheets.Add After:=ActiveSheet
    ThisWorkbook.Save
    Loop
    
End Sub
 
Upvote 0
Is the error on the If Statement or somewhere else?

The only thought I have left is to use InputBox instead of Application.InputBox (you would not need the or ="False" because InputBox cancel returns an empty string whereas Application.InputBox returns False when cancel is clicked

InputBox is a Function
Application.InputBox is a Method
 
Upvote 0
In If statement


Code:
If TikerName = "" Or TikerName = False Then

if click cancel inputbox then it work, if remain blank the box & click ok;
it shows the error
 
Upvote 0

Forum statistics

Threads
1,216,172
Messages
6,129,289
Members
449,498
Latest member
Lee_ray

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