Loop all the rows of a worksheet and copy them into a blank sheet

0
0

I’m facing a serious problem while importing my script into UFT for more than 2 weeks, I tried everything. As a worarround, I’m cpying the workbook and then I import the new on but this sometimes doesn’t work too.
this is my code:

DataTable.ImportSheet workbook1,"name1","sheet1"

this is my workarround:

On error resume next
DataTable
.ImportSheet workbook_path,"name1","sheet1"
MsgBox
"Error: " & Err.Number & " (" & Err.Source & ") - " & Err.Description
If Err.Number <> 0 Then
If err.number = 20012 Then
Set objExcel1 = CreateObject("Excel.Application")
objExcel1
.Visible = False
objExcel1
.DisplayAlerts=False
Dim RelativePath
RelativePath
= "C:\xyz\new_workbook.xls"
Dim objSheet1
Set objWorkbook1= objExcel1.Workbooks.Open("workbook.xls")
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FileExists(RelativePath) Then
filesys
.DeleteFile RelativePath
End If
Set objWorkbook2=objExcel1.Workbooks.Add
objWorkbook2
.saveAs RelativePath
For each objsheet1 in objworkbook1
objworkbook2
.AddSheet objsheet1.Name
objsheet1
.copy objworkbook2.sheets(1)
Next
objWorkbook2
.save
objworkbook1
.close
objworkbook2
.close
objExcel1
.Quit
Set objSheet1 = Nothing
Set objWorkbook1 = Nothing
Set objWorkbook2 = Nothing
Set objExcel1 = Nothing
On error resume next
DataTable
.ImportSheet RelativePath,"name1","sheet1"
MsgBox
"Error: " & Err.Number & " (" & Err.Source & ") - " & Err.Description
End if
End If

I want to try looping all the rows of the sheets and copying them into the new ones instead of copying them directly. Any help please ? if anyone has other solution to solve this issue, pleeeeeeease help

  • You must to post comments
0
0

Why loop through the rows if you want them all? Just copy the sheet. IF you need the code for that, fire up the macro recorder, copy the sheet and stop the macro recorder.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website