Code snippet works but is a bit 'clumbsy'

  • Thread starter Thread starter Howard
  • Start date Start date
H

Howard

Excel 2010.

This snippet of code is from a much larger piece of code.
Range( C1:C26) are the letters of the alphabet.
When I run this snippet , CName asks for a letter. When a letter is entered it is assigned to Range("G1") and used to do stuff within the rest of the code, and that works very well.

I want this snippet to look at Range("C1:C26"), find that G1 value and shade the cell one column to the right, in Column D. It does that okay but the rub comes whereas it also shades the entire Range("C1:C26").

So I have added a simple procedure to clear the shading of Range("C1:C26") just below the line "CName = vbNullString".

'gotta be a better way to get all this done without doing so many SELECTIONS.

Option Explicit
Option Compare Text

Sub TestAtoZ()
Dim i As Integer
Dim c As Range
Dim CName As String

CName = InputBox(" Enter a duplicated letter from the" _
& vbCr & " last Capital name in column M." _
& vbCr & " If there is no duplicate in the" _
& vbCr & " Capital name, enter the first letter" _
& vbCr & " of the Capital name, B for Boise.", "State Letter")
Range("G1") = CName
Range("B1:B26").Select 'Values are A to Z & each appear only once
For Each c In Selection
If c.Value = Range("G1").Value Then c.Offset(0, 1).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Next
CName = vbNullString
Range("B1:B26").Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("G2").Select
End Sub

Thanks for any help.
Regards,Howard
 
"'gotta be a better way to get all this done without doing so many
SELECTIONS"

I don't see in your code why you have to 'select' anything when the
actions your code takes can be done directly on the 'range' it acts on.

Example:

Instead of...
Range("B1:B26").Select
For each c in Selection...

..you could use...
For Each c in Range("B1:B26")...

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Excel 2010.



This snippet of code is from a much larger piece of code.

Range( C1:C26) are the letters of the alphabet.

When I run this snippet , CName asks for a letter. When a letter is entered it is assigned to Range("G1") and used to do stuff within the rest of the code, and that works very well.



I want this snippet to look at Range("C1:C26"), find that G1 value and shade the cell one column to the right, in Column D. It does that okay but the rub comes whereas it also shades the entire Range("C1:C26").



So I have added a simple procedure to clear the shading of Range("C1:C26") just below the line "CName = vbNullString".



'gotta be a better way to get all this done without doing so many SELECTIONS.



Option Explicit

Option Compare Text



Sub TestAtoZ()

Dim i As Integer

Dim c As Range

Dim CName As String



CName = InputBox(" Enter a duplicated letter from the" _

& vbCr & " last Capital name in column M." _

& vbCr & " If there is no duplicate in the" _

& vbCr & " Capital name, enter the first letter" _

& vbCr & " of the Capital name, B for Boise.", "State Letter")

Range("G1") = CName

Range("B1:B26").Select 'Values are A to Z & each appear only once

For Each c In Selection

If c.Value = Range("G1").Value Then c.Offset(0, 1).Select

With Selection.Interior

.Pattern = xlSolid

.PatternColorIndex = xlAutomatic

.ThemeColor = xlThemeColorLight2

.TintAndShade = 0.799981688894314

.PatternTintAndShade = 0

End With

Next

CName = vbNullString

Range("B1:B26").Select

With Selection.Interior

.Pattern = xlNone

.TintAndShade = 0

.PatternTintAndShade = 0

End With

Range("G2").Select

End Sub



Thanks for any help.

Regards,Howard

try this changing the color index number to suit

Sub TestAtoZ_SAS()
Dim c As Range
Dim CName As String

CName = InputBox(" Enter a duplicated letter from the" _
& vbCr & " last Capital name in column M." _
& vbCr & " If there is no duplicate in the" _
& vbCr & " Capital name, enter the first letter" _
& vbCr & " of the Capital name, B for Boise.", "State Letter")
For Each c In Range("B1:B26")
If c.Value = CName Then c.Offset(, 1).Interior.ColorIndex = 37
Next
'Range("B1:B26").Interior.ColorIndex = 0' don't need
End Sub
 
try this changing the color index number to suit



Sub TestAtoZ_SAS()

Dim c As Range

Dim CName As String



CName = InputBox(" Enter a duplicated letter from the" _

& vbCr & " last Capital name in column M." _

& vbCr & " If there is no duplicate in the" _

& vbCr & " Capital name, enter the first letter" _

& vbCr & " of the Capital name, B for Boise.", "State Letter")

For Each c In Range("B1:B26")

If c.Value = CName Then c.Offset(, 1).Interior.ColorIndex = 37

Next

'Range("B1:B26").Interior.ColorIndex = 0' don't need

End Sub

Thanks Don,

I copied this to the proper spot in the full code procedure and it works FINE.

For Each c In Range("B1:B26")
If c.Value = CName Then c.Offset(, 1).Interior.ColorIndex = 37
Next

....and thanks to GS also.

Regards,
Howard
 
Back
Top