[英]Comparing text in multiple cells using vba
可以說我想將這五個單元格一起比較(其中“線”是整數),如果所有單元格中都有這些確切的單詞“不適用”,我希望輸出中的單元格也應為“不適用”我嘗試用.Value替換.Text,但是它也不起作用。 始終存在運行時錯誤“ 13”
Private Sub CommandButton1_Click()
Dim DocNo As Integer
Dim line As Integer
DocNo = (InputBox("Please input document number of record you would like to view"))
line = 1
'Checks the first cell of the results sheet until the number in first cell = the DocNo
'if "DocNo" = the number in the cell, Line is set to its respective y coordinate
Do
line = line + 1
Loop While (DocNo <> Worksheets("Results").Cells(line, 1))
If (Worksheets("Results").Cells(line, 5).Text) And
(Worksheets("Results").Cells(line, 57).Text) And
(Worksheets("Results").Cells(line, 59).Text) And
(Worksheets("Results").Cells(line, 32).Text) And
(Worksheets("Results").Cells(line, 40).Text) = "Not Applicable" Then
Worksheets("Output").Cells(26, 3) = "Not Applicable"
End If
End Sub
如果這是一個愚蠢的錯誤,請原諒我,我才開始使用VBA幾天
稍微格式化會大大幫助您。 確保使用適當的代碼縮進,如下所示。
這是您的代碼的兩個主要問題:
End If
。 此代碼糾正了這些問題:
Private Sub CommandButton1_Click()
Dim DocNo As Integer, line As Integer
DocNo = InputBox("Please input document number of record you would like to view")
line = 1
' Checks the first cell of the results sheet until the number in first cell = the DocNo
' if "DocNo" = the number in the cell, Line is set to its respective y coordinate
Do
line = line + 1
Loop While (DocNo <> Worksheets("Results").Cells(line, 1))
If (Worksheets("Results").Cells(line, 5).Text) And _
(Worksheets("Results").Cells(line, 57).Text) And _
(Worksheets("Results").Cells(line, 59).Text) And _
(Worksheets("Results").Cells(line, 32).Text) And _
(Worksheets("Results").Cells(line, 40).Text) = "Not Applicable" Then
Worksheets("Output").Cells(26, 3) = "Not Applicable"
End If
End Sub
請嘗試以下操作:
If ((Worksheets("Results").Cells(line, 5).Text) = "Not Applicable") And
(Worksheets("Results").Cells(line, 57).Text) = "Not Applicable") And
(Worksheets("Results").Cells(line, 59).Text) = "Not Applicable") And
(Worksheets("Results").Cells(line, 32).Text) = "Not Applicable") And
(Worksheets("Results").Cells(line, 40).Text) = "Not Applicable") Then
Worksheets("Output").Cells(26, 3) = "Not Applicable"
End If
不幸的是,您將不得不以不同的方式來編寫它。
假設您要簡化5個單元格A1,B1,C1,D1和E1中的文本,您可以
IF Range("A1").Value = "Not Applicable" And Range("B1").Value = "Not Applicable" ...
如果我沒記錯的話,Excel有一個本機工作表函數可以執行完全相同的操作-Exact(range,text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.