我是VB的新手,我将把它用于Quick Test Pro(QTP)。我有一个问题是将数据从一个Excel复制到另一个具有特定行和列的Excel文件。这是场景:
我需要复制第一行,因为这是我需要的标题/标题。
到目前为止,这就是我所做的。但它只复制单列而不是我想要的列:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook1 = objExcel.Workbooks.Open("C:\Report\CMaxx_File1.xlsx")
ObjExcel.Visible = True
Set objWorkbook2 = objExcel.Workbooks.Open("C:\Report\CMaxx_File2.xlsx")
objWorkbook1.Worksheets("Sheet1").Range("A1:A21").Copy
objWorkbook2.Worksheets("Sheet1").Range("A1:A21").PasteSpecial
objWorkbook2.Save
答案 0 :(得分:0)
仍然不是最好的方法,但我测试了它,它的工作原理。确保更改变量以匹配您的路径,以及列范围
这将采用超过20行的电子表格并创建新的电子表格(根据需要多少),包含1行标题和d19行数据(总共20行)
allRowsWorkbook = "C:\XXXXX\stack\x\Original.xlsx"
newXLS = "C:\XXXXX\stack\x\New_sheet_20_rows"
Set objExcel = CreateObject("Excel.Application")
'Open the workbook that has all the rows
Set objWorkbook = objExcel.Workbooks.Open(allRowsWorkbook)
rowCnt = 2
LineCnt = 0
intRow = 2
bookCnt = 1
'Spreadsheets that will be created
xlsName = newXLS & bookCnt & ".xlsx"
createNewBook(xlsName)
Do Until objExcel.Cells(intRow,1).Value = ""
for lineCnt = 0 to 20
Wscript.Echo "CN: " & objExcel.Cells(intRow, 1).Value
intRow = intRow + 1
lineCnt = LineCnt + 0
rowCnt = rowCnt + 1
next
LineCnt = 0
wscript.echo "Out"
bookCnt = bookCnt + 1
xlsName = newXLS & bookCnt & ".xlsx"
wscript.echo xlsName
createNewBook(xlsName)
Loop
Function createNewBook(xlsName)
Set objExcel2 = CreateObject("Excel.Application")
Set objWorkbook2 = objExcel2.Workbooks.Add()
objWorkbook.Worksheets("Sheet1").Range("A1:F1").Copy
objWorkbook2.Worksheets("Sheet1").Range("A1:F1").PasteSpecial
objWorkbook.Worksheets("Sheet1").Range("A" & rowCnt & ":i" &(rowCnt +20) ).Copy
'sleep was added to make sure copy had tinme to comlete
wscript.sleep 1000
objWorkbook2.Worksheets("Sheet1").Range("A2:i2").PasteSpecial
objWorkbook2.SaveAs(xlsName)
objWorkbook2.Close(xlsName)
End Function