PC Review


Reply
Thread Tools Rate Thread

Code snippet works but is a bit 'clumbsy'

 
 
Howard
Guest
Posts: n/a
 
      3rd Sep 2012

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
 
Reply With Quote
 
 
 
 
GS
Guest
Posts: n/a
 
      3rd Sep 2012
"'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


 
Reply With Quote
 
 
 
 
Don Guillett
Guest
Posts: n/a
 
      3rd Sep 2012
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
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      3rd Sep 2012
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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Help-Intellisense (Code Snippet) Tab Tab does not insert code (VS2008) Jeff Johnson Microsoft C# .NET 0 12th Jul 2010 10:37 PM
a .dll works on 32-bit machine but not 64-bit machine erxuan Microsoft C# .NET 4 21st Nov 2006 06:29 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd AMD 64 Bit 0 17th Sep 2004 09:07 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Processors 0 17th Sep 2004 09:01 PM
Code Snippet - Solution Suggestions Gav !! Microsoft Excel Programming 0 29th Oct 2003 01:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:36 PM.