Hi folks,
I'm trying to select a number of cells to format using the Range method but it seems that when the selection string becomes too long, the method fails.
This is some test code that illustrates the problem, Test 1 - all values work, whereas test 2 fails:
Other things to note:
The strings will not be sequences of numbers that could be replaced by ($D$14:$D$55)
When I put a watch on the TEST_2 string the last few characters are shown as:
"...,$D$52,$D$53,$D$5,"
instead of what I would have expected which is:
"...$D$52,$D$53,$D$54,$D$55"
However if I print the string variable to the immediate window it appears as I'd expect it to. I don't know enough about the VB editor to know whether that last point is just a distraction or not, but if it's not related I think it's a strange co-incidence that the point that the Range method fails is the same point that the VBE watch does too.
Note that all of these cells exist and are editable which is shown by TEST_1b and TEST_1a.
All help and suggestions of how to get the Range method working properly would be appreciated
Thanks,
Ken.
Excel 2002 SP3, Windows XP
I'm trying to select a number of cells to format using the Range method but it seems that when the selection string becomes too long, the method fails.
This is some test code that illustrates the problem, Test 1 - all values work, whereas test 2 fails:
Code:
Sub xval_mr_excel()
Dim TEST_1a As String
TEST_1a = "$D$13"
Dim TEST_1b As String
TEST_1b = "$D$45,$D$48,$D$49,$D$52,$D$53,$D$54,$D$55,$D$56,$D$57,$D$58,$D$59,$D$60,$D$61,$D$62"
Dim TEST_1c As String
'limit of acceptable range parameter
TEST_1c =
"$D$13,$D$24,$D$25,$D$36,$D$67,$D$98,$D$20,$D$99,$D$21,$D$22,$D$23,$D$24,$D$25,$D$26,$D$27,$D$28,$D$29,$D$30,$D$31,$D$32,$D$33,$D$34,$D$35,$D$36,$D$37,$D$38,$D$39,$D$40,$D$41,$D$42,$D$43,$D$44,$D$45,$D$46,$D$47,$D$48,$D$49,$D$50,$D$51,$D$52,$D$53"
'Remove protection for this programmatic format edit
ActiveSheet.Protect AllowFormattingCells:=True, Password:=passwd
Range(TEST_1a).Font.ColorIndex = 5
Range(TEST_1b).Font.ColorIndex = 5
Range(TEST_1c).Font.ColorIndex = 5
'Reinstate protection after this programmatic format edit has been made.
ActiveSheet.Protect AllowFormattingCells:=False, Password:=passwd
'beyond limit of parameter passed to range - this fails.
Dim TEST2 As String
TEST2 = "$D$13,$D$24,$D$25,$D$36,$D$67,$D$98,$D$20,$D$99,$D$21,$D$22,$D$23,$D$24,$D$25,$D$26,$D$27,$D$28,$D$29,$D$30,$D$31,$D$32,$D$33,$D$34,$D$35,$D$36,$D$37,$D$38,$D$39,$D$40,$D$41,$D$42,$D$43,$D$44,$D$45,$D$46,$D$47,$D$48,$D$49,$D$50,$D$51,$D$52,$D$53,$D$54,$D$55"
'Remove protection for this programmatic format edit
ActiveSheet.Protect AllowFormattingCells:=True, Password:=passwd
Range(TEST_2).Font.ColorIndex = 5
'Reinstate protection after this programmatic format edit has been made.
ActiveSheet.Protect AllowFormattingCells:=False, Password:=passwd
End Sub
Other things to note:
The strings will not be sequences of numbers that could be replaced by ($D$14:$D$55)
When I put a watch on the TEST_2 string the last few characters are shown as:
"...,$D$52,$D$53,$D$5,"
instead of what I would have expected which is:
"...$D$52,$D$53,$D$54,$D$55"
However if I print the string variable to the immediate window it appears as I'd expect it to. I don't know enough about the VB editor to know whether that last point is just a distraction or not, but if it's not related I think it's a strange co-incidence that the point that the Range method fails is the same point that the VBE watch does too.
Note that all of these cells exist and are editable which is shown by TEST_1b and TEST_1a.
All help and suggestions of how to get the Range method working properly would be appreciated
Thanks,
Ken.
Excel 2002 SP3, Windows XP