步进和运行代码作品。 但是使用命令button无法正常工作

单步执行代码或使用Run Sub / UserFormbutton,代码按预期运行。 但是,当我使用命令button(其中macros分配给)代码将数据粘贴到预先存在的数据而不是悬而未决。

Sub Testing() Application.ScreenUpdating = False Dim tt As Range Dim tt2 As Range Dim LR As Long Set tt = ThisWorkbook.Sheets("Table").UsedRange Set tt2 = ThisWorkbook.Sheets("HAA").UsedRange LR = Range("T" & Rows.Count).End(xlUp).Row tt.AutoFilter tt.AutoFilter 12, "already associated" If Range("A1").Value <> "Notes" Then tt.Copy tt2.Range("B1").PasteSpecial xlPasteValues tt2.Range("A1").Value = "Notes" Else If Range("A1").Value = "Notes" Then tt.Offset(1, 0).Copy With tt2 .Range("A" & LR).Offset(1, 1).PasteSpecial xlPasteValues End With End If End If With tt2 .AutoFilter .Columns.AutoFit .AutoFilter End With With tt2.Font .Name = "Arial" .Size = 8 End With Application.ScreenUpdating = True Sheets("HAA").Activate End Sub 

当你执行:

  LR = Range("T" & Rows.Count).End(xlUp).Row 

你应该告诉VBA是否使用表格(“表格”)表格(“HAA”)

同:

  If Range("A1").Value <> "Notes" Then If Range("A1").Value = "Notes" Then