JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,676
- Office Version
- 365
- Platform
- Windows
I am trying to write a macro that will read in a column of special characters (up arrow, down arrow, etc.) that indicate whether a value is over or under some range. It will then scan another set of numbers, store the appropriate character in the cell to the left, and apply a color to both cells.
This code works as long as the characters are from the normal character set. But it fails if I select any special characters.
Here's the mini-sheet:
And here's the macro code. It is invoked by a Button control which is not included in the mini-sheet even though I selected a range that includes it.
If I use characters from the normal text character set, it works.
But if I use special characters from another character set, I get this:
Is there a way I can use any character and make it work?
Thanks
This code works as long as the characters are from the normal character set. But it fails if I select any special characters.
Here's the mini-sheet:
Book2 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | I | J | |||
1 | |||||||||||
2 | |||||||||||
3 | |||||||||||
4 | Ratings | Values | Expected | Unrated | |||||||
5 | 60 | Max | 84 | ↑ | 84 | ||||||
6 | 40 | Min | 39 | ↓ | 39 | ||||||
7 | 91 | ↑ | 91 | ||||||||
8 | ↑ | Too high | 7 | ↓ | 7 | ||||||
9 | → | Goldilocks | 55 | → | 55 | ||||||
10 | ↓ | Too low | 40 | → | 40 | ||||||
11 | 61 | ↑ | 61 | ||||||||
12 | 60 | → | 60 | ||||||||
Sheet1 |
And here's the macro code. It is invoked by a Button control which is not included in the mini-sheet even though I selected a range that includes it.
VBA Code:
Sub RateEm()
Const rnCodes As String = "Codes" 'Column of code characters & colors
Const rnValues As String = "Values" 'Column of values to be rated
Const rnRatings As String = "Ratings" 'Column to put ratings
Dim Values() As Variant 'Values array
Dim NumValues As Long 'Number of values
Dim Codes() As Variant 'Code characters
Dim NumCodes As Long 'Number of codes
Dim Colors() As Long 'Category colors
Dim Min As Long 'Minimum good value
Dim Max As Long 'Maximum good value
Dim iRtg As Long 'Rating index
Dim i As Long 'Loop index
Codes = Range(rnCodes).Value 'Read in the character codes & colors
NumCodes = UBound(Codes, 1) 'Number of codes
ReDim Colors(1 To NumCodes) 'Make colors array the same size
For i = 1 To NumCodes 'Get the code colors
Colors(i) = Range(rnCodes).Cells(i, 1).Interior.Color
Next i
Min = Range("Min").Value 'Get the minimum value
Max = Range("Max").Value 'Get the maximum value
Values = Range(rnValues).Value 'Read in the character codes
NumValues = UBound(Values, 1) 'Get the number of values
For i = 1 To NumValues
Select Case Range(rnValues).Cells(i, 1)
Case Is > Max: iRtg = 1
Case Is < Min: iRtg = 3
Case Else: iRtg = 2
End Select
Range(rnValues).Cells(i, 1).Interior.Color = Colors(iRtg)
Range(rnRatings).Cells(i, 1).Interior.Color = Colors(iRtg)
Range(rnRatings).Cells(i, 1).Value = Codes(iRtg, 1)
Next i
End Sub
If I use characters from the normal text character set, it works.
But if I use special characters from another character set, I get this:
Is there a way I can use any character and make it work?
Thanks