我有一个代码,它从表1中获取具有特定值(TI 002768 E2 XA E005)的单元格(E列),并将这些选定的单元格(在B-F列和可变行中)移动到表2。这些单元格有时会有空白值(通常是D列和F列),这些值会传递到Sheet 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
2条答案
按热度按时间nszi6y051#
使用
COUNTA
函数检查源表中的空白单元格fdbelqdn2#
你就快成功了。只需再插入一个if条件,检查Sheet1中的任何空单元格。就像下面的代码。我把“noEmptyCells”函数,返回布尔检查后列“D”和“F”。如果你想检查其他列,只需在noEmptyCells函数中的Array中添加列字母。:)