+ Reply to Thread
Results 1 to 9 of 9

[SOLVED] Activesheet Reference for every line of code

  1. #1
    Registered User
    Join Date
    06-08-2016
    Location
    Seymour, Connecticut
    MS-Off Ver
    2013 MS Office
    Posts
    29

    [SOLVED] Activesheet Reference for every line of code

    Hi Everyone-

    I'm semi new to the world of VBA Macro's. I use the macro recorder quite a bit to get me going and was wondering if someone could help me with something. I'm attempting to write a Macro to delete rows that meet a certain criteria but I gave up on that task. Instead I've been deleting the rows using the Macro Recorder and a filter. So the Idea behind the Macro is that it

    1.) Copies Data from a Table to a worksheet
    2.) Finds any row that has waste in column 4 and gets rid of the column
    3.) Uses a VLookup to get a SKU for the stone type
    4.) subtotals tonnage produced based on SKU

    I started to get very frusturated because it seems for everyline of code I go through, I need to add the prefix activesheet everytime I refer to a range. Can someone just explain to me why that is and how to fix it?

    So to do the first two steps of this, I have:

    Please Login or Register  to view this content.
    Last edited by skellz022782; 07-06-2016 at 04:30 PM. Reason: SOLVED

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Newbie to Macros and VBA

    EDIT: Title changed and code tags added, thanks.
    Last edited by Olly; 07-06-2016 at 11:39 AM.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    06-08-2016
    Location
    Seymour, Connecticut
    MS-Off Ver
    2013 MS Office
    Posts
    29

    Re: Activesheet Reference for every line of code

    Sorry I corrected both per forum rules.

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Activesheet Reference for every line of code

    Your code can be completely rewritten to avoid selecting everything...

    Will be easier with an example of your workbook, and what you are actually trying to achieve.

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Activesheet Reference for every line of code

    Obviously completely untested (no workbook to test against), but a rewrite might look something like this:

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Activesheet Reference for every line of code

    If you omit ActiveCells then the code will automatically refer to the active sheet when you use things like Cells/Range etc.

    Saying that though, what you should really be doing is expicitly referring to the sheets you want to work with.

    For example the first part of the code, copying the table could be written like this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  7. #7
    Registered User
    Join Date
    06-08-2016
    Location
    Seymour, Connecticut
    MS-Off Ver
    2013 MS Office
    Posts
    29

    Re: Activesheet Reference for every line of code

    Quote Originally Posted by Olly View Post
    Obviously completely untested (no workbook to test against), but a rewrite might look something like this:

    Please Login or Register  to view this content.
    This just worked flawlessly. Not only that, it ran way faster. Much appreciated.

  8. #8
    Registered User
    Join Date
    06-08-2016
    Location
    Seymour, Connecticut
    MS-Off Ver
    2013 MS Office
    Posts
    29

    Re: Activesheet Reference for every line of code

    Quote Originally Posted by Norie View Post
    If you omit ActiveCells then the code will automatically refer to the active sheet when you use things like Cells/Range etc.

    Saying that though, what you should really be doing is expicitly referring to the sheets you want to work with.

    For example the first part of the code, copying the table could be written like this.
    Please Login or Register  to view this content.
    Thanks for the tip. So I guess I should stay away from selecting?

  9. #9
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Activesheet Reference for every line of code

    Happy to help. Hope it's helped you learn a better way of writing code, too!

+ 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. Newbie at Macros - tips for seemingly simple macro?
    By Drexl27 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-24-2015, 01:41 PM
  2. Hellos, another newbie here. An old newbie 31 years just started to use VBA!
    By vba_newbie83 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 08-31-2014, 03:37 AM
  3. Newbie to Macros
    By trumptight in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 08-20-2014, 10:09 AM
  4. Newbie to Macros- Excel spreadsheet with insert images autofit help
    By sidfire in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2011, 03:20 AM
  5. Newbie needs help with String Manipulation & Macros
    By rhyswynne in forum Excel General
    Replies: 1
    Last Post: 10-18-2006, 02:35 PM
  6. [SOLVED] Newbie on Macros
    By [email protected] in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-01-2006, 06:45 AM
  7. Newbie Help - Macros
    By NEO_72 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 05-12-2005, 12:21 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