Code snippet works but is a bit 'clumbsy'

Discussion in 'Microsoft Excel Programming' started by Howard, Sep 3, 2012.

  1. Howard

    Howard Guest

    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
     
    Howard, Sep 3, 2012
    #1
    1. Advertisements

  2. Howard

    GS Guest

    "'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
     
    GS, Sep 3, 2012
    #2
    1. Advertisements

  3. Howard

    Don Guillett Guest

    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
     
    Don Guillett, Sep 3, 2012
    #3
  4. Howard

    Howard Guest

    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
     
    Howard, Sep 3, 2012
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gav !!

    Code Snippet - Solution Suggestions

    Gav !!, Oct 29, 2003, in forum: Microsoft Excel Programming
    Replies:
    0
    Views:
    178
    Gav !!
    Oct 29, 2003
  2. Ravee Srinivasan

    MaxRows 65536 in a sheet - A Code Snippet

    Ravee Srinivasan, Nov 10, 2003, in forum: Microsoft Excel Programming
    Replies:
    0
    Views:
    237
    Ravee Srinivasan
    Nov 10, 2003
  3. Ken McLennan

    Code snippet storage

    Ken McLennan, Aug 16, 2004, in forum: Microsoft Excel Programming
    Replies:
    9
    Views:
    277
    Ken McLennan
    Aug 20, 2004
  4. Problem in WITH/END WITH code snippet

    , Jan 21, 2006, in forum: Microsoft Excel Programming
    Replies:
    2
    Views:
    133
    Guest
    Jan 21, 2006
  5. Solutions Manager

    code snippet below. Is there a shorter way to do this type of thin

    Solutions Manager, Jan 14, 2009, in forum: Microsoft Excel Programming
    Replies:
    6
    Views:
    183
    JE McGimpsey
    Jan 14, 2009
Loading...

Share This Page