VBA Option / Command Button Problem

Southanesq

New Member
Joined
Mar 29, 2008
Messages
4
I’m a n00b with VBA, so please bear with me.<o:p></o:p>
<o:p> </o:p>
I’m running three separate macro(s) that change a selected range of text to either lower, upper or proper (capital letter followed by lower) case.<o:p></o:p>
<o:p> </o:p>
The code I’m using x3 is as follows: -<o:p></o:p>
<o:p> </o:p>
Sub ChangeCaseLower()<o:p></o:p>
Dim Rng As Range<o:p></o:p>
On Error Resume Next<o:p></o:p>
Err.Clear<o:p></o:p>
Application.EnableEvents = False<o:p></o:p>
For Each Rng In Selection.SpecialCells(xlCellTypeConstants, _<o:p></o:p>
xlTextValues).Cells<o:p></o:p>
If Err.Number = 0 Then<o:p></o:p>
' Rng.Value = StrConv(Rng.Text, vbUpperCase)<o:p></o:p>
' Rng.Value = StrConv(Rng.Text, vbLowerCase)<o:p></o:p>
' Rng.Value = StrConv(Rng.Text, vbProperCase)<o:p></o:p>
End If<o:p></o:p>
Next Rng<o:p></o:p>
Application.EnableEvents = True<o:p></o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
… With the appropriate Rng.Value for each action.<o:p></o:p>
<o:p> </o:p>
I’ve created a UserForm with OptionButton(s) for each change of case.<o:p></o:p>
<o:p> </o:p>
If I paste the above code into the OptionButton, the text will change as soon as I run the form and make the selection. However, I’ve created a CommandButton that I want to use to change the text.<o:p></o:p>
<o:p> </o:p>
What I want to do is select some cells with text; run the UserForm; select the appropriate option and then press the CommandButton to run the selection I’ve made.<o:p></o:p>
<o:p> </o:p>
Any help would be appreciated. :rolleyes: <o:p></o:p>
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Test which OptionButton is checked in the code for your CommandButton, eg:

Code:
If OptUpper.Value = True Then
    Rng.Value = StrConv(Rng.Text, vbUpperCase)
ElseIf OptLower.Value = True Then
    Rng.Value = StrConv(Rng.Text, vbLowerCase)
Else
    Rng.Value = StrConv(Rng.Text, vbProperCase)
End If

Change the OptionButton references to suit.
 
Upvote 0
Andrew,

Thank you very much for your reply. :pray:

With your help, I managed to get the behaviour I wanted. I've learned a lot.

I have a few follow-up questions, though.

Once I click CommandButton1 to run my choice I would like to unload the UserForm.

I've come up with this code: -

If CommandButton1.Value = True Then
Unload UserForm1

End If

... to no avail. Do you have an alternative suggestion?

If so, where would it go?

My new code looks like this: -

Private Sub CommandButton1_Click()
If OptionButton1.Value = True Then
Dim Rng As Range
On Error Resume Next
Err.Clear
Application.EnableEvents = False
For Each Rng In Selection.SpecialCells(xlCellTypeConstants, _
xlTextValues).Cells
If Err.Number = 0 Then
Rng.Value = StrConv(Rng.Text, vbUpperCase)
End If

Next Rng
Application.EnableEvents = True
End If

If OptionButton2.Value = True Then
For Each Rng In Selection.SpecialCells(xlCellTypeConstants, _
xlTextValues).Cells
If Err.Number = 0 Then
Rng.Value = StrConv(Rng.Text, vbLowerCase)
End If

Next Rng
Application.EnableEvents = True

End If
If OptionButton3.Value = True Then
For Each Rng In Selection.SpecialCells(xlCellTypeConstants, _
xlTextValues).Cells
If Err.Number = 0 Then
Rng.Value = StrConv(Rng.Text, vbProperCase)
End If

Next Rng
Application.EnableEvents = True
End If

End Sub

Finally, I've noticed that if I run these scripts with only one cell selected it changes the case of the entire work sheet!

I've created another UserForm as an error message (selectRange), but I am not sure how to change my existing code to link to it if only one cell is selected.

Thanks in advance.
 
Upvote 0
You've come across a couple of Excel VBA gotchas!
Once I click CommandButton1 to run my choice I would like to unload the UserForm.

I've come up with this code: -

If CommandButton1.Value = True Then
Unload UserForm1

End If

... to no avail. Do you have an alternative suggestion?

If so, where would it go?
The Value property of a CommandButton is always False, so just use:
Code:
Private Sub CommandButton1_Click()
    Unload UserForm1
End Sub
Finally, I've noticed that if I run these scripts with only one cell selected it changes the case of the entire work sheet!
Yes, if you select only a single cell Excel assumes you want to work with the entire worksheet. The following code checks for Selection.Count being 1 (i.e. 1 cell selected) and converts that cell, otherwise it converts all the selected cells.
Code:
Private Sub Convert()
    Dim rng As Range
    
    If OptionButton1.Value Then
        If Selection.Count = 1 Then
            Selection.Value = StrConv(Selection.Text, vbUpperCase)
        Else
            Application.EnableEvents = False
            On Error Resume Next
            For Each rng In Selection.SpecialCells(xlCellTypeConstants, xlTextValues).Cells
                rng.Value = StrConv(rng.Text, vbUpperCase)
            Next rng
            On Error GoTo 0
            Application.EnableEvents = True
        End If
    End If
    
    If OptionButton2.Value Then
        If Selection.Count = 1 Then
            Selection.Value = StrConv(Selection.Text, vbLowerCase)
        Else
            On Error Resume Next
            For Each rng In Selection.SpecialCells(xlCellTypeConstants, xlTextValues).Cells
                rng.Value = StrConv(rng.Text, vbLowerCase)
            Next rng
            On Error GoTo 0
            Application.EnableEvents = True
        End If
    End If
    
    If OptionButton3.Value Then
        If Selection.Count = 1 Then
            Selection.Value = StrConv(Selection.Text, vbProperCase)
        Else
            On Error Resume Next
            For Each rng In Selection.SpecialCells(xlCellTypeConstants, xlTextValues).Cells
                rng.Value = StrConv(rng.Text, vbProperCase)
            Next rng
            On Error GoTo 0
            Application.EnableEvents = True
        End If
    End If

End Sub
 
Upvote 0
Hi John

Thanks for your help.

I knew I was missing the obvious with the CommandButton :LOL:

Your code for selecting a single cell worked a treat, and I've spend this morning using it on other scripts I'm developing.

Man, I've learnt more in two days here than in the last couple of months!

Thanks again!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

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