Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Code snippet works but is a bit 'clumbsy'


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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Code snippet works but is a bit 'clumbsy'

"'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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,522
Default Code snippet works but is a bit 'clumbsy'

On Monday, September 3, 2012 12:34:02 AM UTC-5, Howard wrote:
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
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Code snippet works but is a bit 'clumbsy'

On Monday, September 3, 2012 9:54:01 AM UTC-7, Don Guillett wrote:
On Monday, September 3, 2012 12:34:02 AM UTC-5, Howard wrote:

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


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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code Snippet - Activate VBE window Tim Childs[_5_] Excel Programming 4 November 10th 10 08:13 PM
Want to modify this VBA code snippet joel Excel Programming 1 April 8th 09 08:46 PM
Want to modify this VBA code snippet Sam Commar Excel Programming 1 April 5th 09 01:50 PM
Problem in WITH/END WITH code snippet [email protected] Excel Programming 2 January 21st 06 02:59 PM
Code snippet storage Ken McLennan[_3_] Excel Programming 9 August 20th 04 03:25 PM


All times are GMT +1. The time now is 11:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"