|
Sub Data()
Call ShowAllInFilter
Call SaveDataIfNoDuplicates
Call timeDataIfNoDuplicates
Call DeleteData
Call timeDeleteData
End Sub
Sub SaveDataIfNoDuplicates()
' 获取源工作表和目标工作表
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Set wsSource = Sheets("payroll")
Set wsTarget = Sheets("year save")
' 获取目标工作表中的下一空行
Dim nextRow As Long
nextRow = wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Row + 1
' 判断重复项:检查 A 列是否已有相同的数据
Dim cell As Range
Dim hasDuplicate As Boolean
hasDuplicate = False
For Each cell In wsSource.Range("A4:A93")
If WorksheetFunction.CountIf(wsTarget.Range("A:A"), cell.Value) > 0 Then
hasDuplicate = True
Exit For
End If
Next cell
' 如果有重复项,提示用户并退出
If hasDuplicate Then
MsgBox "发现重复数据,保存失败。"
Exit Sub
End If
' 没有重复项,将源工作表 A4:AK93 的数据复制到目标工作表的下一空区域
wsTarget.Range("A" & nextRow & ":AK" & (nextRow + 89)).Value = wsSource.Range("A4:AK93").Value
' 提示用户数据已保存
MsgBox "数据已保存到 Sheet2 的下一行区域。"
End Sub
Sub DeleteData()
Range("E4:E8,AF4:AF98,AK4:AK98").ClearContents
Range("D4").Activate
End Sub
Sub timeDeleteData()
Range("D9:AM10,D13:AM14,D17:AM18,D21:AM22,D25:AM26,D29:AM30,D33:AM34,D37:AM38,D41:AM42,D45:AM46,D49:AM50,D53:AM54,D57:AM58,D61:AM62,D65:AM66").ClearContents
Range("E9").Activate
End Sub
Sub timeDataIfNoDuplicates()
' 获取源工作表和目标工作表
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Set wsSource = Sheets("timesheet")
Set wsTarget = Sheets("Time year")
' 获取目标工作表中的下一空行
Dim nextRow As Long
nextRow = wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Row + 1
' 判断重复项:检查 A 列是否已有相同的数据
Dim cell As Range
Dim hasDuplicate As Boolean
hasDuplicate = False
For Each cell In wsSource.Range("A7:A71")
If WorksheetFunction.CountIf(wsTarget.Range("A:A"), cell.Value) > 0 Then
hasDuplicate = True
Exit For
End If
Next cell
' 如果有重复项,提示用户并退出
If hasDuplicate Then
MsgBox "发现重复数据,未保存 timesheet 数据。"
Exit Sub
End If
' 没有重复项,将源工作表 A7:AK71 的数据复制到目标工作表的下一空区域
wsTarget.Range("A" & nextRow & ":AK" & (nextRow + 64)).Value = wsSource.Range("A7:AK71").Value
' 提示用户数据已保存
MsgBox "timesheet 数据已保存到 Time year 的下一行区域。"
End Sub
Sub ShowAllInFilter()
' 检查当前工作表是否有自动筛选启用
If ActiveSheet.AutoFilterMode Then
' 获取有筛选器的区域
With ActiveSheet.AutoFilter
' 遍历每一个已启用的筛选字段
Dim i As Integer
For i = 1 To .Filters.Count
' 如果当前列有筛选条件,清除该列的筛选条件
If .Filters(i).On Then
ActiveSheet.Range(.Range.Address).AutoFilter Field:=i
End If
Next i
End With
End If
End Sub
**************************
请老师看看下面的代码
Sub Data()
Call ShowAllInFilter
Call SaveDataIfNoDuplicates
Call timeDataIfNoDuplicates
Call DeleteData
Call timeDeleteData
End Sub
只有 执行了 Call ShowAllInFilter
其他下面的都没执行
Call SaveDataIfNoDuplicates
Call timeDataIfNoDuplicates
Call DeleteData
Call timeDeleteData
|
|