VBScript仅复制指定的列

时间:2014-04-15 12:54:37

标签: excel vbscript

我想将同一工作簿的多个Excel工作表中的前4列的所有行复制到另一个工作簿的单个工作表(仅复制一次头文件)。 下面是我为此编写的代码,但它正在复制整个范围。我无法专门复制前4列。 请帮助。

on error resume next
set objexcel = createobject ("Excel.Application")

Dim objexcel
dim objworkbook1, objworkbook2, objworksheet
dim i,startrow,intnewrow,endrow,countsheet
dim Firstcell,lastcell
strpath = "C:\Documents and Settings\SupriyaS\Desktop\Copy of Movement Report"

Set objWorkbook2= objExcel.Workbooks.Add()
objWorkbook2.SaveAs("C:\Documents and Settings\SupriyaS\Desktop\Master Dump")

set objworkbook1 = objexcel.workbooks.open(strpath)

countSheet = objworkbook1.Sheets.Count

intnewrow=1
i = 1

for i = 1 to countsheet
wscript.echo i

Set objWorksheet = objWorkbook1.WorkSheets(i)
objworksheet.Activate

'copy from the 2nd row 
If intNewRow = 1 Then
startrow = 1
Else
startrow = 2
End If

'count the number of used row
endrow = objWorkbook1.Worksheets(i).UsedRange.Rows.Count

'copy the data
objWorkbook1.Worksheets(i).Range(startrow &":"& endrow).select
objexcel.selection.copy

'paste it on workbook2
objWorkbook2.Worksheets("Sheet1").Cells(intNewRow,1).Pastespecial

'increment the row
intNewRow = intNewRow + (endrow - startrow + 1)

next

objworkbook1.close
objworkbook2.save
objworkbook2.close

msgbox "Done"

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的那样,改变

objWorkbook1.Worksheets(i).Range(startrow &":"& endrow).select
objexcel.selection.copy 

objWorkbook1.Worksheets(i).Range("A" & startrow &":D"& endrow).Copy

阅读

也会对你有所帮助