+ Reply to Thread
Results 1 to 13 of 13

VB Script Sorting and Conditional Formatting?

  1. #1
    Forum Contributor
    Join Date
    07-17-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2013/2016
    Posts
    107

    VB Script Sorting and Conditional Formatting?



    Excel 2003

    Ok so I've been working on this "HUGE" Inventory spreadsheet.

    What I have is a Validation List in C1.

    So I asked the Sheet to check and every time anything is updated in Column 3 to Sort and perform conditional formatting.

    These to functions work when I run together in one Macro. Except the Check Column 3 is not there.

    Anyway, here's the code. I suspect that my Target.Column function is wrong, but I don't know how to fix it.

    Please Login or Register  to view this content.
    Last edited by fasterthanyours; 07-29-2009 at 09:18 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VB Script Sorting and Conditional Formatting?

    This appears to work, I just reformatted a little:
    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
    If 
    Target.Column <> 3 Then Exit Sub
    Dim cel 
    As RangeRng As Range
    Dim LR 
    As Long
    LR 
    Range("A" Rows.Count).End(xlUp).Row
    Set Rng 
    Range("H3:H" LR)

        
    Range("A2:H" LR).Sort Key1:=Range("H2"), Order1:=xlAscendingKey2:=Range("F2"), _
            Order2
    :=xlDescendingKey3:=Range("G2"), Order3:=xlDescendingHeader:=xlGuess_
            OrderCustom
    :=1MatchCase:=FalseOrientation:=xlTopToBottom_
            DataOption1
    :=xlSortNormalDataOption2:=xlSortNormalDataOption3:=xlSortNormal

        
    For Each cel In Rng
            Select 
    Case cel.Value
                
    Case vbNullString:      cel.Interior.ColorIndex xlNone
                
    Case "Out of Stock":    cel.Interior.ColorIndex 6
                
    Case "None on Hand":    cel.Interior.ColorIndex 43
                
    Case Is 0.75:         cel.Interior.ColorIndex 3
                
    Case 0.75 To 1:         cel.Interior.ColorIndex 45
                
    Case Is 1:            cel.Interior.ColorIndex 41
            End Select
        Next cel

    End Sub 
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    07-17-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2013/2016
    Posts
    107

    Re: VB Script Sorting and Conditional Formatting?

    LOL Not for me.

    It doesn't even sort.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VB Script Sorting and Conditional Formatting?

    Here it is in your own sheet. Type anything in column C and the whole thing sorts and colors column H.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    07-17-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2013/2016
    Posts
    107

    Re: VB Script Sorting and Conditional Formatting?

    Dude, The Change it C3 is working now, but the IF formulas aren't LOL

    I think it's my home PC this time lol

  6. #6
    Forum Contributor
    Join Date
    07-17-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2013/2016
    Posts
    107

    Re: VB Script Sorting and Conditional Formatting?

    OMG Now it's working LOL

    I guess I had to turn down the Macro Security at home close and reopen.

    I'll try it again tomorrow at work. Thanks again.

  7. #7
    Forum Contributor
    Join Date
    07-17-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2013/2016
    Posts
    107

    Re: VB Script Sorting and Conditional Formatting?

    Ok one more question.

    Say my boss asks me to do the Conditional Format from A to J.

    How can I make that Script do that?

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VB Script Sorting and Conditional Formatting?

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
    If 
    Target.Column <> 3 Then Exit Sub
    Dim cel 
    As RangeRng As Range
    Dim LR 
    As Long
    LR 
    Range("A" Rows.Count).End(xlUp).Row
    Set Rng 
    Range("H3:H" LR)

        
    Range("A2:H" LR).Sort Key1:=Range("H2"), Order1:=xlAscendingKey2:=Range("F2"), _
            Order2
    :=xlDescendingKey3:=Range("G2"), Order3:=xlDescendingHeader:=xlGuess_
            OrderCustom
    :=1MatchCase:=FalseOrientation:=xlTopToBottom_
            DataOption1
    :=xlSortNormalDataOption2:=xlSortNormalDataOption3:=xlSortNormal

        
    For Each cel In Rng
            Select 
    Case cel.Value
                
    Case vbNullString
                    Range
    (Cells(cel.Row"A"), Cells(cel.Row"J")).Interior.ColorIndex xlNone
                
    Case "Out of Stock"
                    
    Range(Cells(cel.Row"A"), Cells(cel.Row"J")).Interior.ColorIndex 6
                
    Case "None on Hand"
                    
    Range(Cells(cel.Row"A"), Cells(cel.Row"J")).Interior.ColorIndex 43
                
    Case Is 0.75
                    Range
    (Cells(cel.Row"A"), Cells(cel.Row"J")).Interior.ColorIndex 3
                
    Case 0.75 To 1
                    Range
    (Cells(cel.Row"A"), Cells(cel.Row"J")).Interior.ColorIndex 45
                
    Case Is 1
                    Range
    (Cells(cel.Row"A"), Cells(cel.Row"J")).Interior.ColorIndex 41
            End Select
        Next cel

    End Sub 
    ==========
    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)

  9. #9
    Forum Contributor
    Join Date
    07-17-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2013/2016
    Posts
    107

    Re: VB Script Sorting and Conditional Formatting?

    Thanks that turned out to look like crap.

    LOL Not your fault, my colors sucked
    Last edited by fasterthanyours; 07-29-2009 at 09:20 PM.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VB Script Sorting and Conditional Formatting?

    Yeah, I do stuff like that occasionally and I always use the soft pastel colors, very pleasant to read.

    Cheers.

  11. #11
    Forum Contributor
    Join Date
    07-17-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2013/2016
    Posts
    107

    Re: VB Script Sorting and Conditional Formatting?

    Yup, thought about that too, to lazy to work out the color scheme.

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VB Script Sorting and Conditional Formatting?

    Ah, but why "work it out" when you can just look them up!?

    http://www.mvps.org/dmcritchie/excel/colors.htm

    My favorites are 17, 19, 20, 22, 33-40.

  13. #13
    Forum Contributor
    Join Date
    07-17-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2013/2016
    Posts
    107

    Re: VB Script Sorting and Conditional Formatting?

    Actually that's the site I got my codes from :D

    Still to lazy

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1