VBA Reference a Dynamic Range within R1C1

XfortunaX

New Member
Joined
Aug 28, 2014
Messages
28
Hello,

I am using Excel 2010 and am stuck on inputting the last row within a RefersToR1C1 range. I am getting the Run-time error '1004': The formula you types contains an error.

The code is to find the last used row in column A, take that number and input it into the R1C1 range listed below in red. Currently, Lrow works in finding the last used row, I just cannot get the number into the line of code in red below.

The code:

Sub createJpg(Namesheet As String, nameRange As String, nameFile As String)

Dim plage As Object
Dim Commission As Range
Dim Lrow As Long
Dim coversheet As Workbook, master As Workbook, WB As Workbook

'Name the workbooks that are open
Set master = ActiveWorkbook
For Each WB In Application.Workbooks
If WB.Name Like "Cover Sheet Commission Breakout*" Then WB.Activate
Next WB
Set coversheet = ActiveWorkbook


'Find the last used row
Lrow = ActiveSheet.UsedRange.Rows.Count

'Create the JPG
Worksheets(1).Activate
ActiveWorkbook.Names.Add Name:="Commission", RefersToR1C1:= _
"=CoverSheet!R[1]C[1]:R[Lrow]C[4]"
Set plage = Worksheets(1).Range("Commission")
plage.CopyPicture
With Worksheets(1).ChartObjects.Add(plage.Left, plage.Top, plage.Width, plage.Height)
.Activate
.Chart.Paste
.Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
End With
Worksheets(1).ChartObjects(Worksheets(1).ChartObjects.Count).Delete
Set plage = Nothing
End Sub

Thank you for any help you can offer,

Tuna
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Do you want to create a range CoverSheet!$A$1:$D$x where x = Lrow?

If so try

Code:
ActiveWorkbook.Names.Add Name:="Commission", RefersToR1C1:= _
"=CoverSheet!R1C1:R" & Lrow & "C4"

Observe that i used R1C1 not R[1]C[1], i.e., i used absolute references (Row 1, Column 1)

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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