XL2007 VBA Transpose Using R1C1 References & Public Constants

CiaranEire

New Member
Joined
Dec 4, 2013
Messages
9
Hi, I can't figure out why the following won't work. I have adapted code posted by user VoG on the thread http://www.mrexcel.com/forum/excel-...-applications-copy-column-data-into-rows.html into my workbook as;
Code:
Sheet2.Range("A2").Resize(2928, 80).Value = Application.Transpose(Sheet5.Range("B101:DHQ180"))
It works perfectly. However, I get Runtime Error 1004: "Method 'Range' of object '_Worksheet' failed" when I try to use publicly declared constants for the references as in the following;

Code:
Public Const Sheet5StartRow As Integer = 101
Public Const Sheet5EndRow As Integer = 180
Public Const Sheet5StartCol As Integer = 2
Public Const Sheet5EndCol As Integer = 2929

Public Const Sheet2StartRow As Integer = 2
Public Const Sheet2EndRow As Integer = 2929
Public Const Sheet2StartCol As Integer = 1
Public Const Sheet2EndCol As Integer = 80

Sheet2.Cells(Sheet2StartRow, Sheet2StartCol).Resize(Sheet2EndRow - 1, Sheet2EndCol).Value = _
    Application.Transpose(Sheet5.Range(Cells(Sheet5StartRow, Sheet5StartCol), Cells(Sheet5EndRow, Sheet5EndCol)))

When I highlight the constants in the debugger it displays the values I expect. I have tested with all sheets/cells unhidden and unprotected and no luck. I can use the first code if needs be but if for no other reason than my own learning it would be good to know what's wrong with the second method.

Many Thanks,
Ciaran
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi Ciaran

You have a syntax error, you forgot to qualify the Cells() objects in the right side of the statement:

Code:
Sheet2.Cells(Sheet2StartRow, Sheet2StartCol).Resize(Sheet2EndRow - 1, Sheet2EndCol).Value = _
    Application.Transpose(Sheet5.Range([COLOR=#ff0000]Sheet5.[/COLOR]Cells(Sheet5StartRow, Sheet5StartCol), [COLOR=#ff0000]Sheet5.[/COLOR]Cells(Sheet5EndRow, Sheet5EndCol)))
 
Upvote 0
You're welcome. Thanks for the feedback.

Remark:

Notice that although the syntax is not correct in the general case, it still might work if at the time of the execution of the statement the active sheet is Sheet5. In that case it would work because, as you know, the range objects default to the active sheet. It is not, however, safe to count on it, it's better to have the complete statement.
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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