Excel VBA Macro works on Step Into Mode but not in Run Sub Mode

DMumme

New Member
Joined
Jun 14, 2014
Messages
23
When running a macro in "Step Into" mode it performs correctly. When running the same macro in "Run Sub" mode it fails every time. In the "Run Sub" mode I receive an "Exception occurred." error message followed by "Run-time error '1004': Method 'Range' of object'_Global' failed" message.

I am very experienced in Excel and have written many complex macros. However, I am entirely self-taught and not well informed on the terminology or the reasons for the coding working the way it does. I am certainly not strong with User Forms.

The purpose of this macro is to send the user to a particular part of an alphabetically sorted list by the selecting the first letter from a User Form. The user form stores the chosen letter in a cell name "AlphaChoice" on the Data sheet. A second cell named "AlphaChoiceRowNum" has a formula that determines on which row the first word with that starting letter is found. The macro then takes the user to column A in the row number from that second cell. Pretty simple. When the macro fails during the "Run Sub" mode it appears that the chosen letter was not stored at 'Data'!AlphaChoice.

Immediately below is the code for the macro followed by the code ran by clicking in the User Form. Any help would be appreciated.

MACRO
=============
Code:
Sub RowFinder()


    Range("'Data'!AlphaChoice").ClearContents


    UF1AlphaSelect.Show


    AlphaChoiceRowNum = Range("'Data'!AlphaChoiceRowNum")
    
    GoToLocation = "A" & AlphaChoiceRowNum
    
    Application.Goto Range(GoToLocation), True
       
End Sub

USER FORM
=================
Code:
Private Sub AlphaListBox_Click()


    AlphaChoiceRowNum = Range("'Data'!AlphaChoiceRowNum")
    UF1AlphaSelect.Hide
    
End Sub
 
The properties for the AlphaListBox has a ControlSource of AlphaChoice. Is that not the proper way to link the selection to a cell?
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
It depends on your point of view. Personally, I never link controls directly to cells as it makes it a little unpredictable as to when their events may fire. I prefer to use code to populate both the controls and their related cells.
 
Upvote 0
Does your response indicate that you would leave the ControlSource property of the ListBox blank? Would you provide an example of code that would populate both the controls and their related cells?
 
Upvote 0
Yes, I never use ControlSource or Rowsource. To populate a Listbox I use its List property - for example:

Code:
Me.Listbox1.list = Sheets("some sheet").Range("A1"A50").Value

then to assign a value, you simply use:

Code:
Application.Range("'Data'!AlphaChoiceRowNum").Value = me.listbox1.value

or similar.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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