Asked by duncanb7
at 2024-07-15 06:12:37
Point:250 Replies:5 POST_ID:828358USER_ID:11059
Topic:
Microsoft Development;Microsoft Excel Spreadsheet Software;Visual Basic Programming
My question is the following VBA Excel Code, When I call other excel file from my orginal workbook or activeworkworkbook
and do a copy and paste that is fine for the following code. But when I want to do some paste the copy to offset positon into
the orginal workbook and sheets , it will create
run-time error when I use range(cells(8+a,8), cells(200, 11) instead of range("H8:K200")with a=0 . I create a variabel is want to
do offset paste, for example, a= 10 or 20....It seems when you open other excel file, the VBA could not select back the
orginal worksheet or workbookif using cells() syntax in range. How Can I select file or sheet between orginal one or opening one file or sheet ?
No error:
Workbooks("orginalworkbook").Sheets("orginal-sheet").Range("H8:K200").PasteSpecial xlPasteValues
Have RUn-time error:But Range("H8:K200")=Range(cells(8+a,8,),cells(200,11) if a=0, Right ?
Workbooks("orginalworkbook").Sheets("orginal-sheet").Range(cells(8+a,8,),cells(200,11)).PasteSpecial xlPasteValue
Please advise
Duncan
==================================
Sub Main() 'In orginal workbook and sheet
Dim wkdir as string
Dim datafilename as string
Dim sheetname as string
Dim a as string
a=0
wkdir = "d:"
datafilename="myexample"
sheetname="mysheet"
Workbooks.Open Filename:=wkdir & datafilename, ReadOnly:=True
Workbooks(datafilename).Sheets(sheetname).Range("A8:D200)").Select
Selection.copy
Workbooks("orginalworkbook").Sheets("orginal-sheet").Range("H8:K200").PasteSpecial xlPasteValues
'Workbooks("orginalworkbook").Sheets("orginal-sheet").Range(cells(8+a,8,),cells(200,11)).PasteSpecial xlPasteValues and
'runtime error if it is used
ActiveWorkbook.Close False
End sub
and do a copy and paste that is fine for the following code. But when I want to do some paste the copy to offset positon into
the orginal workbook and sheets , it will create
run-time error when I use range(cells(8+a,8), cells(200, 11) instead of range("H8:K200")with a=0 . I create a variabel is want to
do offset paste, for example, a= 10 or 20....It seems when you open other excel file, the VBA could not select back the
orginal worksheet or workbookif using cells() syntax in range. How Can I select file or sheet between orginal one or opening one file or sheet ?
No error:
Workbooks("orginalworkbook").Sheets("orginal-sheet").Range("H8:K200").PasteSpecial xlPasteValues
Have RUn-time error:But Range("H8:K200")=Range(cells(8+a,8,),cells(200,11) if a=0, Right ?
Workbooks("orginalworkbook").Sheets("orginal-sheet").Range(cells(8+a,8,),cells(200,11)).PasteSpecial xlPasteValue
Please advise
Duncan
==================================
Sub Main() 'In orginal workbook and sheet
Dim wkdir as string
Dim datafilename as string
Dim sheetname as string
Dim a as string
a=0
wkdir = "d:"
datafilename="myexample"
sheetname="mysheet"
Workbooks.Open Filename:=wkdir & datafilename, ReadOnly:=True
Workbooks(datafilename).Sheets(sheetname).Range("A8:D200)").Select
Selection.copy
Workbooks("orginalworkbook").Sheets("orginal-sheet").Range("H8:K200").PasteSpecial xlPasteValues
'Workbooks("orginalworkbook").Sheets("orginal-sheet").Range(cells(8+a,8,),cells(200,11)).PasteSpecial xlPasteValues and
'runtime error if it is used
ActiveWorkbook.Close False
End sub