excel - VBA - If 语句引用不同工作表中单元格中的特定文本

标签 excel vba

我正在尝试构建逻辑,如果特定工作表中的特定单元格填充了特定字符串,它会跳过运行该子程序并继续下一个。我认为这是迄今为止我最接近的一些研究尝试:

        Dim Rng_1 As Range
        Dim x_sheet As Worksheet
        Dim Value_X

        Value_X = "<>Could not generate report for*"
        Set x_sheet = ActiveWorkbook.Worksheets("worksheet_name")
        Set Rng_1 = x_sheet.Range("B8")
            If Rng_1.value = Value_X Then
           'run code
        End If
       'It should continue from here if the referenced cell did contain the string.

无论我如何编辑不包含值,或者即使我添加“else if”,运行上面的代码总是会跳过代码。在这一点上,我什至不确定我是否走在正确的轨道上。

Referenced Sheet
enter image description here

最佳答案

Like处理通配符时需要使用比较运算符。 '不匹配'是Not this Like that .您不添加 <>运算符产生“不匹配”。

您需要Else如果你想要A计划和B计划。

    Value_X = "Could not generate report for*"  'removed <>
    Set x_sheet = ActiveWorkbook.Worksheets("worksheet_name")
    Set Rng_1 = x_sheet.Range("B8")
    If Not Rng_1.value Like Value_X Then
       Debug.Print "msg not found"
       'run code
    Else
       Debug.Print "msg found"
       'It should continue from here if the referenced cell did contain the string.
    End If

首先,从您的样本数据看来,您还可以测试 IsNumeric(Rng_1.value) .

关于excel - VBA - If 语句引用不同工作表中单元格中的特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55563340/

相关文章:

c# - 使用 EPPlus 将列或单元格设置为只读

excel - 从 icCube 中的 Web 小部件将数据下载到 Excel

python - 如何使用xlrd写入excel文件

pdf - 以编程方式从 Excel 数据填写 PDF 表单

vba - $符号在VBA中有什么作用?

excel - 如何加快 Excel 中的 RANK() 速度

vba - 找不到VBA SAP偶然错误控件ID

excel - VBA 将 Microsoft Word 书签和选择文本导出到 Excel 电子表格

excel - 自动筛选错误 : the object invoked has disconnected from its clients

vba - 遍历VBA中的指定工作表