+ Reply to Thread
Results 1 to 4 of 4

Coding for multipe independent targets within VBA

  1. #1
    Registered User
    Join Date
    05-24-2013
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2010
    Posts
    2

    Coding for multipe independent targets within VBA

    Hi Guys,

    New to the forum as well to VBA. I am in the process of building a spreadsheet and am struggling a little bit with some elementary VBA code/layout. Here is what I am trying to accomplish. On Sheet1, I am trying to have a user enter info that can ultimately be used on other sheets for more in depth calculations. For example, in cell B7, I am asking the user (car dealership) to select the total number of Cars that they will be purchasing (option from drop down list of 0 through 9). Then I need to gather data on each one of those individual cars (make, model, front wheel drive/rear/all, etc... you get the point). I don't want to have a separate detailed entry table for 9 cars displayed if they are only purchasing 3, so my goal is to have them enter the total # of cars they are purchasing, and then only displaying detailed entry tables for 3 of those cars rather than all 9. This is the code I have to accomplish this task and it is working beautifully.

    Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address <> "$B$7" Then Exit Sub
    If Target.Value = 0 Then Rows("8:33").EntireRow.Hidden = True
    If Target.Value = 1 Then Rows("8:10").EntireRow.Hidden = False
    If Target.Value = 2 Then Rows("8:13").EntireRow.Hidden = False
    If Target.Value = 3 Then Rows("8:16").EntireRow.Hidden = False
    If Target.Value = 4 Then Rows("8:19").EntireRow.Hidden = False
    If Target.Value = 5 Then Rows("8:22").EntireRow.Hidden = False
    If Target.Value = 6 Then Rows("8:25").EntireRow.Hidden = False
    If Target.Value = 7 Then Rows("8:28").EntireRow.Hidden = False
    If Target.Value = 8 Then Rows("8:31").EntireRow.Hidden = False
    If Target.Value = 9 Then Rows("8:33").EntireRow.Hidden = False
    End Sub

    The problem is that I want to do the same for Trucks, Vans, Buses, & Motorcycles. These are completely independent of the car value above, but nevertheless, still trying to accomplish the same exact task. Given my lack of experience with VBA, I am just not sure how to integrate the following code into the original code and have it work the way I need it to. As you can see, its identical, just applies to a different set of rows within the spreadsheet.

    If Target.Address <> "$B$35" Then Exit Sub
    If Target.Value = 0 Then Rows("36:61").EntireRow.Hidden = True
    If Target.Value = 1 Then Rows("36:38").EntireRow.Hidden = False
    If Target.Value = 2 Then Rows("36:41").EntireRow.Hidden = False
    If Target.Value = 3 Then Rows("36:44").EntireRow.Hidden = False
    If Target.Value = 4 Then Rows("36:47").EntireRow.Hidden = False
    If Target.Value = 5 Then Rows("36:50").EntireRow.Hidden = False
    If Target.Value = 6 Then Rows("36:53").EntireRow.Hidden = False
    If Target.Value = 7 Then Rows("36:56").EntireRow.Hidden = False
    If Target.Value = 8 Then Rows("36:59").EntireRow.Hidden = False
    If Target.Value = 9 Then Rows("36:61").EntireRow.Hidden = False

    Any help on this topic would be much appreciated. Again, sorry if this is elementary, I am just super green when it comes to VBA.

    Thanks in advance for your help!

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Coding for multipe independent targets within VBA

    Hi Scan,

    Try it like this:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    05-24-2013
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Coding for multipe independent targets within VBA

    This worked like a champ!!! Thanks so much for taking the time to help me out XLAdept!!!

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Coding for multipe independent targets within VBA

    You're welcome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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