作者:励志天涯网 | 来源:互联网 | 2023-05-30 12:09
IamfairlynewwithExcelvbabuthavebeenusingaccessvbaforsometimenow.我对Excelvba相当新,但现在已
I am fairly new with Excel vba but have been using access vba for some time now.
我对Excel vba相当新,但现在已经使用访问vba一段时间了。
I have some code which splits a main file into several other files based on a distinct column in excel
我有一些代码可以根据excel中的不同列将主文件拆分成其他几个文件
Sub SplitbyValue()
Dim FromR As Range, ToR As Range, All As Range, Header As Range
Dim Wb As Workbook
Dim Ws As Worksheet
'Get the header in this sheet
Set Header = Range("D8").EntireRow
'Visit each used cell in column D, except the header
Set FromR = Range("D9")
For Each ToR In Range(FromR, Range("D" & Rows.Count).End(xlUp).Offset(1))
'Did the value change?
If FromR <> ToR Then
'Yes, get the cells between
Set All = Range(FromR, ToR.Offset(-1)).EntireRow
'Make a new file
Set Wb = Workbooks.Add(xlWBATWorksheet)
'Copy the data into there
With Wb.ActiveSheet
Header.Copy .Range("A8")
All.Copy .Range("A9")
End With
'Save it
Wb.SaveAs ThisWorkbook.Path & "\" & Format(Date, "yyyy.mm.dd") & _
" - " & FromR.Value & ".xls", xlWorkbookNormal
Wb.Close
'Remember the start of this section
Set FromR = ToR
End If
Next
End Sub
This works great for the main sheet, but have to copy multiple tabs and this only captures one sheet. How can I expand this so it copies the other sheets as well into that file?
这适用于主工作表,但必须复制多个选项卡,这只能捕获一个工作表。如何扩展它以便将其他工作表复制到该文件中?
example: ColumnA Id1 Id2 Id3
示例:ColumnA Id1 Id2 Id3
This creates three files (Id1)(Id2)(Id3) but ignores the other sheets.
这将创建三个文件(Id1)(Id2)(Id3)但忽略其他工作表。
2 个解决方案