+ Reply to Thread
Results 1 to 6 of 6

Referencing Worksheet Using a Variable (New to VBA

  1. #1
    Forum Contributor
    Join Date
    05-03-2021
    Location
    Nashville
    MS-Off Ver
    MS Office 365
    Posts
    184

    Referencing Worksheet Using a Variable (New to VBA

    I am quite new to VBA, while I can do simple "Macro Recording".
    I am trying to branch out and shorten unnecessary code.

    Background of what I am trying to do.

    I have a "database" for every month of the Year (so every tab is named JAN - DEC).

    To prevent having to write the same VBA 12 times referencing sheets("JAN").select all the way to sheets("DEC").select 48 times.
    (exaggeration but you get the gist) I want to be able to create a variable that references the sheet so that if I need to make changes, I am only changing one thing at the top of the code instead of looking at a long string of code and make changes to every line that has "sheets("JAN").select"

    Dim xstr as worksheet
    Set xstr = Worksheet("JAN")

    It seems when I do
    xstr.Select I get an "Debug" request at the line I have this code at.

    I'm sure this is something super easy. Just missing how to get variables to work for me to make coding a lot shorter and easier to update in one spot.

    Thanks in advance.
    Last edited by thenewkidd; 05-03-2021 at 08:53 PM.

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Referencing Worksheet Using a Variable (New to VBA

    Try

    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    419

    Re: Referencing Worksheet Using a Variable (New to VBA

    Difficult to diagnose without more info - what does the debug message say? Can you upload the spreadsheet?

  4. #4
    Forum Contributor
    Join Date
    05-03-2021
    Location
    Nashville
    MS-Off Ver
    MS Office 365
    Posts
    184

    Re: Referencing Worksheet Using a Variable (New to VBA

    Quote Originally Posted by maniacb View Post
    Try

    Please Login or Register  to view this content.
    did that and it is still giving a debug error.

    Quote Originally Posted by mgs73 View Post
    Difficult to diagnose without more info - what does the debug message say? Can you upload the spreadsheet?
    The debut says "Run time error 91 Object or With block variable not set"
    But it is set.. so I'm confused
    I will try and upload. I'm new to VBA so there are a lot of things wrong with the code. I started with "Record" and now I am chopping it down to things I don't need.

  5. #5
    Forum Contributor
    Join Date
    05-03-2021
    Location
    Nashville
    MS-Off Ver
    MS Office 365
    Posts
    184

    Re: Referencing Worksheet Using a Variable (New to VBA

    Workbook is too large to post, it keeps failing.

    But here is the start of the VBA

    Dim xstr As Worksheet
    Dim ii As Integer
    Set Worksheet = Sheets("JAN")

    i = 1

    ---> [ERROR is right here]
    xstr.Select
    Columns("A:H").Select
    Selection.Copy
    Sheets("FILTER").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("I4").Select
    Application.CutCopyMode = False

  6. #6
    Forum Contributor
    Join Date
    05-03-2021
    Location
    Nashville
    MS-Off Ver
    MS Office 365
    Posts
    184

    Re: Referencing Worksheet Using a Variable (New to VBA

    I see what I did,
    Thank you for your help. I feel dumb.

    Old
    Dim xstr As Worksheet
    Dim ii As Integer
    Set Worksheet= Sheets("JAN")

    New
    Dim xstr As Worksheet
    Dim ii As Integer
    Set xstr = Sheets("JAN")

+ 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. Replies: 10
    Last Post: 09-10-2018, 06:58 PM
  2. [SOLVED] Variable cell referencing a worksheet
    By MeBeMel in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-06-2015, 09:54 AM
  3. referencing variable help
    By 13lack13lade in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-10-2014, 08:56 PM
  4. referencing a defined worksheet variable in a formula in vba
    By framptonl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2012, 08:13 PM
  5. Simple formula with variable names referencing another worksheet
    By gototcm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-02-2012, 05:56 AM
  6. Variable referencing?
    By nasbcn in forum Excel General
    Replies: 2
    Last Post: 01-25-2008, 11:33 AM
  7. Referencing a variable in place of worksheet name
    By marksince1984 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-15-2006, 05:36 PM

Tags for this Thread

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