Solved

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

Posted on 2014-01-09
6
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 51

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

Industry Leaders: 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

Title # Comments Views Activity
Modify Text File with Excel Macro 13 47
Cannot locate cell 15 41
need count any combinaton of 4 numbers 7 23
How to turn this IF statement into a UDF? 5 17
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

734 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