Solved

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

Posted on 2014-01-09
6
144 Views
Last Modified: 2014-10-30
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
0
Comment
Question by:donohara1
6 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39767723
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
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39767725
-
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39767734
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
 

Author Comment

by:donohara1
ID: 39778040
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
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40413322
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question