|
各位大咖,遇到了区域单元格内容删除的Bug,特来请教!
下面红色标记的代码,运行完后,指定的区域单元格内容并未被删除!
Private Sub CommandButton1_Click()
'定义变量
Dim x As Long
Dim y As Long
Dim i As Long
Dim j As Long
Dim m As Long
Dim n As Long
Dim c As Long
Dim RowsCount1 As Long
Dim RowsCount2 As Long
Dim ColumnsCount1 As Long
Dim ColumnsCount2 As Long
Dim StartColumn
Dim EndColumn
Dim arr1 As String
Dim arr2 As String
Dim StartDate As Date
Dim EndDate As Date
Dim PIDate As Date
Dim PullDate As Date
Dim t
Dim TargetDate As Date
Dim wb1 As Workbook
Dim wb2 As Workbook '定义工作簿变量wb1、wb2
Dim sh As Worksheet '定义工作表变量sh
Dim Dict As Object
Dim Dict1 As Object
Dim Dict2 As Object
'赋值变量
Set Dict = CreateObject("Scripting.Dictionary") '创建字典对象,并赋值给变量Dict
Set Dict1 = CreateObject("Scripting.Dictionary") '创建字典对象,并赋值给变量Dict1
Set Dict2 = CreateObject("Scripting.Dictionary") '创建字典对象,并赋值给变量Dict2
Set wb2 = Workbooks("W01-W10组PULL期.xlsx")
Set wb1 = Workbooks("PI计划.xlsm") '将工作簿PI 计划赋值给变量wb1
wb1.Activate
For Each sh In Worksheets
MsgBox (sh.Name)
If Left(sh.Name, 1) = "W" Then '取各组sheet表
' Set sh = wb1.Worksheets("W05A")
RowsCount1 = wb1.Sheets(sh.Name).Range("H65536").End(xlUp).row
ColumnsCount1 = wb1.Sheets(sh.Name).Range("XFD2").End(xlToLeft).Column
Debug.Print "RowsCount1=" & RowsCount1
Debug.Print "ColumnsCount1=" & ColumnsCount1
wb1.Activate
wb1.Sheets(sh.Name).Select
Range("L4").Resize(RowsCount1 - 3, ColumnsCount1 - 12).ClearContents
StartDate = Trim(wb1.Sheets(sh.Name).Cells(2, 12).Value)
EndDate = Trim(wb1.Sheets(sh.Name).Cells(2, ColumnsCount1).Value)
Debug.Print "StartDate=" & StartDate
Debug.Print "EndDate=" & EndDate
Dict.RemoveAll
For x = 4 To RowsCount1
arr1 = Trim(wb1.Sheets(sh.Name).Cells(x, 3).Value) & "-" & Trim(wb1.Sheets(sh.Name).Cells(x, 4).Value) _
& "-" & Trim(wb1.Sheets(sh.Name).Cells(x, 5).Value)
' Debug.Print "arr1=" & arr1
Dict.Add arr1, x
Next x
'将PI计划簿sh表中,正常工作日的日期添加入字典Dict2
Dict1.RemoveAll
For y = 12 To ColumnsCount1 Step 3
PIDate = Trim(wb1.Sheets(sh.Name).Cells(2, y).Value)
Dict1.Add PIDate, y
Next y
wb2.Activate
wb2.Sheets(sh.Name).Select
RowsCount2 = wb2.Sheets(sh.Name).Range("A65536").End(xlUp).row
Debug.Print "RowsCount2=" & RowsCount2
ColumnsCount2 = wb2.Sheets(sh.Name).Range("XFD2").End(xlToLeft).Column
Debug.Print "ColumnsCount2=" & ColumnsCount2
'将Pull期簿sh表中,正常工作日的日期添加入字典Dict2
Dict2.RemoveAll
For c = 8 To ColumnsCount2
PullDate = Trim(wb2.Sheets(sh.Name).Cells(2, c).Value)
Dict2.Add PullDate, c
Debug.Print "PullDate=" & PullDate
Next c
' t = Dict2.keys
' wb1.Sheets(sh.Name).Cells(1, 2).Resize(Dict2.Count, 1) = Application.Transpose(t)
' Debug.Print "t=" & t
StartColumn = (Dict2.Item(StartDate)) - 4
EndColumn = (Dict2.Item(EndDate)) - 3
Debug.Print "StartColumn=" & StartColumn
Debug.Print "EndColumn=" & EndColumn
For m = 3 To RowsCount2
arr2 = Trim(wb2.Sheets(sh.Name).Cells(m, 3).Value) & "-" & Trim(wb2.Sheets(sh.Name).Cells(m, 4).Value) & "-" & Trim(wb2.Sheets(sh.Name).Cells(m, 6).Value)
If Dict.exists(arr2) Then
Debug.Print "arr2=" & arr2
For n = StartColumn To EndColumn
If IsNumeric(Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)) = True Then
i = Dict.Item(arr2)
Debug.Print "i=" & i
If Trim(wb1.Sheets(sh.Name).Cells(i, 8).Value) = "洗后查" And Trim(wb1.Sheets(sh.Name).Cells(i, 9).Value) = "洗前钉" _
And Trim(wb1.Sheets(sh.Name).Cells(i, 10).Value) = "不绕" Then
' MsgBox (Trim(wb2.Sheets(sh.Name).Cells(2, n + 4).Value))
TargetDate = Trim(wb2.Sheets(sh.Name).Cells(2, n + 4).Value)
j = Dict1.Item(TargetDate)
Debug.Print "j=" & j
wb1.Sheets(sh.Name).Cells(i, j).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
ElseIf Trim(wb1.Sheets(sh.Name).Cells(i, 8).Value) = "洗后查" And Trim(wb1.Sheets(sh.Name).Cells(i, 9).Value) = "洗前钉" _
And Trim(wb1.Sheets(sh.Name).Cells(i, 10).Value) = "绕" Then
TargetDate = Trim(Cells(2, n + 4).Value)
j = Dict1(TargetDate).Item
wb1.Sheets(sh.Name).Cells(i, j).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
wb1.Sheets(sh.Name).Cells(i, j - 1).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
ElseIf Trim(wb1.Sheets(sh.Name).Cells(i, 8).Value) = "洗后查" And Trim(wb1.Sheets(sh.Name).Cells(i, 9).Value) = "洗后钉" _
And Trim(wb1.Sheets(sh.Name).Cells(i, 10).Value) = "不绕" Then
TargetDate = Trim(Cells(2, n + 4).Value)
j = Dict1(TargetDate).Item
wb1.Sheets(sh.Name).Cells(i, j).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
wb1.Sheets(sh.Name).Cells(i, j + 1).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
ElseIf Trim(wb1.Sheets(sh.Name).Cells(i, 8).Value) = "洗后查" And Trim(wb1.Sheets(sh.Name).Cells(i, 9).Value) = "洗后钉" _
And Trim(wb1.Sheets(sh.Name).Cells(i, 10).Value) = "绕" Then
TargetDate = Trim(Cells(2, n + 4).Value)
j = Dict1(TargetDate).Item
wb1.Sheets(sh.Name).Cells(i, j).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
wb1.Sheets(sh.Name).Cells(i, j + 1).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
wb1.Sheets(sh.Name).Cells(i, j + 2).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
ElseIf Trim(wb1.Sheets(sh.Name).Cells(i, 8).Value) = "洗前查" And Trim(wb1.Sheets(sh.Name).Cells(i, 9).Value) = "洗前钉" _
And Trim(wb1.Sheets(sh.Name).Cells(i, 10).Value) = "绕" Then
TargetDate = Trim(Cells(2, n + 3).Value)
j = Dict1(TargetDate).Item
wb1.Sheets(sh.Name).Cells(i, j + 2).Value = Trim(wb2.Sheets(sh.Name).Cells(m, n).Value)
End If
End If
Next n
End If
Next m
End If
Next sh
End Sub
|
|