+ Reply to Thread
Results 1 to 12 of 12

Variable Frustration!

  1. #1
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Variable Frustration!

    Hi,

    A little background on the goal of my Macro:

    I have about 25 sheets each with identical tables (One table for every Sales Manager in the company). Due to poor data entry of the sales staff and the need for lookups in my workbook, I had to basically double the width of each table. So I paste into the right 10 columns, and columns A:J fill in with the TRIMmed data. When I'm done using/ updating these tables for managers, I need to move columns A:J into one master sheet that I call "AllData." I also need to delete all the columns to the right of A:J for the managers' convenience.

    So step one is to paste columns A:J as values in the same cells they are already in. Here is what I have:

    Sheets("Sheet1").Select
    Range("A2:J2").Select
    Range(Selection, Selection.End(xlDown)).Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    I can easily write this for each of the 25 sheets, but isn't the idea of variables to avoid having to copy and paste this code 25 times? How can I just write it so I say something like:

    Variable work for Sheet1
    Variable work for Sheet2
    Variable work for Sheet3

    and so on... "Variable" being the code for pasting values that I wrote above

    I don't need a lesson on naming conventions either I understand the importance (people always seem to bring this up whether inquired or not when discussing variables)

    Any and all help is appreciated!

  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: Variable Frustration!

    Something like:
    Please Login or Register  to view this content.
    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
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Variable Frustration!

    Thanks Olly!

    I'm just hoping to understand what I'm putting into my Macro doing so I can learn.
    What is "ws"? Does that stand for worksheet?
    What does "For" do?
    What does "Each" do?
    What is "Variant"?

    I'm very new to Macros if you couldn't tell! Thanks for your help!

  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: Variable Frustration!

    ws is the variable name. You could use almost anything you like to name this - but you don't want to discuss variable naming convention

    It is dimensioned as a variant, so that we can assign any sort of value to it, and use the "For Each" method (can only be used with Variants or Objects). In this case, we are passing string values from an array, each of which is a worksheet name.

    For Each means that each element of the array will be assigned to the variable in turn, then the actions between For Each... and Next... will be performed.

    HTH
    Last edited by Olly; 02-05-2015 at 06:12 PM.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Variable Frustration!

    @Olly: a worksheet is an object

    So you could also have:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  6. #6
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Variable Frustration!

    Hello again, Olly!

    Ok so I named the variable "vPasteTrimmedValues" *note solid naming convention

    I copied your code verbatim, but it didn't work. It highlights the line that says "With Worksheets(ws)" (or in my case, it says With Worksheet(vPasteTrimmedValues)) when debugging.

    Thanks again!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Variable Frustration!

    There's nothing wrong with Olly's code. It works perfectly, as is, and could be dropped straight in and executed without change.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Variable Frustration!

    What did you change?

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Variable Frustration!

    Well I managed to find the first error... I had misspelled a Sheet name ><

    But now it's having me debug at the line that says ".Value = .Value and I got an error message saying "Excel cannot complete this task with available resources. Choose less data or close other applications." My two guesses are 1) I'm using too many sheets (I'd be surprised if that were the reason, though. 2) Some of the tables on my sheets are blank.

    Any thoughts?

    UPDATE: I filled in all tables and it twerked!

    Thanks Olly!!!
    Last edited by AstToTheRegionalMGR; 02-06-2015 at 09:23 AM.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Variable Frustration!

    If there's nothing in column J, it would be operating on over 10 million cells.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  11. #11
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Variable Frustration!

    Not solved yet, I have more questions for you geniuses!

    The second part of my Macro is to delete columns K:AB on the same sheets. Would the following code work?

    Dim vDeleteColumns As Variant
    For Each vDeleteColumns In Array("Sheet1", "Sheet2")
    With Worksheets(vDeleteColumns)
    With .Range("K:AB")
    Selection.Delete
    End With
    End With
    Next vDeleteColumns

    Selection.Delete doesn't work. I am unsurprised. I suppose it's because I never actually told it that K:AB should be selected
    Last edited by AstToTheRegionalMGR; 02-06-2015 at 11:33 AM.

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

    Re: Variable Frustration!

    1. Please use CODE tags when posting code.
    2. You don't need to select ranges to work with them. It slows everything down.
    3. You need to specify exactly what it is you want to delete - in this case, it's the EntireColumns:
    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. Excel 2007 : Subtraction frustration
    By Bungee in forum Excel General
    Replies: 2
    Last Post: 12-10-2010, 04:37 AM
  2. DIV/0! Frustration
    By dtevol in forum Excel General
    Replies: 3
    Last Post: 05-06-2008, 09:06 AM
  3. Iteration frustration
    By sharkfoot in forum Excel General
    Replies: 1
    Last Post: 03-23-2006, 04:39 PM
  4. Auto_Open Frustration
    By CG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-07-2006, 11:10 PM
  5. Help with a read only frustration.
    By tweacle in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-25-2006, 11:55 AM

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