[英]Comparing text in multiple cells using vba
lets say i want to compare these five of these cells together (where "line" is an integer) and if all of them have these exact words "Not Applicable" in them, i want the cell in output to also be "Not Applicable" I've tried replacing .Text with .Value but it doesn't work either. 可以说我想将这五个单元格一起比较(其中“线”是整数),如果所有单元格中都有这些确切的单词“不适用”,我希望输出中的单元格也应为“不适用”我尝试用.Value替换.Text,但是它也不起作用。 there's always a Run-time Error "13"
始终存在运行时错误“ 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
If this is some stupid mistake please forgive me, I've only started using VBA for a few days 如果这是一个愚蠢的错误,请原谅我,我才开始使用VBA几天
A little formatting goes a long way. 稍微格式化会大大帮助您。 Make sure to use appropriate code indentation, as shown below.
确保使用适当的代码缩进,如下所示。
Here's the two main problems with your code: 这是您的代码的两个主要问题:
End If
for your If block. End If
。 This code corrects these issues: 此代码纠正了这些问题:
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
Try the following: 请尝试以下操作:
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
You will have to write it in a different way, unfortunately. 不幸的是,您将不得不以不同的方式来编写它。
Say, you have texts in 5 cells, A1, B1, C1, D1 and E1 to simplify, you can either do 假设您要简化5个单元格A1,B1,C1,D1和E1中的文本,您可以
IF Range("A1").Value = "Not Applicable" And Range("B1").Value = "Not Applicable" ...
If I recall correctly, Excel has a native worksheet function doing the exact same thing - Exact( range, text ) 如果我没记错的话,Excel有一个本机工作表函数可以执行完全相同的操作-Exact(range,text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.