我有一个代码,它从工作表 1 中获取具有特定值 (TI002768E2XA E005) 的单元格(E 列),并将这些选定的单元格(B-F 列和变量行中)移动到工作表 2。这些单元格有时会有空白值(通常是列) D 和 F) 延续到工作表 2,但我不想要包含空白单元格的行。如何避免将带有空白单元格的行从工作表 1 移动到工作表 2?感谢您的帮助!
Sub SAPDashboard()
Dim LastRow As Long
Dim myRow As Long
Dim myCopyRow As Long
Dim LastRow1 As Long
myCopyRow = 3
LastRow = Sheets("Sheet1").Cells(Rows.Count, "E").End(xlUp).Row
Application.ScreenUpdating = False
For myRow = 1 To LastRow
If Sheets("Sheet1").Cells(myRow, "E") = "TI002768E2XA E005" Then
Sheets("Sheet2").Cells(myCopyRow, "B") = Sheets("Sheet1").Cells(myRow, "e")
Sheets("Sheet2").Cells(myCopyRow, "C") = Sheets("Sheet1").Cells(myRow, "d")
Sheets("Sheet2").Cells(myCopyRow, "D") = Sheets("Sheet1").Cells(myRow, "F")
Sheets("Sheet2").Cells(myCopyRow, "E") = Sheets("Sheet1").Cells(myRow, "G")
Sheets("Sheet2").Cells(myCopyRow, "F") = Sheets("Sheet1").Cells(myRow, "H")
myCopyRow = myCopyRow + 1
End If
Next myRow
LastRow1 = Range("b5000").End(xlUp).Row
LastRow = Range("A5000").End(xlUp).Row
Range("E" & LastRow1 + 1).Formula = "=SUM(E3:E" & LastRow1 & ")"
Range("F" & LastRow1 + 1).Formula = "=SUM(F3:F" & LastRow1 & ")"
Columns("A:AB").HorizontalAlignment = xlCenter
Range("B2:F2").Value = Array("Charge Code", "Name", "Title", "Cost", "Hours")
Worksheets("Sheet2").Range("B2:F2").Font.Bold = True
Range("B2:F2").Interior.ColorIndex = 15
Range("B2:F2").EntireColumn.AutoFit
Range("E:E").Style = "Currency"
Set rng = Nothing
With Range("B2:F" & Range("B" & Rows.Count).End(xlUp).Row)
.Borders.ColorIndex = 1
.Borders.Weight = xlThin
.BorderAround , xlThick, 1
.Resize(1).Borders(xlEdgeBottom).Weight = xlThick
End With
End Sub
使用
COUNTA
函数检查源表中是否有空白单元格
Dim srcRange As Range
For myRow = 1 To LastRow
If Sheets("Sheet1").Cells(myRow, "E") = "TI002768E2XA E005" Then
Set srcRange = Sheets("Sheet1").Cells(myRow, "d").Resize(1, 5)
If Application.CountA(srcRange) = 5 Then
Sheets("Sheet2").Cells(myCopyRow, "B") = Sheets("Sheet1").Cells(myRow, "e")
Sheets("Sheet2").Cells(myCopyRow, "C") = Sheets("Sheet1").Cells(myRow, "d")
Sheets("Sheet2").Cells(myCopyRow, "D") = Sheets("Sheet1").Cells(myRow, "F")
Sheets("Sheet2").Cells(myCopyRow, "E") = Sheets("Sheet1").Cells(myRow, "G")
Sheets("Sheet2").Cells(myCopyRow, "F") = Sheets("Sheet1").Cells(myRow, "H")
myCopyRow = myCopyRow + 1
End If
End If
Next myRow