Run Time Error '1004': Method 'Range' of object '_Worksheet' failed

flagen

New Member
Joined
Jun 22, 2015
Messages
16
I am trying to take a column of data that I want to paste in five different columns. I want it to go:
1 to 12345
2 12345
3 12345
4 12345
5 12345
1
2
3
4
5



... and so on. Here is the code:

Code:
Option Explicit
Sub ReArrangeCells()


  Dim ws As Worksheet, LastRow As Long
  Set ws = Excel.ActiveSheet


  LastRow = Range("G71311").End(xlUp).Row


  Dim i As Long, j As Long, FromCell As Range, ToCell As Range, sNewCol As String, sNewRow As String
  For i = 1 To LastRow


    Set FromCell = ws.Range("G" & i)                       'the cell we want to move
    sNewCol = IIf(i Mod 5 = 0, Chr$(79), Chr$((i Mod 5) + 25))
    sNewRow = IIf(i Mod 5 = 0, (i \ 5), (i \ 5) + 1)
    Set ToCell = ws.Range(sNewCol & sNewRow)               'the cell we want to copy the data to


    FromCell.Copy ToCell
    If i <> 1 Then FromCell.Clear


    If i Mod 100 = 0 Then DoEvents


  Next i


End Sub

Here is the line the error occurs:
Code:
Set ToCell = ws.Range(sNewCol & sNewRow)               'the cell we want to copy the data to

I'm not sure how to adjust it.
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi flagen,

I'd say the issue is that the sNewCol variable is not returning a letter and so the ToCell range cannot be set.

HTH

Robert
 
Upvote 0
Set FromCell = ws.Range("G" & i) 'the cell we want to move
sNewCol = IIf(i Mod 5 = 0, Chr$(79), Chr$((i Mod 5) + 25))
sNewRow = IIf(i Mod 5 = 0, (i \ 5), (i \ 5) + 1)
Set ToCell = ws.Range(sNewCol & sNewRow) 'the cell we want to copy the data to
[/code]
What character/column-letter do you expect the red highlighted text to generate?

Perhaps you meant the 25 to be 65?
 
Upvote 0
Hi Rick and Trebor,

Ah, that part was edited from another forum post I found that was attempting to move data into 8 different columns. I have never done anything with Chr values so I wasn't sure how it worked until reading a list of them for a little while, it should have been:
Rich (BB code):
sNewCol = IIf(i Mod 5 = 0, Chr$(79), Chr$((i Mod 5) + 74))
I want it posting to column O, then the 4 columns after. Thank you for the help and for recognizing my mistake!
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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