参考这段VBA代码:Excel文件1.xls中的每行数据保存到2.xls中的不同工作表中
[Visual Basic] 纯文本查看 复制代码
Sub CopyColumnsToSheets()
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Dim LastColumn As Long
Dim i As Long
Dim sheetName As String
' 打开源文件和目标文件
Set wbSource = Workbooks.Open("C:\Path\To\1.xls")
Set wbTarget = Workbooks.Open("C:\Path\To\2.xls")
Set wsSource = wbSource.Sheets(1) ' 假设数据在第一个工作表
' 获取源工作表的最后一列
LastColumn = wsSource.Cells(1, wsSource.Columns.Count).End(xlToLeft).Column
' 遍历每一列
For i = 1 To LastColumn
' 创建新的工作表
Set wsTarget = wbTarget.Sheets.Add(After:=wbTarget.Sheets(wbTarget.Sheets.Count))
sheetName = "Column" & i ' 给新工作表命名
wsTarget.Name = sheetName
' 复制当前列到新工作表的B列
wsSource.Columns(i).Copy Destination:=wsTarget.Columns(2)
Next i
' 保存并关闭工作簿
wbTarget.Save
wbSource.Close False
wbTarget.Close False
' 清理
Set wsSource = Nothing
Set wsTarget = Nothing
Set wbSource = Nothing
Set wbTarget = Nothing
End Sub
|