[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

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
0
donohara1
Asked:
donohara1
1 Solution
 
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
 
Rgonzo1971Commented:
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
 
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 LissRetired ProgrammerCommented:
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

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now