Error copying cells using PasteSpecial method - excel

Set objExcel = createObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\deven.kamlesh.jain\Desktop\abcd.xlsx")
iRow = objworkbook.sheets("Sheet1").Usedrange.rows.Count
objworkbook.Sheets("sheet1").Range("A1:C"& iRow).Copy
objExcel.visible = true
Set Obj1 = objexcel.workbooks.add()
objExcel.displayalerts = false
Obj1.saveas("Copied abcd")
Obj1.Sheets("Sheet1").Range("A1").PasteSpecial
I am trying to copy one file to another, but I get an error saying that the PasteSpecial method of the Range class failed.

The reason why PasteSpecial fails with the observed error is because the SaveAs operation clears the copied data. Move the SaveAs after the PasteSpecial and move the Copy right before the PasteSpecial. I would also recommend to put general configuration properties like Visible or DisplayAlerts right after the instantiation of the application object.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open _
("C:\Users\deven.kamlesh.jain\Desktop\abcd.xlsx")
Set Obj1 = objExcel.Workbooks.Add
iRow = objWorkbook.Sheets("Sheet1").UsedRange.Rows.Count
objWorkbook.Sheets("sheet1").Range("A1:C"& iRow).Copy
Obj1.Sheets("Sheet1").Range("A1").PasteSpecial
Obj1.SaveAs "Copied abcd"

Related

How to Copy specific range column values from one excel 365 file to another using VBScript? [duplicate]

This question already has answers here:
Why does "Paste Method of Worksheet class failed" occasionally occur?
(4 answers)
Runtime error 1004 : paste method of worksheet class failed
(4 answers)
Closed 9 months ago.
start edit 20220610
I'm trying to copy specific column values (range "M:P") from one excel 365 file to the other on the range "M:P", using VBScript.
Master.xlsx file structure
Copy_2022.xlsx file structure (after the copy columns from Master.xlsx)
The copy of specific column values working correctly from Master.xlsx file to Copy_2022.xlsx file.
But the problem is that the copied columns values start from cell M1 and not from cell M4 on Copy_2022.xlsx file.
This is what I've tried.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Master.xlsx")
Set objWorkbook2 = objExcel.Workbooks.Open("C:\Copy_2022.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
objWorkbook2.Worksheets(1).UnProtect
Set objRange = objWorkSheet.Range("M:P").EntireColumn
objRange.Copy
Set objWorksheet2 = objWorkbook2.Worksheets(1)
objWorksheet.Activate
Set objRange = objWorkSheet2.Range("M:P")
objWorksheet.Paste(objRange)
objWorkbook2.Save
objWorkbook2.Close
objWorkbook.Close
objExcel.Quit
Set objExcel = Nothing
Any suggestion?
end edit 20220610
I'm trying to copy specific column values (range "M:P") from one excel 365 file to the other on the range "M:P", using VBScript.
This is what I've tried.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Master.xlsx")
Set objWorkbook2 = objExcel.Workbooks.Open("C:\Copy_2022.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
Set objRange = objWorksheet.Range("M:P").EntireColumn
objRange.Copy
Set objWorksheet2 = objWorkbook2.Worksheets(1)
objWorksheet2.Activate
objWorksheet.Range("M:P").EntireColumn.Copy objWorksheet2.Paste
objWorksheet2.Range("M:P")
objWorkbook2.Save
objWorkbook2.Close
When the code reaches the below line I am getting the below error.
objWorksheet.Range("M:P").EntireColumn.Copy objWorksheet2.Paste
Run Time Error '1004': Paste Method Of worksheet Class Failed error
Any suggestion?
Thanks in advance for any help.
Really appreciated
edit
New version same error
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Master.xlsx")
Set objWorkbook2 = objExcel.Workbooks.Open("C:\Copy_2022.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
Set objRange = objWorksheet.Range("M:P").EntireColumn
objRange.Copy
Set objWorksheet2 = objWorkbook2.Worksheets(1)
objWorksheet2.Activate
objWorksheet.unprotect
objWorksheet2.unprotect
objWorksheet.Range("M:P").EntireColumn.Copy objWorksheet2.Paste
objWorksheet2.Range("M:P")
objWorksheet.protect
objWorksheet2.protect
objWorkbook2.Save
objWorkbook2.Close

Getting error on closing excel macro using vbscript

Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C: \Net_Zero_Final.xlsm",0,True)
ObjExcel.Visible = True
ObjExcel.Run "Net_Zero"
ObjExcel.application.quit ' <--- error here
Set ObjWB = Nothing
Set ObjExcel = Nothing
Getting error on the BOLD Part
Can anyone please suggest. tried everything to fix this.
I think you are trying to run a VBA Macro from a .vbs file (or any other location).
I tested the following code inside test.vbs.
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C:\Users\PC\Downloads\TestMacro2.xlsm", False, False)
ObjExcel.Visible = True
ObjExcel.Application.Run "TestMacro2.xlsm!TheMacro"
ObjWB.Close
Set ObjWB= Nothing
Set ObjExcel = Nothing
Where the macro inside the Excel file TestMacro2.xlsm was pretty simple
Sub TheMacro()
MsgBox ("Hello")
End Sub
It worked without any problems.
Notice that instead of trying to close Excel application (ObjExcel.Application.Quit), I just close the opened Workbook (ObjWB.Close). This will avoid the script to close Excel while the user is working in another project.
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C: \Net_Zero_Final.xlsm",0,True)
ObjExcel.Visible = True
ObjExcel.Run "Net_Zero"
ObjExcel.DisplayAlerts = False
ObjWB.Close = False
ObjExcel.quit
Set ObjWB = Nothing
Set ObjExcel = Nothing
It worked like this, I just passed ObjExcel.DisplayAlerts = False
it won't display any alerts and it will simply close the excel without saving it.
i hope this is right.

Getting AdvancedFilter method of range class failed error while coping unique data from one excel to another

I have attached the snippet of the excel file where column B is the desired Output. This similar logic worked in macro but as per our requirement, we need to build a VBScript for it. Below is my code
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
Set inputExcelWorkbook = oExcel.Workbooks.Open("C:\...\Test1.xlsx")
Set inputExcelSheet = inputExcelWorkbook.ActiveSheet
inputExcelSheet.Range("A2:A11").AdvancedFilter _
xlFilterCopy,,inputExcelSheet.Range("B2"),True
inputExcelWorkbook.save
inputExcelWorkbook.close
Snippet of the Excel File
So, My issue was resolved when instead of using xlFilterCopy , I used "2" as per #nick.McDermaid suggestion
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
Set inputExcelWorkbook = oExcel.Workbooks.Open("C:\...\Test1.xlsx")
Set inputExcelSheet = inputExcelWorkbook.ActiveSheet
inputExcelSheet.Range("A2:A11").AdvancedFilter _
2,,inputExcelSheet.Range("B2"),True
inputExcelWorkbook.save
inputExcelWorkbook.close

Selection.End(xlToRight) does not work

I am having a rough time getting this VBscript line to work with the excel object:
set fso=CreateObject("Scripting.FileSystemObject")
Set WShell = CreateObject("WScript.Shell")
Set objExcel = createobject("Excel.application")
objexcel.Visible = true
objexcel.Application.ScreenUpdating = True
objexcel.Workbooks.Open dir & masterFileName
objexcel.Activeworkbook.Worksheets("xActive_User_Ratio").Activate
objexcel.Range("A1").Select
objexcel.Range(Selection, Selection.End(xlToRight)).Select
when I run this code I get an error:
Object required: 'Selection'
What am I doing wrong? Any example would be very much helpful.
Please Help
It's because you are running this from outside Excel.
Use objExcel.Selection instead of just Selection. So that your code knows that Selection is associated with the Excel Application. Additionally, you'll need to define xlToRight or replace it with it's numerical value.
Better yet, I'd use with and rewrite the whole thing like so:
Set fso = CreateObject("Scripting.FileSystemObject")
Set WShell = CreateObject("WScript.Shell")
Set objexcel = CreateObject("Excel.application")
xlToRight = -4161 ' -4161 is the value of xlToRight
With objexcel
.Visible = True
.Application.ScreenUpdating = True
'using variables for workbook and worksheet to be explicit
Set wb = .Workbooks.Open(Dir & masterFileName)
Set ws = wb.Worksheets("xActive_User_Ratio")
ws.Activate
ws.Range("A1").Select
ws.Range(.Selection, .Selection.End(xlToRight)).Select
End With

Transposing Excel through vbscript

I have an Excel spreadsheet that I have exported from some other program.
It has rows that are colored based on few business conditions.
Now I have to transpose the whole excel sheet along with the colors and formatting.
Please note that I have to do this using Vbscript only.
This is the code I've written so far, but this transposes without the formatting:
sub transpose
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.Workbooks.Add()
set table = ActiveDocument.GetSheetObject( "CH01" )
CellRect = ActiveDocument.GetApplication().GetEmptyRect()
CellRect.Top = 0
CellRect.Left = 0
CellRect.Width = table.GetColumnCount
CellRect.Height = table.GetRowCount
set CellMatrix = table.GetCells( CellRect )
for RowIter=CellRect.Top to CellRect.Width-1
for ColIter=CellRect.Left to CellRect.Height-1
ObjExcel.Cells(RowIter+1, ColIter+1).Value = CellMatrix(ColIter)(RowIter).Text
'msgbox(CellMatrix(ColIter)(RowIter).Text)
next
next
objExcel.ActiveWorkbook.SaveAs("C:\Documents and Settings\prasanna\Desktop\test3.xls")
objExcel.Application.Workbooks.Open("C:\Documents and Settings\prasanna\Desktop\test3.xls")
objExcel.Application.Visible = True
objExcel = Nothing
end sub
Phew.., this costed some time and experimenting, here a working solution for office 2012
const xlPasteValuesAndNumberFormats = 12 'doesn't work with Excel 2010 ?
const xlFormats =-4122
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = false
if you allready have your target xls you can skip these lines
Set wbkDest = objExcel.Workbooks.Add
wbkDest.saveAs "c:\test2.xls"
wbkDest.close
and go on here
Set objWorkbook1= objExcel.Workbooks.Open("C:\test1.xls")
Set objWorkbook2= objExcel.Workbooks.Open("C:\test2.xls")
objWorkbook1.Worksheets("Sheet1").UsedRange.Copy
'we have to do the paste twice, once for the values, once for the formats
objWorkbook2.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
objWorkbook2.Worksheets("Sheet1").Range("A1").PasteSpecial xlFormats
objWorkbook1.save
objWorkbook2.save
objWorkbook1.close
objWorkbook2.close
set objExcel=nothing

Resources