|
原本是想着WPS有这个工资条的功能但是只能做一个的,不能有多个数据就自己写了一部分,AI优化了一部分
使用的是最简单的方法,筛选和复制所以会等待比较长时间,肯定有更好的方法,时间原因就随便做了一个,能用就行。分享一下然后看看有没有大佬优化
- Sub ProcessUniqueValues()
- Dim inputColumn As String, activeSheetName As String
- Dim lastRow As Long
-
- inputColumn = InputBox("请输入姓名所在的列(输入字母)")
- activeSheetName = ActiveWorkbook.ActiveSheet.Name
-
- ' 获取最大行
- lastRow = Sheets(activeSheetName).Cells(Rows.Count, 1).End(xlUp).Row
-
- If lastRow < 2 Or Trim(inputColumn) = "" Then
- MsgBox "请检查当前激活工作表数据,参照模板", vbExclamation
- Exit Sub
- End If
-
- Application.ScreenUpdating = False
-
- Call ExtractUniqueValues(inputColumn & "2:" & inputColumn & lastRow, activeSheetName, lastRow)
-
- Rows("1:2").Delete Shift:=xlShiftUp
- Application.ScreenUpdating = True
- Call dy
- End Sub
- Sub ExtractUniqueValues(inputRange As String, activeSheetName As String, lastRow As Long)
- Dim rng As Range
- Dim uniqueValues As Variant
- Dim i As Long
- Dim lastUsedRow As Long
- Dim outputSheet As Worksheet
-
- ' 检查用户输入的范围
- If Trim(inputRange) = "" Then
- MsgBox "您没有输入范围。", vbExclamation
- Exit Sub
- End If
-
- ' 设置范围
- On Error Resume Next
- Set rng = Range(inputRange)
- On Error GoTo 0
-
- If rng Is Nothing Then
- MsgBox "输入的范围无效,请检查后重试。", vbExclamation
- Exit Sub
- End If
-
- ' 创建新工作表
- Set outputSheet = ThisWorkbook.Worksheets.Add
- outputSheet.Name = "成绩条" & Format(Now, "yyyymmddhhmmss")
-
- ' 获取唯一值
- uniqueValues = GetUniqueValues(rng)
-
- ' 过滤和复制数据
- With Sheets(activeSheetName)
- .Range("A1").AutoFilter
- Dim lastColumn As String
- lastColumn = GetLastUsedColumnLetter(activeSheetName)
-
- For i = LBound(uniqueValues) To UBound(uniqueValues)
- .Range("A1:" & lastColumn & lastRow).AutoFilter Field:=2, Criteria1:=Array(uniqueValues(i)), Operator:=xlFilterValues
-
- lastUsedRow = .Cells(Rows.Count, 1).End(xlUp).Row
- .Range("A1:" & lastColumn & lastUsedRow).Copy
-
- Dim outputRow As Long
- outputRow = outputSheet.Cells(Rows.Count, 1).End(xlUp).Row + 3
- outputSheet.Range("A" & outputRow).PasteSpecial
-
- With outputSheet.Range("A" & outputRow + 3 & ":" & lastColumn & outputRow + 3).Borders(xlEdgeBottom)
- .Weight = xlMedium
- .LineStyle = xlDash
- End With
- Next i
-
- .Range("A1").AutoFilter
- End With
- End Sub
- Function GetUniqueValues(rng As Range) As Variant
- Dim cell As Range
- Dim uniqueCollection As Collection
- Dim uniqueArray() As Variant
- Dim i As Long
-
- Set uniqueCollection = New Collection
- On Error Resume Next ' 忽略错误以避免重复值引发的错误
-
- For Each cell In rng
- If Not IsEmpty(cell.Value) Then
- uniqueCollection.Add cell.Value, CStr(cell.Value)
- End If
- Next cell
-
- On Error GoTo 0 ' 恢复默认错误处理
-
- ReDim uniqueArray(0 To uniqueCollection.Count - 1)
- For i = 1 To uniqueCollection.Count
- uniqueArray(i - 1) = uniqueCollection(i)
- Next i
-
- GetUniqueValues = uniqueArray
- End Function
- Function GetLastUsedColumnLetter(sheetName As String) As String
- Dim lastColumn As Long
- lastColumn = Sheets(sheetName).Cells(1, Columns.Count).End(xlToLeft).Column
- GetLastUsedColumnLetter = Split(Cells(1, lastColumn).Address, "$")(1)
- End Function
- Sub dy()
- ActiveSheet.PageSetup.LeftMargin = 18
- ActiveSheet.PageSetup.RightMargin = 18
- ActiveSheet.PageSetup.TopMargin = 54
- ActiveSheet.PageSetup.BottomMargin = 54
- ActiveSheet.PageSetup.HeaderMargin = 21.5
- ActiveSheet.PageSetup.FooterMargin = 21.5
- ActiveSheet.PageSetup.FitToPagesWide = 1
- ActiveSheet.PageSetup.FitToPagesTall = 0
- ActiveSheet.PageSetup.BottomMargin = 42.519685
- ActiveSheet.PageSetup.TopMargin = 39.685039
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|