Using variables for vb[color]

Novice_excel

New Member
Joined
May 17, 2010
Messages
17
Greetings everyone,

Can't say I contribute a whole lot to this community but I sure appreciate the information provided here.

I have some problem using a variable in place of the following code;

Code:
    If HighLightResponse = vbYes Then
        
        If ColorResponse = Empty Then
            ColorResponse = InputBox(prompt:="Please pick color of highlight (vb+; Blue, Red, Green", _
               Title:="Highlight Color")
        End If
        
        MsgBox "vb" & ColorResponse
        
        'Rows(RowNum).Font.Color = vbRed
        Rows(RowNum).Font.Color = ColorResponse
    End If

For some reason, vb does not recognize the ColorResponse variable. When I do run the code, I get the type mismatch error.

It is dim as string and it will work if the value was hardcoded into ColorResponse (ColorResponse = vbRed) but it will not work if the data came from the input box.

Someone help :)
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
No, you cannot manipulate variables or constants like that. what are you actually trying to do>
 
Upvote 0
In that code the variable colorResponse is a string

The .Color property of a range is a number. not a string.

Cocanenating strings will not create variable names that VBA will recognize.

You'd need code like this. (Note the use of the ColorIndex property rahther than Color

Code:
Dim ColorResponse as String
Dim myColorIndex as Long

ColorResponse = InputBox("Input a color")
If ColorResponse = vbNullString Then Exit Sub: Rem canceled

Select Case LCase(ColorResponse)
    Case "red"
        myColorIndex = vbRed
    Case "blue"
        myColorIndex = vbBlue
    Case "green"
        myColorIndex = vbGreen
    Rem etc
End Select

If myColorIndex = 0 then Exit Sub: Rem bad entry e.g. "fish"

Rows(1).ColorIndex = myColorIndex
 
Upvote 0
This macro is supposed to give the user a choice of colors for use when it finds the desired string.

I wanted to use the input box option to allow the user to enter 'red' or 'blue' and the macro automatically uses that color scheme.

I hope I explained it correctly :biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

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