+ Reply to Thread
Results 1 to 8 of 8

Case statement to set variable cell range

  1. #1
    Registered User
    Join Date
    07-19-2011
    Location
    home
    MS-Off Ver
    Excel 2007
    Posts
    4

    Case statement to set variable cell range

    Hey everyone. What I have is a series of buttons that each do the same thing just with different cells. So I'm trying to come up with a way to do this easily, since I'm a novice. At first I did this with a series of different functions for each button, but I have to think there is an easier way to do it. I did some searching online and figured a case statement would be the easiest way, but I can't seem to get it to work. Here is a a snipet of my code, there are a total of 50 CommandButton's but they all need to copy different cells and then print Sheet 1 and place an "X" in a different cell, that cell depends on which button is pressed. Thanks for the help.

    Please Login or Register  to view this content.
    PS: Sorry for the horrid coding, I am learning as I go! :D Thanks again.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Just a quick pointer, there's no need for the Goto statements.

    Oh, and if you want the button name have a look at Application.Caller.

    That only works for Forms buttons though.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-19-2011
    Location
    home
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Case statement to set variable cell range

    Thanks for the quick reply! If I remove the Goto statements I get an object variable or with block variable not set error.

    I had tried at one point using Application.Caller to use the name, but was having issues, I figured it was due to the buttons being command buttons and not form buttons.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    The Goto statements don't do anything.

  5. #5
    Registered User
    Join Date
    07-19-2011
    Location
    home
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Case statement to set variable cell range

    They are not doing anything. It was basically my last ditch effort to carry the cells into the print_BTN function. Without them I get the same variable not set error.

  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: Case statement to set variable cell range

    I'm sorry but your code doesn't really make sense.

    You have a Select Case statement based on a variable ButtonName, but that variable is never given a value in the code you posted.

    That probably means that none of the code in the Select Case is ever executed.

    Even if it were executed you aren't setting myrange and myrange2 to anything.

    To do that you would need to use Set.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-19-2011
    Location
    home
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Case statement to set variable cell range

    That makes a lot of sense. Thank you for the info. I still have a lot to learn on this, obviously. I will get to work on writing the statement to give the variable a value. Thanks for the help.

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

    Re: Case statement to set variable cell range

    How did you try Application.Caller?

+ 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: 4
    Last Post: 05-06-2013, 11:06 AM
  2. Case Statement on Changed Cell's Value
    By meissen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2013, 12:07 PM
  3. Cell value as variable in a range statement
    By JROG in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-06-2011, 10:12 PM
  4. How to add range to Case Statement
    By Launchnet in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-20-2010, 01:50 PM
  5. Case Statement using a Range of Conditions
    By Andrew Bird via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-28-2005, 12:06 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