Could someone please edit this code for me? Thank for helping!

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi,

I am running this code from book1. trying to copy from book1 and then paste it in book2.

Macro3 is to simply copy from region "A3 to F3"

I am getting Runtime error '9'
subcript out of range

Please help!

I understand, I think you guys must be tired of my thread but I would request not to. I need help;)

Pedie

Code:
Sub Test2()
    Dim wb As Workbook, MyFile As String
    MyFile = "C:\book2.xls"
    On Error Resume Next
        Set wb = Workbooks.Open(Filename:=MyFile[COLOR=red], Password:="123"[/COLOR]) 
        On Error GoTo 0

    
    If wb Is Nothing Then MsgBox "Couldn't locate " & MyFile: Exit Sub
  
call Macro3
    
With wb
       With sheet1
            .Range("A3").Select
             Do Until ActiveCell.Value = ""
             ActiveCell.Offset(1, 0).Select
             Loop
             ActiveSheet.Paste            
 
            End With
        .Save
        .Close
    End With
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Now I am gettng runtime error '1004'
pastespecial method of range class failed
Code:
Sub Test2()
    
    Application.ScreenUpdating = False
    ThisWorkbook.Sheets("Sheet1").Range("A3:F3").Copy
    Dim wb As Workbook, MyFile As String
    MyFile = "C:\book2.xls"
    On Error Resume Next
        Set wb = Workbooks.Open(Filename:=MyFile, Password:="123")
        On Error GoTo 0
    If wb Is Nothing Then MsgBox "Error occured": Exit Sub
        
    With wb
        .Unprotect Password:="123"
    With Sheets("Sheet1")
            .Range("A3").Select
             Do Until ActiveCell.Value = ""
             ActiveCell.Offset(1, 0).Select
             Loop
             Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
             
                       
        End With
            .Save
            .Close
    End With
    ThisWorkbook.Sheets("Sheet1").Select
    Application.CutCopyMode = False
    Cells(1).Select
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
pedie

You haven't copied anything. That's why the pastespecial fails.
 
Upvote 0
pedie

You haven't copied anything. That's why the pastespecial fails.

I thought the code below has already copied to be pasted...:biggrin:
Vog, so what do i do?:)
Code:
ThisWorkbook.Sheets("Sheet1").Range("A3:F3").Copy
 
Upvote 0
This code works fine. but I dont know how to apply this in my code...:confused:
Code:
Sub try()
Windows("Book1.xls").Activate
Sheet1.Range("A3:F3").Copy
    Windows("Book2.xls").Activate
    Sheets("Sheet2").Select
    cells(1).select
    ActiveSheet.Paste
    Windows("Book1.xls").Activate
    Application.CutCopyMode = False
    Range("A3").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top