Click here to Skip to main content
15,881,618 members

Copy Data to other workbook Macro

SebSCO asked:

Open original thread
Hi

I'm using this Macro to copy past data from one workbook to another
but i'm geting a run time error 9 subscript out of Range
on this line :

Set shttocopy = wkbSource.Sheets("sheet3").Range("F65:N89")


Any one with good knowlage of VBA know what mistake i done ??

I just want copy data from one range on sheet3 to another in sheet51 with past special just values

I actualy need to Copy two ranges F65:N89 from sheet3 to sheet51 c8
and f93:m94 in sheet3 to c66 in sheet51


VB
Sub copydata()
Dim wkbSource As Workbook
Dim wkbDest As Workbook
Dim shttocopy As Worksheet
Dim wbname As String

' check if the file is open
ret = Isworkbookopen("C:\Test\MASTER_ROTA.xls")
If ret = False Then
' open file
Set wkbSource = Workbooks.Open("C:\Test\MASTER_ROTA.xls")
Else
'Just make it active
 'Workbooks("C:\stack\file1.xlsx").Activate
 Set wkbSource = Workbooks("MASTER_ROTA.xls")
 End If

' check if the file is open

ret = Isworkbookopen("C:\Test\WK33.xls")
If ret = False Then
' open file
Set wkbDest = Workbooks.Open("C:\Test\WK33.xls")
Else
'Just make it active
 'Workbooks("C:\stack\file2.xlsx").Activate
 Set wkbDest = Workbooks("WK33.xls")

End If

'perform copy
Set shttocopy = wkbSource.Sheets("sheet3").Range("F65:N89")
shttocopy.Copy: wkbDest.Sheets("Sheet51").Range("C8").PasteSpecial xlPasteValues

End Sub

Function Isworkbookopen(filename As String)
Dim ff As Long, ErrNo As Long
Dim wkb As Workbook
Dim nam As String

wbname = filename
On Error Resume Next

ff = FreeFile()
Open filename For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: Isworkbookopen = False
Case 70: Isworkbookopen = True
Case Else: Error ErrNo
End Select

End Function




Thank You in Advance
Tags: VBA, Microsoft Excel

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900