Help with VBA if/then statement

Jameo

Active Member
Joined
Apr 14, 2011
Messages
270
Hi all,

I have several dynamic named ranges (day, hour station1, station2 etc.) in my exel spreadsheet.

I also have the following code to plot, and arrange a chart:

Code:
    Charts.Add
    ActiveChart.ChartType = xlXYScatter
    ActiveChart.SetSourceData Source:=Sheets("Histograms").Range("I21")
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).XValues = "=Book1!day"
    ActiveChart.SeriesCollection(1).Values = "=Book1!Station1"
    ActiveChart.SeriesCollection(1).Name = "=Book1!name1"
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Histograms"
 
Dim RngToCover As Range
Dim ChtOb As ChartObject
Set RngToCover = ActiveSheet.Range("a1:h20")
Set ChtOb = ActiveChart.Parent
ChtOb.Height = RngToCover.Height ' resize
ChtOb.Width = RngToCover.Width ' resize
ChtOb.Top = RngToCover.Top ' reposition
ChtOb.Left = RngToCover.Left ' reposition

My problem is that dependant on the query, some of the dynamic ranges are empty. If they are, then I do not want to plot the chart. Is there a way I can use an if/then statement to:

If - The named range has data populated.
Then - run the above code,
Else if - move on to the next dynamic range (and complete the same checks)
End if

Any help would be great.

Thanks in advance
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
you can add an if statement, so my test the range name: abc_list

If Application.WorksheetFunction.CountA(Range("abc_list")) = 0 Then
MsgBox "Range is empty"
Exit Sub
End If
 
Upvote 0
Excellent stuff, thanks. I'll give this a go today. Next step is to get the appropriate data exported automatically from Access, and also clear the data on export/import (leaving the dynamic ranges intact), and add a line to the top of the above to clear any previous graphs plotted on the spreadsheet.

Cheers for your help
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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