|
发表于 2025-9-15 10:28:40
|
显示全部楼层
你的需求已理解。以下是修改后的VBA代码,现在支持用户输入大写字母(如"A,B,H,M")来指定要跳过的列:
- Sub 报表汇总()
- Dim folderPath As String
- Dim baseFile As String
- Dim skipRows As String
- Dim skipColsLetters As String
- Dim fileCount As Integer
-
- ' 步骤1:选择文件夹并搜索xlsx文件
- With Application.FileDialog(msoFileDialogFolderPicker)
- .Title = "请选择包含报表的文件夹"
- If .Show = -1 Then
- folderPath = .SelectedItems(1)
- Else
- MsgBox "未选择文件夹,操作已取消。"
- Exit Sub
- End If
- End With
-
- ' 搜索xlsx文件
- Dim fileList As String
- Dim fileName As Variant
- fileName = Dir(folderPath & "\*.xlsx")
- Do While fileName <> ""
- fileList = fileList & fileName & vbCrLf
- fileName = Dir
- Loop
-
- If fileList = "" Then
- MsgBox "在指定文件夹中未找到任何xlsx文件。"
- Exit Sub
- End If
-
- MsgBox "找到以下xlsx文件:" & vbCrLf & fileList
-
- ' 步骤2:选择基表
- baseFile = InputBox("请输入作为基表的文件名(例如:123.xlsx)", "选择基表")
- If baseFile = "" Then
- MsgBox "未选择基表,操作已取消。"
- Exit Sub
- End If
-
- ' 复制基表
- Workbooks.Open folderPath & "" & baseFile
- ActiveWorkbook.SaveCopyAs folderPath & "\汇总表.xlsx"
- Workbooks.Open folderPath & "\汇总表.xlsx"
- Dim summaryBook As Workbook
- Set summaryBook = ActiveWorkbook
-
- ' 步骤3:输入跳过行列
- skipRows = InputBox("请输入不参与统计的行号(用逗号分隔,如:1,2,3,9,11)", "跳过行")
- skipColsLetters = InputBox("请输入不参与统计的列字母(用逗号分隔,如:A,B,H,M)", "跳过列")
-
- ' 将列字母转换为列号
- Dim skipColsArray As Variant
- Dim skipCols As String
- skipColsArray = Split(skipColsLetters, ",")
- For i = LBound(skipColsArray) To UBound(skipColsArray)
- skipCols = skipCols & Range(skipColsArray(i) & "1").Column & ","
- Next i
- skipCols = Left(skipCols, Len(skipCols) - 1) ' 移除最后一个逗号
-
- ' 步骤4:遍历并累加数据
- fileCount = 0
- fileName = Dir(folderPath & "\*.xlsx")
- Do While fileName <> ""
- If fileName <> "汇总表.xlsx" And fileName <> baseFile Then
- fileCount = fileCount + 1
- Dim sourceBook As Workbook
- Set sourceBook = Workbooks.Open(folderPath & "" & fileName)
-
- Dim srcSheet As Worksheet
- Dim dstSheet As Worksheet
- Set srcSheet = sourceBook.Worksheets(1)
- Set dstSheet = summaryBook.Worksheets(1)
-
- Dim row As Long, col As Long
- For row = 1 To srcSheet.UsedRange.Rows.Count
- If InStr("," & skipRows & ",", "," & CStr(row) & ",") = 0 Then ' 检查是否跳过该行
- For col = 1 To srcSheet.UsedRange.Columns.Count
- If InStr("," & skipCols & ",", "," & CStr(col) & ",") = 0 Then ' 检查是否跳过该列
- Dim srcValue As Variant
- srcValue = srcSheet.Cells(row, col).Value
-
- If IsNumeric(srcValue) Then
- dstSheet.Cells(row, col).Value = dstSheet.Cells(row, col).Value + srcValue
- ElseIf srcValue = "" Then
- ' 空值用0代替,但不在汇总表中加0(保持原值)
- If dstSheet.Cells(row, col).Value = "" Then
- dstSheet.Cells(row, col).Value = 0
- End If
- End If
- End If
- Next col
- End If
- Next row
-
- sourceBook.Close SaveChanges:=False
- End If
- fileName = Dir
- Loop
-
- ' 步骤5:显示统计结果
- summaryBook.Save
- MsgBox "统计完成!共累加了 " & fileCount & " 个xlsx表格。"
- End Sub
复制代码
主要修改内容:
1. 添加了将列字母转换为列号的功能:
- ' 将列字母转换为列号
- Dim skipColsArray As Variant
- Dim skipCols As String
- skipColsArray = Split(skipColsLetters, ",")
- For i = LBound(skipColsArray) To UBound(skipColsArray)
- skipCols = skipCols & Range(skipColsArray(i) & "1").Column & ","
- Next i
- skipCols = Left(skipCols, Len(skipCols) - 1) ' 移除最后一个逗号
-
复制代码
2. 改进了行列跳过检查逻辑,使用更精确的逗号分隔匹配:
- If InStr("," & skipRows & ",", "," & CStr(row) & ",") = 0 Then
- If InStr("," & skipCols & ",", "," & CStr(col) & ",") = 0 Then
-
复制代码
使用说明:
1. 当提示输入跳过的列时,请输入大写字母,如:A,B,H,M
2. 支持单个字母列(A-Z)和双字母列(AA-ZZ)
3. 行号仍然使用数字输入,如:1,2,3,9,11
这个修改后的代码会让用户输入列字母更加直观,同时保持了原有的功能。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|