Need help with "Dim"

bcurrey

Board Regular
Joined
Aug 11, 2011
Messages
110
Office Version
  1. 365
Platform
  1. MacOS
I have the code below in a macro and it copies the data in Range("C26:D38") and saves it as a png file. After it saves the data once, I have code copying and pasting data over this info. I would then like to export the data again (it has now been updated), as a different file name. When I tried to do this it gave me an error "COMPILE ERROR DUPLICATE DECLARATION IN CURRENT SCOPE."

I assume this means that VBA code is not read top to bottom (like a book) and I need to change something each time I insert the code in the macro. I'm not sure what to change though. Any ideas?

Thanks!!


Code:
' save a range from Excel as a picture
Dim rng As Excel.Range
Dim cht As Excel.ChartObject
 
Const strPath As String = "C:\Documents and Settings\CB021\Desktop\Real time stats web\images\"
 
Application.ScreenUpdating = False
 
Set rng = Range("C26:D38").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 & "OrdersShipped_text.png"
 
cht.Delete
 
ExitProc:
Application.ScreenUpdating = True
Set cht = Nothing
Set rng = Nothing
 
Last edited by a moderator:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
If you try to declare (Dim) the same variable twice in the same macro procedure, you will get that error.

Example: if you have
Code:
Dim rng As Excel.Range
...and then you have it again in the same procedure, that causes the error. Same applies to Const.

You don't need to declare rng twice, you can simply set it to a different value.
 
Last edited:
Upvote 0
That makes sense.

So would my declare line look like this:

Code:
 Set rng = Excel.Range("C26:D38").CurrentRegion

Thanks!!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

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