歡迎stackoverflow.com
你必須換你copy code block
與for loop
,IF-ELSE
聲明和Boolean
類型變量。
首先,你要遍歷您指定的單元格範圍,並確保它們都充滿
Dim allFilled As Boolean
Dim i As Long
For i = 7 To 28 Step 3
If Not IsEmpty(Sheet1.Range("F" & i)) Then
allFilled = True
Else
allFilled = False
End If
Next i
如果他們是你可以用複製粘貼出發,如果他們不程序會顯示一個消息框:Not all the cells are filled! Cant copy
您的完整代碼:
Sub CommandButton3_Click()
Application.ScreenUpdating = False
Dim allFilled As Boolean
Dim i As Long
For i = 7 To 28 Step 3
If Not IsEmpty(Sheet1.Range("F" & i)) Then
allFilled = True
Else
allFilled = False
End If
Next i
If allFilled Then ' = if (allFilled = true) then
Dim NextRow As Range
Sheet1.Range("F7,F10,F13,F16,F19,F22,F25,F28").Copy
Sheets("Overzicht").Select
Set NextRow = ActiveSheet.Cells(Cells.Rows.Count, 1).End(xlUp). _
Offset(1, 0)
NextRow.Select
Selection.PasteSpecial (xlValues), Transpose:=True
MsgBox "Invoer is opgeslagen"
Application.CutCopyMode = False
Application.ScreenUpdating = True
Else
MsgBox "Not all the cells are filled! Cant copy"
End If
End Sub
Update
,從評論:
是的,它可以執行不同的檢查單獨過,例如:
Dim allFilled As Boolean
If Not IsEmpty(Range("F7, F10, F13, F16")) And IsEmpty(Range("F8")) Then
' F7, F10, F13, F16 are not empty and F8 is empty
allFilled = True
ElseIf IsEmpty(Range("F28")) Then
' F28 empty cannot execute copy-paste
allFilled = False
Else
allFilled = False
End If
感謝Mehow,這是非常有幫助的。是否也可以使單元格F16,F19,F22,F25中的一個有條件。那麼,那F7,F10,F13,F28和F16或F19或F22或F25?在複製之前是空的嗎? – user2618164
@ user2618164是的,請參閱更新。 – 2013-07-25 11:41:06
非常感謝! – user2618164