+ Reply to Thread
Results 1 to 11 of 11

Help Understanding Some Code

  1. #1
    Registered User
    Join Date
    07-10-2019
    Location
    New York, NY
    MS-Off Ver
    MS Office 2016
    Posts
    27

    Help Understanding Some Code

    I got some help with a code the other day and I'm trying to understand how it works. I started making some notes for myself but got confused after a certain point. Could someone please try to explain in layman's terms what the following code does?
    Please Login or Register  to view this content.
    I've also attached the example workbook so you can see the context.

    Thanks very much!
    Attached Files Attached Files

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Help Understanding Some Code

    Why don't you just as the coder Marc L to comment the code for you...Provided here...
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Help Understanding Some Code

    I started explaining - see below but an confused by what do you expect the outcome to be? As far as I can see that Inner With..End With block doesn't change anything? AT least provided the macro is run with the Summary sheet being the active sheet. If you run it with TopSheet1 being active then the sheet gets screwed up,


    'Used Range.Offset(1) is commented out. UsedRange operates on whichever sheet is active or being referenced in code and is the whole of the data from A1 to the cell in
    'the last row and last column. The Offset(1) simply changes that range from A1:?? to B1:?? +1. Usually done to avoid deleting the first row of column labels

    'R& is a variable. It's good practice to specify the type of variable by including some reference in the variable name. The & is just a shortcut and means a Long variable
    'get into the habit of declaring stuff like Dim stMyName As String, lRow As Long,

    'The For Loop uses the variable S& as counter and loops from 2 to the number of sheets in the workbook, i.e. the .Count bit. Be careful though using this Sheet index since it refers
    'to the order of the Tab names. If someone were to move a sheet then the macro would be compromised. I think it's better to loop through all sheets and onclude an IF test to exclude
    'sheets that don't meet a particular criteria. That could be the first 3 letters of the sheet name, i.e. "Top", or some other common value in each sheet.

    'The Range Rf is set to be the cell in column B (the 2nd column) of the TopSHeet where the first characters of the cell start with "Name". i.e. B37

    'The .Range("B9".... line does an {End}{Down} on B8 and finds B17. The Range B9:B17 is then resized. The comma in the Resize(,C) keeps the number of rows the
    'same i.e. 9:17 but extends the columns by the value of the C constant of 12. The range is then B9:M17

    'with the BM9:BM17 ....then what???????????? see opening remarks
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    07-10-2019
    Location
    New York, NY
    MS-Off Ver
    MS Office 2016
    Posts
    27

    Re: Help Understanding Some Code

    Thanks for your help, Richard Buttrey. Apologies, I didn't realize the code was still located in the module. I thought I had corrected that to only live on the Summary sheet to avoid exactly what you mentioned.

    As for what I'm actually trying to do, I have a bunch of these timesheet summaries pages from supervisors (referred to as the Top Sheet). I have combined all the top sheets into one workbook and then am using the Summary sheet to extract the relevant data from all the other sheets so I can sum and sort and more easily reconcile with the actual payroll data. I'm working with past data, so I cannot change the format of the top sheets, otherwise I'd use consolidate or power query or something like that.

    Does that make sense?

    So what I'm trying to understand now is once the info is found on the top sheet, how does it make it onto the summary sheet? Also, when it moves onto the next sheet, how does it know not to overwrite the data it just extracted from the previous top sheet?

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Help Understanding Some Code


    Quote Originally Posted by Richard Buttrey View Post
    Used Range.Offset(1) is commented out. UsedRange operates on whichever sheet is active
    Hi Richard,

    as you can see in the original thread (link in Sintek's post …)
    this VBA procedure must be located in a specific worksheet module
    so any range without any worksheet reference refers to this specific worksheet like UsedRange
    whatever the active sheet !

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Help Understanding Some Code


    Quote Originally Posted by AAnderson4912 View Post
    how does it know not to overwrite the data it just extracted from the previous top sheet?
    Just according to the row index variable R …

  7. #7
    Registered User
    Join Date
    07-10-2019
    Location
    New York, NY
    MS-Off Ver
    MS Office 2016
    Posts
    27

    Re: Help Understanding Some Code

    I have to admit I'm still not fully understanding. Maybe it would help me to compare with a similar situation. I have another workbook of timesheets for individuals and like the previous example, I want to extract the following data from each timesheet:
    • Employee Name
    • Period Beginning
    • Period Ending
    • Pay Date
    • Regular Hrs
    • OT Hrs
    • PTO Hrs
    • Vacation Hrs
    • Other Hrs
    • Total Hrs

    And then copy that to the Summary page. How would you approach this example? (See attachment)
    Attached Files Attached Files

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Help Understanding Some Code

    Do the timesheets contain ONLY NEW Data that must be added to the summary?
    Does the stuff already on the Summary sheet need to be kept.

    Sometimes it's simpler just to zap a summary and refresh it from scratch. If you can clarify this aspect then it should be asimple task to copy everything on the timesheets and recreate the summary.

  9. #9
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Help Understanding Some Code

    Easy enough to understand...Data set no to big to justify storing all in Array to pass at end...
    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    Quote Originally Posted by AAnderson4912 View Post
    How would you approach this example? (See attachment)
    Exactly the same way like in your previous thread : logically obviously with a row index variable …

    According to your post #7 attachment you can delete the useless VBA Module1
    then paste this new VBA demonstration to the Sheet1 (Summary) worksheet module :

    PHP Code: 
    Sub Demo2()
            
    Dim R&
            
    UsedRange.Offset(1).Clear
        With Worksheets
            With Rows
    ("2:" & .Count).Columns
                
    .Item("B:D").NumberFormat "m/d/yyyy"
                
    .Item("E:J").NumberFormat "0.00"
            
    End With
            
    For 2 To .Count
                With 
    .Item(R)
                    
    Cells(R1).Value2 = .[C2].Value2
                    Rows
    (R).Columns("B:D").Value2 = .[TRANSPOSE(J2:J4)]
                    
    Rows(R).Columns("E:J").Value2 = .[TRANSPOSE(J32:J37)]
                
    End With
            Next
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 04-08-2022 at 10:29 AM.

  11. #11
    Registered User
    Join Date
    07-10-2019
    Location
    New York, NY
    MS-Off Ver
    MS Office 2016
    Posts
    27

    Re: Help Understanding Some Code

    Thank you all for your help! I think especially seeing the same problem approached in 2 different ways has been very informative

+ 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. Understanding VBA Code
    By Moa88 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2019, 08:49 AM
  2. Understanding this code
    By vj2651 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-13-2018, 11:12 PM
  3. [SOLVED] Understanding code
    By K243 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-09-2018, 04:19 AM
  4. Help with understanding VBA code
    By snaik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2014, 04:22 PM
  5. need help understanding a bit of code
    By lazerphazer21 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-24-2013, 10:11 PM
  6. Understanding this VBA code
    By rez9000 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-28-2010, 10:14 AM
  7. understanding code
    By karinos57 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-12-2009, 08:35 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