FonR
FonR

Reputation: 31

Stepping & Running through code works. However using command button does not function properly

Stepping through the code or using the Run Sub/UserForm button, the code functions as intended. However, when I use the Command button (which has the macro assigned to) the code pastes the data over the pre-existing data instead of apending.

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

Upvotes: 0

Views: 44

Answers (1)

Gary&#39;s Student
Gary&#39;s Student

Reputation: 96753

When you execute:

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

You should tell VBA whether to use Sheets("Table") or Sheets("HAA")

same with:

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

Upvotes: 3

Related Questions