2017-06-26 71 views
1

使用Excel文檔,它需要先填充幾個單元格才能保存。我能夠用下面的代碼來解決這個問題。VBA問題 - 試圖使多個單元格爲強制保存Excel文檔

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 
If Cells(6, 5).Value = "" Then 
    MsgBox "Cell E6 requires user input", vbInformation, "Kutools for Excel" 
    Cancel = True 
    Exit Sub 
End If 

If Cells(8, 5).Value = "" Then 
    MsgBox "Cell E8 requires user input", vbInformation, "Kutools for Excel" 
    Cancel = True 
    Exit Sub 
End If 

If Cells(10, 5).Value = "" Then 
    MsgBox "Cell E10 requires user input", vbInformation, "Kutools for Excel" 
    Cancel = True 
    Exit Sub 
End If 

我遇到的問題是如果特定的單元格被填充,那麼在保存文檔之前需要其他單元格。我已經嘗試了isEmpty函數,但它仍然無法正常工作。請幫忙!

+1

'如果Len(Trim(Cells(1,1).Value))<> 0 Then' –

回答

0

使用If Cells(1,1).Value <> ""然後需要像你一直在做的其他單元格。像這樣:

If Cells(1,1).Value <> "" Then 
    If Cells(10, 5).Value = "" Then 
     MsgBox "Cell E10 requires user input", vbInformation, "Kutools for Excel" 
     Cancel = True 
     Exit Sub 
    End If 
End If 
相關問題