|
本帖最后由 fushuwei 于 2024-11-3 09:59 编辑
[color=rgba(0,0,0,0.85)]代码可以正常生成反馈表并按照要求以班级名称命名放入到班级的文件夹中,但是并没有将午餐的姓名放到午餐区域,晚餐的姓名放入晚托的区域,各位大神帮忙看看问题在哪里,谢谢
[color=rgba(0,0,0,0.85)]
[color=rgba(0,0,0,0.85)][color=rgba(0, 0, 0, 0.85)]Sub GenerateDataAndSave()
Dim arrData As Variant
Dim lastRow As Long
Dim sheetData As Worksheet, sheetFeedback As Worksheet
Dim i As Long
Set sheetData = ThisWorkbook.Sheets("数据表")
Set sheetFeedback = ThisWorkbook.Sheets("反馈表")
' 确定数据表中有数据的行数并读取数据,确保找到真正的最后一行
lastRow = 1
For i = sheetData.Rows.Count To 1 Step -1
If sheetData.Cells(i, "A").Value <> "" Then
lastRow = i
Exit For
End If
Next i
If lastRow > 0 Then
arrData = sheetData.Range("A1:D" & lastRow).Value
End If
' 仅当有足够数据行时进行后续操作,提取日期并格式化
If UBound(arrData) >= 2 Then
Dim rq As Date
rq = arrData(2, 3)
Dim rq1 As String
' 正确的日期范围格式化
rq1 = Format(rq, "yyyy-mm") & "—" & Format(DateAdd("d", -1, DateSerial(Year(rq), Month(rq) + 1, 1)), "yyyy-mm-dd")
' 创建字典存储数据
Dim dataDict As Object
Set dataDict = CreateObject("scripting.dictionary")
For i = 2 To UBound(arrData)
dataDict(arrData(i, 1)) = dataDict(arrData(i, 1)) & "|" & arrData(i, 2) & "|" & arrData(i, 4)
Next i
' 处理字典数据并填入反馈表
Dim keysArr As Variant, itemsArr As Variant
keysArr = dataDict.keys
itemsArr = dataDict.items
Dim k As Long
For k = 0 To UBound(keysArr)
Dim tempArr As Variant
tempArr = Split(itemsArr(k), "|")
Dim n As Integer
n = 0
Dim l As Integer
l = 1
Dim resultArr As Variant
ReDim resultArr(1 To 23, 1 To 12)
Dim j As Long
For j = 0 To UBound(tempArr) Step 2
' 添加数据验证,确保餐次信息正确且不超出tempArr范围
If j + 2 <= UBound(tempArr) Then
If tempArr(j + 1) = "午餐" Or tempArr(j + 1) = "晚餐" Then
If tempArr(j + 1) = "午餐" Then
n = n + 1
If n <= 23 Then
resultArr(n, l) = tempArr(j + 2)
l = l + 2
End If
ElseIf tempArr(j + 1) = "晚餐" Then
n = n + 1
If n <= 23 Then
resultArr(n, 7) = tempArr(j + 2)
End If
End If
End If
End If
Next j
Dim bt As String
bt = keysArr(k)
sheetFeedback.Cells(2, "A") = "班级:" & bt & " " & "班主任:" & " " & rq1
If UBound(resultArr, 1) >= 4 And UBound(resultArr, 2) >= 1 Then
sheetFeedback.Cells(4, "A").Resize(23, 6) = Application.Index(resultArr, Evaluate("row(1:23)"), Evaluate("column(A:F)"))
End If
If UBound(resultArr, 1) >= 4 And UBound(resultArr, 2) >= 7 Then
sheetFeedback.Cells(4, "G").Resize(23, 6) = Application.Index(resultArr, Evaluate("row(1:23)"), Evaluate("column(G:L)"))
End If
Call SaveAsFile(bt) '明确使用Call关键字调用过程,确保名称正确且无作用域问题
Next k
End If
End Sub
Sub SaveAsFile(ByVal bt As String) '添加ByVal关键字,明确参数传递方式,也可避免一些潜在问题
On Error GoTo ErrorHandler
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("反馈表")
ws.Copy
Dim folderName As String
folderName = Left(bt, 3)
If Not FolderExists(ThisWorkbook.Path & "\" & folderName) Then
MkDir ThisWorkbook.Path & "\" & folderName
End If
ActiveWorkbook.SaveAs fileName:=ThisWorkbook.Path & "\" & folderName & "\" & bt & ".xlsx"
ActiveWorkbook.Close SaveChanges:=False
Set ws = Nothing
Exit Sub
ErrorHandler:
ActiveWorkbook.Close SaveChanges:=False
MsgBox "Error: " & Err.Description
End Sub
Function FolderExists(folderPath As String) As Boolean
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
FolderExists = fso.FolderExists(folderPath)
End Function
修改代码生成数据部分使数据能够正确填入反馈表中
|
|