+ 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.

  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,726

    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:
    Please Login or Register  to view this content.
    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,726

    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,726

    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.

    Please Login or Register  to view this content.
    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,726

    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,726

    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.
    Please Login or Register  to view this content.
    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,726

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

    Try:
    Please Login or Register  to view this content.
    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

    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.

    Please Login or Register  to view this content.

  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

    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!!!!

  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,726

    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,726

    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:
    Please Login or Register  to view this content.

  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,726

    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:
    Please Login or Register  to view this content.

  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.

    Please Login or Register  to view this content.

+ 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