Repositioning copy-paste operations from simple (full row or full columns) to targeted cells

I have been given a large spreadsheet with many VBA macros to convert (to a reformatted source workbook).
Below is am example of a simple copy & paste operation, Using the new source I have to target the paste target to start on row 3 (vs row 1). So I have to convert the .Columns operator to a  .Cells operator. For example:  change:

Set r = thwb.Sheets(8).Columns("p")   (r is a range,  thwb is this workbook used as a target)
   to:

   for example:
Set r = thwb.Sheets(8).Cells("p3")    to start the Paste on Row 3.
Set SourceSh = Sourcewb.Sheets("my data")
                    SourceSh.Columns("a5").Copy
                    r.PasteSpecial xlPasteValues, , , False

When I do this, I get the error msg:  ' application or object designed error '

What am I not doing correctly?

Thanks,
Don


Sample code (after changing from .Columns to .Cells ):

Sub Stocks()

    Dim Sourcewb As Workbook
    Dim SourceSh As Worksheet
    Dim thwb As Workbook
    Dim r As Range
   
        Set thwb = ThisWorkbook
        Set r = thwb.Sheets(8).Cells("p3")
Err.Clear
On Error GoTo errhandler
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
        FilePath = "C:\Users\DonO\Documents\FIRMS\Test\QRM\"
        Filename = Dir(FilePath & "*.xlsx")
        Do While Filename <> ""
  '
  '   only one workbook in source directory.
  '
            If Filename = thwb.Name Then GoTo nxt
            If Filename & ".xlsx" = thwb.Name Then GoTo nxt
                Set Sourcewb = Workbooks.Open(FilePath & Filename, False)
                    Set SourceSh = Sourcewb.Sheets("Risk by Securities")
                    SourceSh.Columns("a:d").Copy
                    r.PasteSpecial xlPasteValues, , , False
       
        Set r = thwb.Sheets(8).Cells("aa3")
        SourceSh.Columns("e:g").Copy
                    r.PasteSpecial xlPasteValues, , , False
       
       
        Set r = thwb.Sheets(8).Cells("x3")
        SourceSh.Columns("i:j").Copy
                    r.PasteSpecial xlPasteValues, , , False
       
       
        Set r = thwb.Sheets(8).Cells("af3")
        SourceSh.Columns("k:s").Copy
                    r.PasteSpecial xlPasteValues, , , False
       
       
        Set r = thwb.Sheets(8).Cells("ad3")
        SourceSh.Columns("j").Copy
                    r.PasteSpecial xlPasteValues, , , False
       
       
        Set r = thwb.Sheets(8).Cells("z3")
        SourceSh.Columns("s").Copy
                    r.PasteSpecial xlPasteValues, , , False
       
            Workbooks(Filename).Close SaveChanges:=False
nxt:
                Filename = Dir
        Loop
     
    Set Sourcewb = Nothing
    Set SourceSh = Nothing
    Set thwb = Nothing
errhandler:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
        If Err.Number <> 0 Then
            MsgBox "An error has occurred." & vbNewLine & Err.Description
        End If
        Sheets("Control").Select
    Range("A1").Select
End Sub
donohara1Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Rgonzo1971Connect With a Mentor Commented:
Hi,

You are trying to paste a whole column at row 3 instead of row 1 you have at least to resize the data to match the maximum rows you can copy to

Set r = thwb.Sheets(8).Range("aa3")
        SourceSh.Range("e:g").Resize(Rows.Count - 3).Copy
                    r.PasteSpecial xlPasteValues, , , False

Open in new window


or do you want to copy only from Row 3
Set r = thwb.Sheets(8).Range("aa3")
        SourceSh.Range("e3:g" & Rows.Count).Copy
                    r.PasteSpecial xlPasteValues, , , False

Open in new window


Regards
0
 
Patrick MatthewsCommented:
Proper syntax for Cells is:

ActiveSheet.Cells(r, c)

r is a row index, and c is a column index, although a column letter is OK if you are using the A1 reference style.

Thus, change:

        Set r = thwb.Sheets(8).Cells("p3")

Open in new window


to:

        Set r = thwb.Sheets(8).Cells(3, "p3")

Open in new window


Etc.
0
 
MacroShadowCommented:
-
0
 
donohara1Author Commented:
Hi,

I tried the -3 approach and that worked well on 4 other sheets. Thank you.
I have one sheet (shown below), where I find that only the first 'r=thwb.sheets(7).Range....
is being used.  All subsequent "R=" specs seem to be ignored. All further paste operations are placed in the same cell (B3) as the first place.

please advise.
Thanks,
Don


Sub Risk()
    Dim Sourcewb As Workbook
    Dim SourceSh As Worksheet
    Dim thwb As Workbook
    Dim r As Range
   
        Set thwb = ThisWorkbook
        Set r = thwb.Sheets(7).Range("b3")
Err.Clear
On Error GoTo errhandler
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
       ' FilePath = "L:\Externally Managed Investments\External Managers\Managers - Current\Summary\QRM\" 'Change "c:\XXX\xy charts\" to the path of your files
      ' Directory change
        FilePath = "C:\Users\DonO\Documents\FIRMS\UnivCal\KCHowCoaker131025\Test\QRM\"
        Filename = Dir(FilePath & "*.xlsx")
        Do While Filename <> ""
            If Filename = thwb.Name Then GoTo nxt
            If Filename & ".xlsx" = thwb.Name Then GoTo nxt
                Set Sourcewb = Workbooks.Open(FilePath & Filename, False)
                    Set SourceSh = Sourcewb.Sheets("risk summary")
                    SourceSh.Range("e20").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                    r = thwb.Sheets(7).Range("c5")
                    SourceSh.Range("e7").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                     r = thwb.Sheets(7).Range("e8:e16")
                    SourceSh.Range("e25:e33").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                     r = thwb.Sheets(7).Range("c18")
                    SourceSh.Range("e4").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                     r = thwb.Sheets(7).Range("c19")
                    SourceSh.Range("e2").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                     r = thwb.Sheets(7).Range("c20")
                    SourceSh.Range("e3").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                     r = thwb.Sheets(7).Range("g19")
                    SourceSh.Range("e21").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                     r = thwb.Sheets(7).Range("g20")
                    SourceSh.Range("e24").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                     r = thwb.Sheets(7).Range("h19")
                    SourceSh.Range("e22").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
                     r = thwb.Sheets(7).Range("h20")
                    SourceSh.Range("e24").Copy
                    r.PasteSpecial xlPasteValues, , , False
                   
            '        SourceSh.Cells("c8:c16").Copy
            '        r.Offset(5, 1).PasteSpecial xlPasteValues, , , False
            '        SourceSh.Cells("e8:e16").Copy
             '       r.Offset(5, 3).PasteSpecial xlPasteValues, , , False
            '        SourceSh.Cells("c18:c21").Copy
            '        r.Offset(15, 1).PasteSpecial xlPasteValues, , , False
            '        SourceSh.Cells("g19:h20").Copy
            '        r.Offset(16, 5).PasteSpecial xlPasteValues, , , False
            '        SourceSh.Cells("a27:q39").Copy
             '       r.Offset(24, -1).PasteSpecial xlPasteValues, , , False
                                       
                   
            Workbooks(Filename).Close SaveChanges:=False
nxt:
                Filename = Dir
        Loop
     
    Set Sourcewb = Nothing
    Set SourceSh = Nothing
    Set thwb = Nothing
errhandler:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
        If Err.Number <> 0 Then
            MsgBox "An error has occurred." & vbNewLine & Err.Description
        End If
        Sheets("Control").Select
    Range("A1").Select
End Sub
0
 
Martin LissOlder than dirtCommented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Rgonzo1971's comment #a39767734

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.