Infinite Loop Help

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
As the title states my msgbox within my userform is stuck in an infinite loop. I decided to include every command button code there is on this form in case it will help to solve this problem. Also there is also one textbox as well. I've tried various types of loops except the For Loop because every For Loop example I have seen has a counter or some form of increment formula. What I would like to happen in my loop is if the user clicks on the command button labeled open and txtbxSelectFile.value="" then display the message box and keep doing this every time the cmdbtnOpen_Click is true and txtbxSelectFile.value="". The only thing that came close to working, was the If...Then conditional statement but it would not loop. It would only run once and then continued to the Else condition. I really hope my explanation makes sense. Thank you.

Code:
Private Sub cmdBrowse_Click()    
    'myFile = Application.GetOpenFilename(, , "Select a File.")
    
    Dim fname As String
    Dim fpath As String
    
    fpath = ThisWorkbook.Path
    
    With Application.FileDialog(msoFileDialogOpen)
        .InitialFileName = fpath
        .ButtonName = "Get File Name"
        .Title = "File Selection"
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xl; *.xlsx; *.xlsm; *.xlb; *.xlam; *.xltx; *.xltm; *.xls; *.xla; *.xlt; *.xlm; *.xlw"
        .AllowMultiSelect = False
        
        If .Show = True Then
            fname = .SelectedItems(1)
            Me.txtbxSelectFile.Text = fname
        Else
            MsgBox "Operation Canceled"
            Unload Me
        End If
    End With
End Sub


Private Sub cmdbtnOpen_Click()
    Do While txtbxSelectFile <> ""
        MsgBox "Please Select a file", vbOKOnly, "No File Selected"
    Loop
    Workbooks.Open Me.txtbxSelectFile
    Unload Me
    selectRangefrm.Show
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
In this sub:

Code:
Private Sub cmdbtnOpen_Click()
    Do While txtbxSelectFile <> ""
        MsgBox "Please Select a file", vbOKOnly, "No File Selected"
    Loop
    Workbooks.Open Me.txtbxSelectFile
    Unload Me
    selectRangefrm.Show
End Sub

Where is the value for the txtbxSelectFile supposed to be coming from...
 
Upvote 0
I think I might of misunderstood this loops usage. So let me rephrase the question. If the user keeps clicking the open button and there is nothing in the textbox then keep displaying the message box. The value from the textbox is supposed to come from a file browse button. When the user clicks the browse button a file dialog opens so the user can locate the file they want to open. Hope that helps. Thank you for the quick response.
 
Upvote 0
I think I understand what you are saying, but they are two different subs. When you "Unload" the form in the cmdBrowse_Click() sub the value in that textbox gets wiped out.

There are probably lots of ways to accomplish what you want to do. One way might be is to copy your fileDialog over again and put that in the loop so that if the textbox is empty it runs the filedialog so that the user can put the file name back in. Or perhaps declare the textbox as a module variable instead of a code variable, that way it will stay populated.

I hope this makes sense.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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