+ Reply to Thread
Results 1 to 4 of 4

Understanding code

  1. #1
    Registered User
    Join Date
    12-18-2017
    Location
    Durban, South Africa
    MS-Off Ver
    365
    Posts
    8

    Understanding code

    Hi All

    Please can someone help me understand the following code.

    I needed to collate all sheets onto one sheet to pivot data from it. I found this code online and it works perfectly however I don't understand why it does and hope someone here can explain it in very basic and simple terms to someone who knows very little about coding:

    In the below code:
    Line 1 - where does "J" come from, what is that? Why is it J?
    Line 2 - what does "On error Resume Next" mean?

    I do follow lines 3 - 5

    Line 6 - I have no sheet called "Sheets(2)" so what is this doing?

    Line 8 - same question there is nothing called "Sheets(1)" so how does this reference work?

    Line 9 - hopefully will be answered above but what is "J". Why equal "2"?

    Please Login or Register  to view this content.
    The code does work however I am needing to do a lot more in vba and am still clearly battling with the basics.

    I am going through this website and this https://powerspreadsheets.com/vba-sub-procedures/ which seems pretty good however as often happens, I need to actually be creating the sheets and not learning about them so any assistance in speeding up the process would be highly appreciated.

    Thank you

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,591

    Re: Understanding code

    Quote Originally Posted by Pig243 View Post
    In the below code:
    Line 1 - where does "J" come from, what is that? Why is it J?
    Variable declared as Integer will be used for a loop counter.
    Why "J"?, The author of the code might like it.
    Quote Originally Posted by Pig243 View Post
    Line 2 - what does "On error Resume Next" mean?
    Ignores Run-time error, but this line would cause the problem, so should be deleted.
    Quote Originally Posted by Pig243 View Post
    I do follow lines 3 - 5
    Do you really?
    Quote Originally Posted by Pig243 View Post
    Line 6 - I have no sheet called "Sheets(2)" so what is this doing?
    Quote Originally Posted by Pig243 View Post
    Line 6 - I have no sheet called "Sheets(2)" so what is this doing?
    Line 8 - same question there is nothing called "Sheets(1)" so how does this reference work?
    Sheets(1), Sheet(2)...
    If you understand the lines, you should know why Sheet(2) comes in play...
    The number in bracket is an Index from the left, not its name.
    If you Select most left sheet, Sheet(1), and add a new sheet without specifying its location via code, it adds one worksheet BEFORE activesheet, so newly added worksheet become Sheet(1).
    So Sheet(2) is now become that was Sheet(1).
    Quote Originally Posted by Pig243 View Post
    Line 9 - hopefully will be answered above but what is "J". Why equal "2"?
    Becuase the worksheets to be summarized starts from 2nd left sheet to the end, exlculeds 1st sheet.

  3. #3
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,147

    Re: Understanding code

    hi there. "J" is simply a variable name that is created by the user. it can be "abc" or "xyz". it doesnt matter. if i have a lot of variables, i like to give it a more meaningful name for me to amend the code more easily. for this case, i might want to call it "SheetX", because my variable is going to be a loop from 2nd worksheet to the last worksheet.

    "On error Resume Next" is for the next line of code, which is:
    Please Login or Register  to view this content.
    so if this above code encounters an error, it will skip this code and resume to the next line of code. i am not that good in VBA though, and i can't think of a scenario where this will run into an error because there is always a first worksheet.

    "Sheets(2)" is not referring to the worksheet name, but the 2nd worksheet in your file. you would have a 2nd worksheet because of the code:
    Please Login or Register  to view this content.
    Line 8, as i mentioned similarly above is not referring to the worksheet name, but the 1st worksheet in your file.

    Line 9 is the loop i mentioned in the beginning which will start from 2nd worksheet to the total number of worksheets in the file

    hope that helps.

    ps: jindon covered it pretty well, but i might as well reply since i have written a bunch of it

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  4. #4
    Registered User
    Join Date
    12-18-2017
    Location
    Durban, South Africa
    MS-Off Ver
    365
    Posts
    8

    Re: Understanding code

    Thank you very much jindon.
    Very fair question, clearly I don't (didn't) understand 3 - 5. Thanks for the explanations on the other queries.
    I will delete line 2 and see what happens.

    Benishiryo, thank you, I really appreciate your time and responses.

+ 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 why the code works
    By coys1717 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-21-2014, 05:42 AM
  2. Understanding a code for getting to last row of data
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-29-2014, 12:40 PM
  3. Help with understanding VBA code
    By snaik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2014, 04:22 PM
  4. 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
  5. Understanding 2 lines of code
    By rmb623 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-28-2011, 04:00 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