+ Reply to Thread
Results 1 to 33 of 33

Coding Multiple Cases in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Coding Multiple Cases in VBA

    I am at a loss. I am writing VBA code to do the following:
    IF A1 > 50 then print array C1:E7

    I have this code and it works.

    Sub Test()
    If Range("A1") > 50 Then
    ActiveSheet.PageSetup.PrintArea = "C1:E7"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1
    End If
    Call cler_print
    End Sub
    Sub cler_print()
    ActiveSheet.PageSetup.PrintArea = ""
    End Sub
    Now, I want to be able to do the same for the following within the same module:
    If A10>50 then print C10:E17
    If A21>50 then print C21:E30 and so on for three more 'tests' of variables

    Question: can a series of such commands be included in one code activated by one MACRO button? If so, how does one write a series of cases testing the values in a series of cells to deterine if an associated array should be printed?

    I have tried to do this repeatedly and cannot find the right syntax that will trigger a series of tests with one macro button click.

    In the end, I want to user to complete a checklist - then click a MACRO button that will print arrays of information if the values in specific cells meet the criterion >50. I want the code to 'test' the values in 6 different cells and to print, based on compliance with the criterion, 6 different arrays of information.

    I'm under the gun and am stalled. People are waiting on my solution and I can't seem to move forward as I am not too familiar with VBA.

    Can and will anyone HELP???

    Best regards...I know you are all busy!!


    Bill (e-mail address removed by mod)
    Last edited by billofsoo; 03-25-2009 at 09:25 AM. Reason: e-mail address removed by mod

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

    Re: Coding Multiple Cases in VBA

    billofsoo,

    e-mail address removed and code wrapped as per forum rules below.

    Is that a pattern to the cells and ranges to print

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Re: Coding Multiple Cases in VBA

    Sorry first timer I guess.

    No it is not a pattern. There are six sections to the checklist we are using. Teachers complete the checklist and percentages are calculated based on the teacher's ratings of items. Each section has a cell with a percentage entered.

    Asssociated with each section is an array of cells containing text.

    What I would like the program to do is to 'test' the percentage value in the six cells - one for each section. If the number in the cell is above 50, I want it to print the associated array of cells that hold the text.

    I can do it for one cell and the associated array but don't know how to write code so that once the macro button is clicked each of the six cell values are tested and the appropriate information is printed.

    I don't know if this is possible. I just posted the cell designations in the previous post as examples; however, I do have the specific cell locations to to be tested and the associated arrays that would be needed.

    A28 S100:U161
    B38 S164:U188
    C50 S192:U218
    D61 S224:U269
    E73 S276:U313
    F93 S321:U382

    I don't know if this is clear to you or not. I was just hoping there would be a way to write a code that would keep things really simple for those with whom I work.
    Thanks for your time & interest.
    Bill

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

    Re: Coding Multiple Cases in VBA

    Maybe

    Sub Test()
    Dim ArrRng As Variant
    Dim ArrPrint As Variant
    Dim i As Long
        ArrRng = Array("A28", "B38", "C50", "D61", "E73", "F93")
        ArrPrint = Array("S100:U161", "S164:U188", "S192:U218", "S224:U269", "S276:U313", "S321:U382")
    For i = 0 To UBound(ArrRng)
        If Range(ArrRng(i)) > 50 Then
            With ActiveSheet
                .PageSetup.PrintArea = ArrPrint(i)
                .PrintOut Copies:=1
            End With
        End If
        
        Call cler_print
    Next i
    End Sub
    Sub cler_print()
         ActiveSheet.PageSetup.PrintArea = ""
    End Sub
    VBA Noob

  5. #5
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Re: Coding Multiple Cases in VBA

    Man you are something else....I will try this.

    Where does this second peice fit in? Is it part of the larger code you wrote?

    Code:
    Sub cler_print()
         ActiveSheet.PageSetup.PrintArea = ""
    End Sub
    I'm sorry, I highlighted this code and click the # sign above but I can't seem to make this work as per rules!!

    Not sure where to put this second peice of code you wrote.

    BillofSoo

  6. #6
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

    Re: Coding Multiple Cases in VBA

    billofsoo,


    It's the code you posted earlier. It resets the PrintArea to blank

    Place code in same module

    Also I've wrapped you're code for you this time. Next time type in [code] at the start and end of the code and add a / to the last one between the [ and the c

    e.g
    [/code
    Can't add the last bracket but you get the idea.

    If you're happy with the solution please mark it as solved

    VBA Noob
    Last edited by VBA Noob; 03-24-2009 at 06:10 PM.

  7. #7
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Re: Coding Multiple Cases in VBA

    Sorryl my Question was cross posted to these forums:

    http://www.mrexcel.com/forum/showthread.php?t=378477
    http://bytes.com/topic/visual-basic/...67106-vb-excel

    VBA Noob, is this how to do this?

  8. #8
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

    Re: Coding Multiple Cases in VBA

    Thanks

    Maybe those cells are all less than 50. Here is my example but instead of printing I've used printpreview to save trees

    VBA Noob
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Re: Coding Multiple Cases in VBA

    I ran your module and it worked.
    The values in the testing cells are all at 100 - ah...but the value is being transferred to the cell using a formula - could that be a problem?

    I tried the module changing J28 to K28 with a value of 100 and without a formula. It should have printed the array associated with K28 but nothing happened.

    Could it be that I am not setting up the macro to the button properly?
    Actually when I use the RUN button at the top right of the MACRO window there is still no action.
    Any suggestions?

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Coding Multiple Cases in VBA

    Just another tack, from a 101 approach, better for my easily addled brain. The only real bonus to this approach is that it reads more 1-to-1 and there is no "print area" to set/clear.
    Sub PrintArrays()
        If Range("A28").Value > 50 Then Range("S100:U161").PrintOut Copies:=1
        If Range("B38").Value > 50 Then Range("S164:U188").PrintOut Copies:=1
        If Range("C50").Value > 50 Then Range("S192:U218").PrintOut Copies:=1
        If Range("D61").Value > 50 Then Range("S224:U269").PrintOut Copies:=1
        If Range("E73").Value > 50 Then Range("S276:U313").PrintOut Copies:=1
        If Range("F93").Value > 50 Then Range("S321:U382").PrintOut Copies:=1
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  11. #11
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Re: Coding Multiple Cases in VBA

    It works...thanks so much. Seems like simpler was better in a sense.

    I will mark this thread as solved.

    Thanks again.

    Billofsoo

  12. #12
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Coding Multiple Cases in VBA

    VBA NOOD

    I think I spoke too soon.

    It seems that if the first two cells are >50 then only their associated arrays print - like it is supposed to work.
    If the first cell is less than 50 then the first associate arrays doesn't print - like it is supposed to work..
    BUT
    If the first third and sixth cells are less than 50 then the first array does not print but ALL the others do.

    Don't know why....can you consider why this might be happeing?



    Thanks
    Last edited by billofsoo; 03-24-2009 at 09:23 PM.

  13. #13
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Re: Coding Multiple Cases in VBA

    VBA Nood

    Actually if all cells are less than 50 except the first cell then ALL arrays are printed. Can't see any problem with the code at all. In fact if only the third cell is greater than 50 then all pages still print. Weird!

    Let me know what you think - it is late here..need to go home. Will check for a response in the morning.

    Thanks a lot for your patience.

    BillofSoo

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Coding Multiple Cases in VBA

    Threads merged.
    Entia non sunt multiplicanda sine necessitate

  15. #15
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

    Re: Coding Multiple Cases in VBA

    Using my example again it all seem to work.

    Post your file (remove any info) and I'll take a look or go with the other option you got

    VBA Noob
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Re: Coding Multiple Cases in VBA

    Thanks VBA Noob

    I have stripped out the info from the worksheet - left some in as well along with some notes so you don't have to work hard to figure out how I set this up. I included three modules you have seen and adjusted them to match my exact cell/array locations.

    Best of luck...hope you get this.

    Best regards,

    BillofSoo
    Attached Files Attached Files

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Coding Multiple Cases in VBA

    Did the remedial version I posted not work at all?

  18. #18
    Registered User
    Join Date
    11-18-2005
    Posts
    17

    Re: Coding Multiple Cases in VBA

    VBA Noob: not sure what you mean by remedial version. However, I do know that your code works if there is a way to take the formula produced number and paste it as a value.

    Can we write a macro to begin the module that copies the numbers from J that result from a formula and Paste Special into column K as a value?

    I think that would do it...just not sure how to insert into your module.

    Is it possible?

    BillofSoo

  19. #19
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

    Re: Coding Multiple Cases in VBA

    One way

    Sub Test()
    Dim ArrRng As Variant
    Dim ArrPrint As Variant
    Dim cl As Range
    Dim i As Long
        For Each cl In Columns("J:J").SpecialCells(xlCellTypeFormulas, 23)
            With cl
                If IsNumeric(.Value) Then
                    cl.Offset(0, 1).Value = .Value
                End If
            End With
        Next cl
        ArrRng = Array("J28", "J38", "J50", "J61", "J73", "J93")
        ArrPrint = Array("S100:U161", "S164:U188", "S192:U218", "S224:U269", "S276:U313", "S321:U382")
    For i = 0 To UBound(ArrRng)
        With Range(ArrRng(i))
            If IsNumeric(.Value) Then
                If .Value > 50 Then
                    With ActiveSheet
                        .Range(ArrPrint(i)).PrintOut Copies:=1
                    End With
                End If
            End If
        End With
        
    Next i
    End Sub
    VBA Noob

+ 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