Macro to generate a report, select next department from list and recalc

VEI

New Member
Joined
Aug 25, 2011
Messages
3
I was wondering if it is possible to have a macro that would select a department from a drop down list, recalculate the sheet, print and then select the next department in the list and start the process over again. This would continue until it reached the end of the list.

This would produce an income statement for each department based on the department selected.

I have searched and I have not found anything that I thought applied. Thank you in advance for your expertise.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This should do it, just edit the MyCell to the cell with your drop down list.
Code:
Option Explicit

Sub CycleThroughDataValidationOptions()
'Summary:   Cycle through all the options in a drop down for a specific cell
Dim MyCell As Range, myDVList As String
Dim DVCnt As Long, Itm As Long

'set the cell address here
    Set MyCell = Range("B1")
'retrieve the DV source range from the cell
    myDVList = Mid(MyCell.Validation.Formula1, 2, 99)
'count the items in that list
    DVCnt = Range(myDVList).Cells.Count

'cycle through the items one at a time
    For Itm = 1 To DVCnt
        MyCell = Range(myDVList).Cells(Itm)
        ActiveSheet.PrintOut
    Next Itm

End Sub
 
Last edited by a moderator:
Upvote 0
You make this look so easy...

I need a manual recalculation after each department is selected.

Is it possible to have a way to stop the macro if you do not want it to go through the whole list?

How about the ability to select a starting and stoping point on the data validation list?

I am taking a 3 day course on VBA locally so hopefully that will get me up to speed quickly.

Thanks again for your expertise.
 
Upvote 0
Perhaps add this:
Rich (BB code):
    For Itm = 1 To DVCnt
        MyCell = Range(myDVList).Cells(Itm)
        ActiveSheet.Calculate
        ActiveSheet.PrintOut
    Next Itm


As for starting and ending points... ugh. How long is that list?
 
Upvote 0
Approximately 300 items in the list. This is just a wish list if not easily done. I think a work around would be to just modify the data validation list. Any way to stop the macro once it is started in case I need to stop it?
 
Upvote 0
What if you add an additional cell to your sheet that use the same DV list, then select your FIRST and LAST categories in those two cells? If B1 is the first cell, let's make C1 the second cell, and it's optional, so if nothing is in there it will continue to the end of the list from the starting point in B1. If B1 is empty, too, it will simply process all the categories.
Code:
Option Explicit

Sub CycleThroughDataValidationOptions()
'Author:    Jerry Beaucaire,  8/25/2011
'Summary:   Cycle through all the options in a drop down for a specific cell
Dim MyCell As Range, myDVList As String, strVal As String
Dim DVCnt As Long, Itm As Long
Dim Frst As Long, Last As Long

With ActiveSheet
'set the primary cell address here
    Set MyCell = .Range("B2")
'retrieve the DV source range from the cell
    myDVList = Mid(MyCell.Validation.Formula1, 2, 99)

'First and last
    On Error Resume Next
    'strVal = MyCell.Value
    Frst = WorksheetFunction.Match(MyCell, Range(myDVList), 0)
    If Frst = 0 Then Frst = 1
    'strVal = MyCell.Offset(, 1).Value
    Last = WorksheetFunction.Match(MyCell.Offset(, 1), Range(myDVList), 0)
    If Last = 0 Then Last = Range(myDVList).Cells.Count

'cycle through the items one at a time
    For Itm = Frst To Last
        MyCell = Range(myDVList).Cells(Itm)
        .Calculate
        .PrintOut
    Next Itm
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top