|
楼主 |
发表于 2024-9-30 07:57
来自手机
|
显示全部楼层
按自己理解写了一下,但是还是无法实现,请各位老师如果有时间帮忙修改,再次感谢
Sub SplitWorkbooksByMultipleColumns()
Dim sourceWorkbook As Workbook
Set sourceWorkbook = Workbooks("数据.xlsx") '请修改为实际的工作簿名称
Dim sourceSheet As Worksheet
Set sourceSheet = sourceWorkbook.Sheets("源数据")
Dim lastRow As Long
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "Q").End(xlUp).Row
Dim uniqueQValues As New Collection
Dim i As Long
For i = 2 To lastRow
value = sourceSheet.Cells(i, "Q").Value
On Error Resume Next
uniqueQValues.Add value, CStr(value)
On Error GoTo 0
Next i
Dim desktopPath As String
desktopPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
Dim qValue As Variant
For Each qValue In uniqueQValues
Dim uniqueKValues As New Collection
Dim uniqueMValues As New Collection
Dim uniqueOValues As New Collection
For i = 2 To lastRow
If sourceSheet.Cells(i, "Q").Value = qValue Then
kValue = sourceSheet.Cells(i, "K").Value
mValue = sourceSheet.Cells(i, "M").Value
oValue = sourceSheet.Cells(i, "O").Value
On Error Resume Next
uniqueKValues.Add kValue, CStr(kValue)
uniqueMValues.Add mValue, CStr(mValue)
uniqueOValues.Add oValue, CStr(oValue)
On Error GoTo 0
End If
Next i
Dim kValue As Variant
Dim mValue As Variant
Dim oValue As Variant
For Each kValue In uniqueKValues
For Each mValue In uniqueMValues
For Each oValue In uniqueOValues
Dim newWorkbook As Workbook
Set newWorkbook = Workbooks.Add
Dim newSheet As Worksheet
Set newSheet = newWorkbook.Sheets(1)
newSheet.Name = qValue & "-" & kValue
sourceSheet.Range("A1:Q" & lastRow).AutoFilter Field:=17, Criteria1:=qValue '假设 Q 列是第 17 列
sourceSheet.Range("A1:Q" & lastRow).AutoFilter Field:=11, Criteria1:=kValue '假设 K 列是第 11 列
sourceSheet.Range("A1:Q" & lastRow).AutoFilter Field:=13, Criteria1:=mValue '假设 M 列是第 13 列
sourceSheet.Range("A1:Q" & lastRow).AutoFilter Field:=15, Criteria1:=oValue '假设 O 列是第 15 列
Select Case newSheet.Name
Case qValue & "-" & kValue
newSheet.Range("A1").Value = "对应列 L"
newSheet.Range("A2").Value = sourceSheet.Cells(2, "L").Value
Dim lRow As Long
lRow = 2
For i = 2 To lastRow
If sourceSheet.Cells(i, "Q").Value = qValue And sourceSheet.Cells(i, "K").Value = kValue And sourceSheet.AutoFilterMode Then
lRow = lRow + 1
newSheet.Range("A" & lRow).Value = sourceSheet.Cells(i, "L").Value
End If
Next i
Case qValue & "-" & mValue
newSheet.Range("A1").Value = "对应列 N"
newSheet.Range("A2").Value = sourceSheet.Cells(2, "N").Value
Dim nRow As Long
nRow = 2
For i = 2 To lastRow
If sourceSheet.Cells(i, "Q").Value = qValue And sourceSheet.Cells(i, "M").Value = mValue And sourceSheet.AutoFilterMode Then
nRow = nRow + 1
newSheet.Range("A" & nRow).Value = sourceSheet.Cells(i, "N").Value
End If
Next i
Case qValue & "-" & oValue
newSheet.Range("A1").Value = "对应列 P"
newSheet.Range("A2").Value = sourceSheet.Cells(2, "P").Value
Dim pRow As Long
pRow = 2
For i = 2 To lastRow
If sourceSheet.Cells(i, "Q").Value = qValue And sourceSheet.Cells(i, "O").Value = oValue And sourceSheet.AutoFilterMode Then
pRow = pRow + 1
newSheet.Range("A" & pRow).Value = sourceSheet.Cells(i, "P").Value
End If
Next i
End Select
newWorkbook.SaveAs desktopPath & "\" & qValue & "_folder\" & qValue & "-" & kValue & ".xlsx"
newWorkbook.Close False
MkDir desktopPath & "\" & qValue & "_folder"
Next oValue
Next mValue
Next kValue
Next qValue
sourceSheet.AutoFilterMode = False
End Sub
|
|