+ Reply to Thread
Results 1 to 21 of 21

Need help with VBA code. Tons of code seperated in two, second part of code doesn't work.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Need help with VBA code. Tons of code seperated in two, second part of code doesn't work.

    I had to break up the 2 sections of code because it was to long.
    The left column on a sheet will run the first part of the code.
    And the right column on that same sheet will run the second set.
    I'm not sure what the second part should be at the very beginning.

    You'll notice a line across the whole document where the second part of the code starts.
    I've tried changing the name of the second part of the code, but it doesn't work.
    If I delete the first part of the code, then the second one works fine.
    Attached Files Attached Files
    Last edited by FragaGeddon; 11-28-2015 at 09:10 PM.

  2. #2
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    I found the solution from a very old thread on mr excel forum.

    If you have very long code in VBA, you will need to separate it.
    Your first part of the code you will have it start with something like: Private Sub Worksheet_Change_A(ByVal Target As Range)
    Then separate the second part of the code with: Private Sub Worksheet_Change_B(ByVal Target As Range)

    Then at the very top add:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheet_Change_A Target
    Worksheet_Change_B Target
    End Sub

    Don't forget to add an End Sub after your first part of your code.

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    It seems to me your first 750 lines could be reduced to:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Rw As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
            With ws
                .Unprotect (sPW)
                .Range(Rw & ":" & Rw & "," & Rw + 41 & ":" & Rw + 41 & "," & _
                  Rw + 82 & ":" & Rw + 82 & "," & Rw + 123 & ":" & Rw + 123).EntireRow.Hidden = blHide
                .Protect (sPW)
            End With
        Next
        With Sheets("Extra Week")
            .Unprotect (sPW)
            .Range(Rw & ":" & Rw).EntireRow.Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("Cosmetician Sales")
            .Unprotect (sPW)
            .Range(Rw & ":" & Rw & "," & Rw + 31 & ":" & Rw + 31).EntireRow.Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("CAST")
            .Unprotect (sPW)
            .Range((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19).EntireRow.Hidden = blHide
            .Protect (sPW)
        End With
    End If
    Application.ScreenUpdating = True
    End Sub
    I'll leave it to you to simplify the rest of your code and incorporate it into the above sub.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    I'm not sure what all that code means (.Range(Rw & ":" & Rw & "," & Rw + 41 & ":" & Rw + 41 & "," & _
    Rw + 82 & ":" & Rw + 82 & "," & Rw + 123 & ":" & Rw + 123)), but looks very interesting, and I guess saves a ton of editing.

    Thanks, and I'll look at it soon.

    O.K. I think I figured it out.
    So basically if cell E4 is empty, then look at row 4, then the 41'st row after that, etc.
    And I guess columns would be something like:

    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Then
    blHide = (Target.Value = ""): Cw = Target.Column
    For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
    With ws
    .Unprotect (sPW)
    .Range(Cw & ":" & Cw & "," & Cw + 41 & ":" & Cw + 41 & "," & _
    Cw + 82 & ":" & Cw + 82 & "," & Cw + 123 & ":" & Cw + 123).EntireColumn.Hidden = blHide
    .Protect (sPW)
    End With
    Last edited by FragaGeddon; 11-21-2015 at 02:56 AM.

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Quote Originally Posted by FragaGeddon View Post
    I'm not sure what all that code means
    Have a look at what Rw is and how it is derived. The rest is just math & string compilation.

  6. #6
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Quote Originally Posted by macropod View Post
    Have a look at what Rw is and how it is derived. The rest is just math & string compilation.
    Sorry I just edited my post above. Thanks again for your help. Much Appreciated.

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Quote Originally Posted by FragaGeddon View Post
    And I guess columns would be something like:

    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Then
    blHide = (Target.Value = ""): Cw = Target.Column
    For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
    With ws
    .Unprotect (sPW)
    .Range(Cw & ":" & Cw & "," & Cw + 41 & ":" & Cw + 41 & "," & _
    Cw + 82 & ":" & Cw + 82 & "," & Cw + 123 & ":" & Cw + 123).EntireColumn.Hidden = blHide
    .Protect (sPW)
    End With
    Correct, though you should also define Cw As Long (Since Rw is an abbreviation of Row, I wouldn't abbreviate Column as Cw).

    PS: Please use the code tags, inserted via the # symbol on the posting menu, when posting code.

  8. #8
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Not sure if I'm doing something wrong, but I did the second part of the code to hide columns as a test. If I delete a cell from I4, it hides all columns in that sheet.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheet_Change_B Target
    Worksheet_Change_V Target
    End Sub
    Private Sub Worksheet_Change_B(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Rw As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
            With ws
                .Unprotect (sPW)
                .Range(Rw & ":" & Rw & "," & Rw + 41 & ":" & Rw + 41 & "," & _
                  Rw + 82 & ":" & Rw + 82 & "," & Rw + 123 & ":" & Rw + 123).EntireRow.Hidden = blHide
                .Protect (sPW)
                .EnableSelection = xlUnlockedCells
            End With
        Next
        With Sheets("Extra Week")
            .Unprotect (sPW)
            .Range(Rw & ":" & Rw).EntireRow.Hidden = blHide
            .Protect (sPW)
            .EnableSelection = xlUnlockedCells
        End With
        With Sheets("Cosmetician Sales")
            .Unprotect (sPW)
            .Range(Rw & ":" & Rw & "," & Rw + 31 & ":" & Rw + 31).EntireRow.Hidden = blHide
            .Protect (sPW)
            .EnableSelection = xlUnlockedCells
        End With
        With Sheets("CAST")
            .Unprotect (sPW)
            .Range((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19).EntireRow.Hidden = blHide
            .Protect (sPW)
            .EnableSelection = xlUnlockedCells
        End With
    End If
    Application.ScreenUpdating = True
    End Sub
    Private Sub Worksheet_Change_V(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Cl As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("I4:I33")) Is Nothing Then
        blHide = (Target.Value = ""): Cl = Target.Column
        For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
            With ws
                .Unprotect (sPW)
                .Range(Cl & ":" & Cl).EntireColumn.Hidden = blHide
                .Protect (sPW)
                .EnableSelection = xlUnlockedCells
            End With
        Next
        With Sheets("Extra Week")
            .Unprotect (sPW)
            .Range(Cl & ":" & Cl).EntireColumn.Hidden = blHide
            .Protect (sPW)
            .EnableSelection = xlUnlockedCells
        End With
    End If
    Application.ScreenUpdating = True
    End Sub
    Last edited by FragaGeddon; 11-23-2015 at 12:51 AM.

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Quote Originally Posted by FragaGeddon View Post
    Not sure if I'm doing something wrong, but I did the second part of the code to hide columns as a test. If I delete a cell from I4, it hides all columns in that sheet.
    What are you trying to hide/unhide? Yoy have defined 'Cl = Target.Column', but that just returns a number thatou need to convert that to a letter. But, if you do that and reference Cl for 'Hidden = blHide', what you'll end up hiding is column I. Is that your intention?

    Is it only I4 you're concerned with for the column hiding/unhiding? Your code presently tests I4:I33, so any of those cells will trigger that.

  10. #10
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    It will basically do that same thing for the rows, but it will hide columns and rows instead. It would be for a sheet called "Daily Sales Tracking".
    So if I4 is blank, then hide columns J & K and rows 382 & 383. I5 would hide columns P & Q and rows 384 & 385, etc.

  11. #11
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    So why is your added code trying to hide columns on sheets "P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13" & "Extra Week", but not on the sheet "Daily Sales Tracking"?

  12. #12
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Sorry I was just testing to see if it worked. For the "Daily Sales Tracking" sheet, this is what needs to be done:

    On the "Input Info" sheet, cells I4 to I33 it would have a name or have nothing, basically the same as E4 to E33.
    Except I need it to hide 2 rows & 2 columns at the same time on the "Daily Tracking Sheet". There's already 4 columns hidden between each of the 2 columns that need to be displayed, or hidden.
     
     I4 = Column J:K & Row 382:383
     I5 = Column P:Q & Row 384:385
     I6 = Column V:W & Row 386:387
     I7 = Column AB:AC & Row 388:389
     I8 = Column AH:AI & Row 390:391
     I9 = Column AN:AO & Row 392:393
    I10 = Column AT:BU & Row 394:395
    I11 = Column AZ:BA & Row 396:397
    I12 = Column BF:BG & Row 398:399
    I13 = Column BL:BM & Row 400:401
    
    I14 = Column BR:BS & Row 402:403
    I15 = Column BX:BY & Row 404:405
    I16 = Column CD:CE & Row 406:407
    I17 = Column CJ:CK & Row 408:409
    I18 = Column CP:CQ & Row 410:411
    I19 = Column CV:CW & Row 412:413
    I20 = Column DB:DC & Row 414:415
    I21 = Column DH:DI & Row 416:417
    I22 = Column DN:DO & Row 418:419
    I23 = Column DT:DU & Row 420:421
    
    I24 = Column DZ:DA & Row 422:423
    I25 = Column EF:EG & Row 424:425
    I26 = Column EL:EM & Row 426:427
    I27 = Column ER:ES & Row 428:429
    I28 = Column EX:EY & Row 430:431
    I29 = Column FD:FE & Row 432:433
    I30 = Column FJ:FK & Row 434:435
    I31 = Column FP:FQ & Row 436:437
    I32 = Column FV:FW & Row 438:439
    I33 = Column GB:GC & Row 440:441
    Last edited by FragaGeddon; 11-27-2015 at 03:15 AM.

  13. #13
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Try:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Rw As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
            With ws
                .Unprotect (sPW)
                .Rows(Rw).Hidden = blHide
                .Rows(Rw + 41).Hidden = blHide
                .Rows(Rw + 82).Hidden = blHide
                .Rows(Rw + 123).Hidden = blHide
                .Protect (sPW)
            End With
        Next
        With Sheets("Extra Week")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("Cosmetician Sales")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Rows(Rw + 31).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("CAST")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("Daily Sales Tracking")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 2 + 382 & ":" & (Rw - 4) * 2 + 383).Hidden = blHide
            .Columns((Rw - 3) * 6 + 4).Hidden = blHide
            .Columns((Rw - 3) * 6 + 5).Hidden = blHide
            .Protect (sPW)
        End With
    End If
    Application.ScreenUpdating = True
    End Sub
    Note that I've revised some of the earlier code to make it easier for people to understand and, hence, maintain.

  14. #14
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Quote Originally Posted by macropod View Post
    Try:

    Note that I've revised some of the earlier code to make it easier for people to understand and, hence, maintain.
    Yes that is a lot more better. Great Job!!!!

  15. #15
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    That's pretty awesome. And I will be able to use this on another workbook. I did have to fix it up a bit.
    The only thing I get confused about is when you do for the "CAST" sheet: .Rows((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19)
    I'm guessing your saying start at row 0 (Rw - 4), then hide the next 19 rows, then add 1 blank row(?), and the second part would start at row 1 and hide the next 19 rows?
    That's where I'm at a bit of a loss.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheet_Change_BE Target
    Worksheet_Change_VS Target
    End Sub
    
    Private Sub Worksheet_Change_BE(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Rw As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
            With ws
                .Unprotect (sPW)
                .Rows(Rw).Hidden = blHide
                .Rows(Rw + 41).Hidden = blHide
                .Rows(Rw + 82).Hidden = blHide
                .Rows(Rw + 123).Hidden = blHide
                .Protect (sPW)
            End With
        Next
        With Sheets("Extra Week")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("Cosmetician Sales")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Rows(Rw + 31).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("CAST")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19).Hidden = blHide
            .Protect (sPW)
        End With
    End If
    Application.ScreenUpdating = True
    End Sub
    
    Private Sub Worksheet_Change_VS(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Rw As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("I4:I33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        With Sheets("Daily Sales Tracking")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 2 + 382 & ":" & (Rw - 4) * 2 + 383).Hidden = blHide
            .Columns((Rw - 3) * 6 + 4).Hidden = blHide
            .Columns((Rw - 3) * 6 + 5).Hidden = blHide
            .Protect (sPW)
            End With
        With Sheets("Vendor Sales")
                .Unprotect (sPW)
                .Rows(Rw).Hidden = blHide
                .Rows(Rw + 34).Hidden = blHide
                .Rows(Rw + 68).Hidden = blHide
                .Protect (sPW)
        End With
    End If
    Application.ScreenUpdating = True
    End Sub

  16. #16
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Quote Originally Posted by FragaGeddon View Post
    The only thing I get confused about is when you do for the "CAST" sheet: .Rows((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19)
    I'm guessing your saying start at row 0 (Rw - 4), then hide the next 19 rows, then add 1 blank row(?), and the second part would start at row 1 and hide the next 19 rows?
    If Rw = 4 then (Rw - 4) = 0. 0 *19 = 0 and 0 +1 = 1. Similarly Rw = 4 then (Rw - 3) = 1. 1 *19 = 1. 1 & ":" & 19 = 1:19. So .Rows(1:19).Hidden = blHide
    If Rw = 5 then (Rw - 4) = 1. 1 *19 = 1 and 19 +1 = 20. Similarly Rw = 5 then (Rw - 3) = 2. 2 *19 = 38. 20 & ":" & 38 = 20:38. So .Rows(20:38).Hidden = blHide
    etc.
    Rows are never added to the workbook.

  17. #17
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Awesome. I totally understand now. Thanks again for all your help.

    Edit: I kind of thought it was a math equation. I was thinking why would you add + 1 to zero, but it really makes sense when you get to the next box in I5, and so on.
    Last edited by FragaGeddon; 11-28-2015 at 09:18 PM.

  18. #18
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Since the outcomes for columns E & I are the same for all rows, and the only column-based difference concerns updating the "Daily Sales Tracking" worksheet if column I is being updated, your entire 1356 lines of code could be reduced to just 45 lines:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Rw As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Or Not Intersect(Target, ActiveSheet.Range("I4:I33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
            With ws
                .Unprotect (sPW)
                .Rows(Rw).Hidden = blHide
                .Rows(Rw + 41).Hidden = blHide
                .Rows(Rw + 82).Hidden = blHide
                .Rows(Rw + 123).Hidden = blHide
                .Protect (sPW)
            End With
        Next
        With Sheets("Extra Week")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("Cosmetician Sales")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Rows(Rw + 31).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("CAST")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19).Hidden = blHide
            .Protect (sPW)
        End With
    End If
    If Not Intersect(Target, ActiveSheet.Range("I4:I33")) Is Nothing Then
        With Sheets("Daily Sales Tracking")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 2 + 382 & ":" & (Rw - 4) * 2 + 383).Hidden = blHide
            .Columns((Rw - 3) * 6 + 4).Hidden = blHide
            .Columns((Rw - 3) * 6 + 5).Hidden = blHide
            .Protect (sPW)
        End With
    End If
    Application.ScreenUpdating = True
    End Sub

  19. #19
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Quote Originally Posted by macropod View Post
    Since the outcomes for columns E & I are the same for all rows, and the only column-based difference concerns updating the "Daily Sales Tracking" worksheet if column I is being updated, your entire 1356 lines of code could be reduced to just 45 lines:
    Actually column E would be something like Sales Team A. And that would target sheets P1 to P13, Extra Week, CAST & Cosmetician Sales.
    Where column I would be Sales Team B, and would target sheets Daily Sales Tracking & Vendor Sales.
    So column I shouldn't hide/show rows on the Sales Team A's sheets.

  20. #20
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    OK, so with 52 lines of code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Rw As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
            With ws
                .Unprotect (sPW)
                .Rows(Rw).Hidden = blHide
                .Rows(Rw + 41).Hidden = blHide
                .Rows(Rw + 82).Hidden = blHide
                .Rows(Rw + 123).Hidden = blHide
                .Protect (sPW)
            End With
        Next
        With Sheets("Extra Week")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("Cosmetician Sales")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Rows(Rw + 31).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("CAST")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19).Hidden = blHide
            .Protect (sPW)
        End With
    End If
    If Not Intersect(Target, ActiveSheet.Range("I4:I33")) Is Nothing Then
        With Sheets("Vendor Sales")
                .Unprotect (sPW)
                .Rows(Rw).Hidden = blHide
                .Rows(Rw + 34).Hidden = blHide
                .Rows(Rw + 68).Hidden = blHide
                .Protect (sPW)
        End With
        With Sheets("Daily Sales Tracking")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 2 + 382 & ":" & (Rw - 4) * 2 + 383).Hidden = blHide
            .Columns((Rw - 3) * 6 + 4).Hidden = blHide
            .Columns((Rw - 3) * 6 + 5).Hidden = blHide
            .Protect (sPW)
        End With
    End If
    Application.ScreenUpdating = True
    End Sub

  21. #21
    Registered User
    Join Date
    12-30-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Google Sheets
    Posts
    43

    Re: Need help with VBA code. Tons of code seperated in two, second part of code doesn't wo

    Actually 53 lines of code. Just missed the blHide = (Target.Value = ""): Rw = Target.Row in the second part.

    Thanks again for all your help. I really appreciate it. And hopefully this will help others.
    I wish I knew how to do this earlier, because it took my like 1.5 hours to sit there and edit my code.
    Now if I ever wanted to add 10 more lines, I just have to change the 33 value to 43, pretty awesome.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet, blHide As Boolean, Rw As Long
    Const sPW As String = "pass1234"
    Application.ScreenUpdating = False
        '==================== Beauty Expert ====================
    If Not Intersect(Target, ActiveSheet.Range("E4:E33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        For Each ws In Sheets(Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12", "P13"))
            With ws
                .Unprotect (sPW)
                .Rows(Rw).Hidden = blHide
                .Rows(Rw + 41).Hidden = blHide
                .Rows(Rw + 82).Hidden = blHide
                .Rows(Rw + 123).Hidden = blHide
                .Protect (sPW)
            End With
        Next
        With Sheets("Extra Week")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("Cosmetician Sales")
            .Unprotect (sPW)
            .Rows(Rw).Hidden = blHide
            .Rows(Rw + 31).Hidden = blHide
            .Protect (sPW)
        End With
        With Sheets("CAST")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 19 + 1 & ":" & (Rw - 3) * 19).Hidden = blHide
            .Protect (sPW)
        End With
    End If
    If Not Intersect(Target, ActiveSheet.Range("I4:I33")) Is Nothing Then
        blHide = (Target.Value = ""): Rw = Target.Row
        With Sheets("Vendor Sales")
                .Unprotect (sPW)
                .Rows(Rw).Hidden = blHide
                .Rows(Rw + 34).Hidden = blHide
                .Rows(Rw + 68).Hidden = blHide
                .Protect (sPW)
        End With
        With Sheets("Daily Sales Tracking")
            .Unprotect (sPW)
            .Rows((Rw - 4) * 2 + 382 & ":" & (Rw - 4) * 2 + 383).Hidden = blHide
            .Columns((Rw - 3) * 6 + 4).Hidden = blHide
            .Columns((Rw - 3) * 6 + 5).Hidden = blHide
            .Protect (sPW)
        End With
    End If
    Application.ScreenUpdating = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Tool or code to copy VBA code to OneNote or Word while preserving VBA formatting
    By gregersdk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-05-2015, 04:07 PM
  2. [SOLVED] Pattern Building VBA Code - Working code, would like to use cleaner code
    By Benisato in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-10-2015, 03:05 PM
  3. [SOLVED] Excel VB code. Message pops up while code running asking question. Code must not wait.
    By Heinrich Venter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-28-2014, 06:10 AM
  4. [SOLVED] VBA code for assigning a numeric code to text; then numeric code populates table
    By cteaster in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2014, 08:01 PM
  5. Replies: 2
    Last Post: 03-09-2013, 04:30 AM
  6. Replies: 2
    Last Post: 03-17-2011, 08:55 PM

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