Solved

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

Posted on 2014-01-09
6
111 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
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
-
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now