Vba 按按钮运行时代码失败

Vba 按按钮运行时代码失败,vba,Vba,我有一个连接到按钮的调用序列中的这个过程。当按钮调用时,这并不总是起作用,但当单独运行时,它总是起作用。有没有想过为什么?本程序的目的是分析编号账户并对其进行分类 Sub IndexMatchBsIS() 'declare the variable Dim lr As Long 'create formula to calculate the last row based on number of materials in column J

我有一个连接到按钮的调用序列中的这个过程。当按钮调用时,这并不总是起作用,但当单独运行时,它总是起作用。有没有想过为什么?本程序的目的是分析编号账户并对其进行分类

 Sub IndexMatchBsIS()

        'declare the variable
        Dim lr As Long

        'create formula to calculate the last row based on number of materials in column J

        lr = Sheets("JDE TB").Cells(1048576, 1).End(xlUp).Row

        'declare a variant to store the row count
        Dim x As Variant

        'row that I want to start in
        x = 2

        'declare variables as ranges
        Dim rng As Range, Cell As Range

        'initialize the Range object rng with Range("E2:the last row in column E")
        Set rng = Sheets("JDE TB").Range("n2" & ":" & "n" & lr)


        'Add the For Each Next loop.
        For Each Cell In rng

        'Perform the index match calculation without using the spreadsheet filldown method

    If Cell(x, 3) > 50000 Then

    Cells(x, 14).Value = "IS"
    Else
    Cells(x, 14).Value = "BS"
    End If


        'Add the next row to the current count
        x = x + 1



            'loop to the next cell until complete
            Next Cell

        'exit the function

        Sheets("JDE TB").Range("k1").Value = "Entity"
        Sheets("JDE TB").Range("l1").Value = "ONESHIRE HFM Account"
        Sheets("JDE TB").Range("m1").Value = "ONESHIRE HFM Account Desc."
        Sheets("JDE TB").Range("n1").Value = "BS/IS"
        Sheets("JDE TB").Range("o1").Value = "Lv4 JDE"

    End Sub

我猜一下,你的if语句应该是这样的

  If Cell(x, 3) > 50000 Then
    Cell(x, 14).Value = "IS"
  Else
    Cell(x, 14).Value = "BS"
  End If

看我怎么把每一个单数而不是复数?单元格指的是活动工作簿,单元格变量来自限定范围。

我猜您的if语句应该是这样的

  If Cell(x, 3) > 50000 Then
    Cell(x, 14).Value = "IS"
  Else
    Cell(x, 14).Value = "BS"
  End If
看我怎么把每一个单数而不是复数?单元格引用活动工作簿,单元格变量来自限定范围。

使用偏移量而不是x变量

If Cell.Offset(, 3) > 50000 Then    
    Cell.Offset(, 14).Value = "IS"
Else
    Cell.Offset(, 14).Value = "BS"
End If
使用偏移量而不是x变量

If Cell.Offset(, 3) > 50000 Then    
    Cell.Offset(, 14).Value = "IS"
Else
    Cell.Offset(, 14).Value = "BS"
End If

它不起作用,这是用户可能描述问题的方式。收到这样一封你根本不知道问题出在哪里的电子邮件不是很气愤吗?嗯,这正是你的问题所要做的。请参阅。FWIW我管理的VBIDE加载项OSS项目将标记这些单元格x,14。值分配为对活动工作表的隐式引用,这将告诉您问题所在。它不起作用是您的用户如何描述问题。收到这样一封你根本不知道问题出在哪里的电子邮件不是很气愤吗?嗯,这正是你的问题所要做的。请参阅。FWIW我管理的VBIDE加载项OSS项目会将这些单元格标记为对活动工作表的隐式引用,这会告诉您问题所在。