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

本秂侑毒 提交于 2020-01-06 15:49:26

问题


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


回答1:


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.




回答2:


Change your DataTable.ImportSheet workbook1,"name1","sheet1" call to DataTable.ImportSheet workbook1,"name1","Action1" or to DataTable.ImportSheet workbook1,"name1","Global". Make sure that your path is correct for the workbook and name1 sheet exists in your workbook




回答3:


Are you able to import manually into DataTable? Sometimes, the special characters from the spreadsheet throw error.

If you are receiving "Invalid file error", follow the steps.

1. Open UFT and Activate Data Table and Perform the below action

Perform

  1. Choose the appropriate sheet to be imported.
  2. Check if any "Invalid File Error Dialog". If yes Goto Step 5 else GoTo Step 2
  3. Go back to actual spreadsheet and replace all of the special characters including spaces and clear all the Formatting of the cells


来源:https://stackoverflow.com/questions/40126567/loop-all-the-rows-of-a-worksheet-and-copy-them-into-a-blank-sheet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!