Userform textbox code

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Greetings, Here is a snippet of code that I am using to have a textbox in a userform call up a specific WS.

Code:
 If TextBox43 = 1 Then
        Sheet1.Activate
        End If
    If TextBox43 = 2 Then
        Sheet2.Activate
        End If
    If TextBox43 = 3 Then
        Sheet3.Activate
        End If
    If TextBox43 = 4 Then
        Sheet4.Activate
        End If
...all the way up to textbox43 = 40


dmt32 recently sent me this code tweak (thanks dmt32) for another project that worked great, but this code was just to locate the textboxes and clear their contents; not to see the value in a single textbox and pass it on to the code.

Code:
 Dim i As Integer
    For i = 1 To 40
    Me.Controls("TextBox" & i).Text = ""
    Next i
    TextBox43 = ""

I am wondering if I can apply this same principle to this set of code as well, so much cleaner....
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The CodeName attribute to the sheet is what you're using in your post. You could loop through each sheet within this workbook to see if the codename has your number.

Code:
Dim sh As Worksheet
Select Case TextBox43.Value
    Case 1 To 40
        For Each sh In ThisWorkbook.Sheets
            If InStr(1, sh.CodeName, TextBox43.Value, 1) > 0 Then sh.Activate
        Next sh
End Select
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,104
Members
449,205
Latest member
ralemanygarcia

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