Help with VBA Code to select a range that include blank rows

t.powley

Board Regular
Joined
Jun 13, 2002
Messages
76
I hope someone can help,

I have this macro that finds all data in columns A:D, creates a chart from that data, saves the chart as a .png image then deletes the chart.

It works perfectly when the are no blank rows, but as soon as there is a blank row it will only create the chart from the first blank row up.

I need the chart to be created from the very last row of data up.


Code:
Sub printbulletin()

LstRwInD = Cells("D" & Rows.Count).End(xlUp).Row
Set Rng = Excel.Range("A1:D" & LstRwInD).CurrentRegion
      Rng.CopyPicture xlScreen, xlPicture
Set cht = ActiveSheet.ChartObjects.Add(0, 0, Rng.Width + 0.01, Rng.Height + 0.01)
cht.Chart.Paste
cht.Chart.Export strPath & "T:\Homepage\staffbulletin.png"
ActiveSheet.ChartObjects.Delete

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I have this macro that finds all data in columns A:D, creates a chart from that data, saves the chart as a .png image then deletes the chart.

It works perfectly when the are no blank rows, but as soon as there is a blank row it will only create the chart from the first blank row up.

I need the chart to be created from the very last row of data up.
Rich (BB code):
Sub printbulletin()

LstRwInD = Cells("D" & Rows.Count).End(xlUp).Row
Set Rng = Excel.Range("A1:D" & LstRwInD).CurrentRegion
      Rng.CopyPicture xlScreen, xlPicture
Set cht = ActiveSheet.ChartObjects.Add(0, 0, Rng.Width + 0.01, Rng.Height + 0.01)
cht.Chart.Paste
cht.Chart.Export strPath & "T:\Homepage\staffbulletin.png"
ActiveSheet.ChartObjects.Delete

End Sub
Remove the .CurrentRegion (shown in red above)... you already have the full range with Range("A1:D" & LstRwInD).
 
Upvote 0
Remove the .CurrentRegion (shown in red above)... you already have the full range with Range("A1:D" & LstRwInD).

Thank you, now works as required.

I'm still learning by hacking together VBA scripts so I'm slowly learning from mistakes and advice, having these comments is very helpfull.
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

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