【问题标题】:Comparing two text cells in different sheets比较不同工作表中的两个文本单元格
【发布时间】:2012-05-30 13:57:43
【问题描述】:

我正在尝试比较来自不同工作表的两个文本单元格(如 abcDEF)。一张是固定的,另一张没有。

我的代码是:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim i As Long, LastRow As Long, n As Long
Dim Project As String
Dim Responsible As String, Site As String, Sample As String, _
    Description As String, Parameter As String, Method As String
Dim j As Long

Application.EnableEvents = False

' Find LastRow in Col A into the Sheet2
LastRow = Sheet2.Range("A" & Rows.Count).End(xlUp).Row

' Select all Col A in Project
For Each Value In Sheet2.Range("A2:A" & LastRow)
   Project = Project & "," & Value
Next Value

Sheet1.Range("A2").ClearContents: Sheet1.Range("A2").Validation.Delete

' Create the Data Validation List
With Range("A2").Validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween,     Formula1:=Project
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

' Select the sheet coinciding with the cell "A2" value
For j = 3 To Sheets.Count

    If Sheets(j).Range("A2").Text = Sheets(1).Range("A2").Text Then
        'Write 4 in sheet1 cell C6 when the two values are coinciding.
        Sheet1.Range("C6") = 4
    End If

Next j
End Sub

问题是If... 行,可能是范围定义。我试过.Text.Value 都不管用。

【问题讨论】:

  • 这在 VBA 项目结构中保存在哪里?它应该保存在您要处理更改事件的每个工作表中。另外,你不需要循环,If 语句条件应该是Range("A2").Text = Sheets(1).Range("A2").Text
  • 所以代码会抛出一个错误(哪个和在哪里?)或者条件永远不会评估为 True?
  • @MarkM 我只有在 Sheet1 中有这个。你说的我试过了,还是不行。
  • @PaulB。我可以完美地编译代码。问题是你所说的条件。
  • 尝试切换EnableEvents,正如我在回答中提到的那样,看看是否有效。

标签: excel vba loops compare


【解决方案1】:

你可能想要使用的是

If StrComp(Sheets(j).Range("A2").Value2, Sheets(1).Range("A2").Value2, _
    vbTextCompare) = 0 Then
'added the underscore since I made it two lines for neatness

vbTextCompare 不区分大小写,vbBinaryCompare 区分大小写。网上有几个关于string comparison 的资源可以帮助你。

另外,我注意到您正在使用Worksheet_Change 并更改Sheet1 上单元格的值。我猜你的Worksheet_ChangeSheet1,是吗?如果是这种情况,那么每次修改Sheet1 时,都会再次调用子程序(一次又一次......直到它崩溃)。为了防止这种情况,你想添加

Application.EnableEvents = False

到子的开头,然后

Application.EnableEvents = True

最后。这样您对工作表所做的任何更改都不会触发Worksheet_Change sub。

【讨论】:

  • 你说的我已经试过了。但它仍然无法正常工作......在我在sheet1的单元格“A2”中有一个验证数据列表之前。当我检查这个时,我想将该单元格与 Sheet3 中的其他“A2”单元格与其他工作表进行比较,当它为真时,在 sheet1 中的其他单元格中必须根据工作表显示其他验证数据列表。所有代码都在工作,但我仍然对 If... 有问题,它永远不会实现。
  • 我在循环的上一步完成了一点代码。也许还有另一个我看不到的错误,但我看到的是条件永远不会实现。没有它,其余的都可以正常工作。谢谢!
  • 在你的代码结束时(在 sub 结束之前)你需要再次启用事件 (Application.EnableEvents = True)。此外,由于您已停用它们,因此重新激活它们的简单方法是仅使用以下行制作一个小子/宏:Application.EnableEvents = True。之后,您的 Worksheet_Change 应该会再次成功触发(您当前的代码意味着它仅在第一次更改后运行,然后由于事件被禁用而停止)。当我将您的代码复制到新工作簿中时,您的代码似乎对我很好。只要确保你启用了我所说的事件。
  • 为了清楚起见,创建一个新的 Sub(例如 Private Sub ReEnable()),添加启用事件的行,然后运行该宏一次(F5 或按下编辑器中的按钮)。一旦事件被禁用,只需按下“重置”按钮就不会启用事件。
  • 我尝试在子末尾添加 Application.EnableEvents = True。但是,当我选择列表中的选项时,它们不会停留在单元格上。另外,如果我在实际 sub 的末尾添加一个新的 Sub,它就不起作用。
【解决方案2】:

你可以使用EXACT(text1,text2)函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2015-07-08
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多