I'd like to automate an annual task: I have a spreadsheet with 5 columns of data, and I need to create 20 bar charts.
The data look like this:
DRG Hospital Discharges AvgCharges Title
15 OHC 7 $5,225 CVA
15 Mt A 8 $5,651 CVA
15 Copley 6 $5,895 CVA
15 Valley 3 $6,070 CVA
There are 20 DRGs, with varying numbers of hospitals per DRG. So I've created a bar chart for each DRG, showing the Avg Charge for each hospital as a bar, along with the number of discharges sitting on top of each bar (it's actually a separate series but with the offset set to 100 so they overlap).
It's very time-consuming to update the charts each year - each year, these things change:
- DRG (so the chart titles change)
- number of hospitals appearing in a chart (so the data source range changes)
I figure it should be possible to loop through my data and create a chart for each group of DRGs. I've pilfered some code posted in another thread (68-865846 - thanks dkathrens77!) but can't get past an error ('object variable or with block variable not set').
Ideally:
- the chart title would reflect the title column
- the charts would go into separate tabs, and the tab names would reflect the DRGs
- the bars would all be dark in color except for the one assigned to "Mt A" hospital
I know I'm asking a lot... any help is much appreciated.
The data look like this:
DRG Hospital Discharges AvgCharges Title
15 OHC 7 $5,225 CVA
15 Mt A 8 $5,651 CVA
15 Copley 6 $5,895 CVA
15 Valley 3 $6,070 CVA
There are 20 DRGs, with varying numbers of hospitals per DRG. So I've created a bar chart for each DRG, showing the Avg Charge for each hospital as a bar, along with the number of discharges sitting on top of each bar (it's actually a separate series but with the offset set to 100 so they overlap).
It's very time-consuming to update the charts each year - each year, these things change:
- DRG (so the chart titles change)
- number of hospitals appearing in a chart (so the data source range changes)
I figure it should be possible to loop through my data and create a chart for each group of DRGs. I've pilfered some code posted in another thread (68-865846 - thanks dkathrens77!) but can't get past an error ('object variable or with block variable not set').
Code:
Public Sub cmdChart()
Dim strActiveSheet As String 'name of the Active Sheet
Dim arDRGChange() As String 'an array to hold all possible DRGs
Dim firstDRG As Range 'address of first row for chart
Dim lastDRG As Range 'address of last row for chart
Dim DRGA As Integer 'rememeber a DRG
Dim DRGB As Integer 'remember first DRG different from preceeding DRG
Dim intJ As Integer ' 1 to 1000 (lines on the worksheet
Dim intI As Integer ' 1 to 31 (possible dates in the month)
On Error GoTo ErrorHandler
Application.ScreenUpdating = False 'suppress jerky screen behavior
strActiveSheet = ActiveSheet.Name
intI = 1
'Note the address of the first DRG entered
' = Range ("A11") in arDRGChange(1)
'
'Go down the column of DRGs, looking for
' the first cell where the DRG is different
' from the preceeding array element
'
' Note the cell value in next available array element
' repeat this process to the last date entered on the sheet
Worksheets("DRG Source Data").Range("$A$11:$A$1000").Select
Range("A11").Activate 'the first DRG entered on this sheet
DRGA = ActiveCell.Value 'remember the first DRG
firstDRG = ActiveCell
For intJ = 11 To 1000 'step down each line on the worksheet
' looking for DRG changes
Range("A" & intJ).Select
Select Case Range("A" & intJ).Value
Case Is = DRGA
'do nothing
Case IsNull(Range("A" & intJ).Value) 'no more entries
Exit For
Case Else
'Create a new sheet then chart based on range above
ActiveWorkbook.Worksheets.Add.Name = "DRG " _
& ActiveCell.Offset(-1, 0).Value
Sheets("DRG Source Data").Activate
lastDRG = ActiveCell.Offset(-1, 0)
'add code here for creating a chart
firstDRG = ActiveCell
DRGA = Range("A" & intJ).Value
End Select
Next
Sub_Exit:
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
Application.ScreenUpdating = True
MsgBox Err.Description, vbCritical, "Error: " & Err.Number
GoTo Sub_Exit
End Sub
Ideally:
- the chart title would reflect the title column
- the charts would go into separate tabs, and the tab names would reflect the DRGs
- the bars would all be dark in color except for the one assigned to "Mt A" hospital
I know I'm asking a lot... any help is much appreciated.