简体   繁体   中英

Command Button on macro acts differnet than selecting macro from list

I don't understand why this would be a thing but I have a macro that moves data from one worksheet to various other worksheets based on country names. If I select the macro from the command ribbon (developer tab/macros/select from list) the macro works perfectly. If I use a command button with the macro assigned, or use a button_click method that calls the macro, the result is only partial. The macro doesn't complete the job but the code runs all the way through and there are no errors. I can even select the button_click method from the list and it works fine. Why would there be different behavior from a button vs. selecting from the macro list?

Sub MoveButton_Click()

Call MoveDataToWorksheet

End Sub
Sub MoveDataToWorksheet()

Dim i As Variant
Dim pname As String
Dim rng As Range
Dim lastrow As Long
Dim wslastrow As Long
Dim ws As Worksheet
Dim count As Long
Dim rawdata As Worksheet

For Each ws In ThisWorkbook.Worksheets
    If ws.Name = "Raw_Data" Or ws.Name = "Charts" Or _
    ws.Name = "Tables" Then
    'skips the sheets I want to keep
    Else
        wslastrow = ws.Cells(Rows.count, "a").End(xlUp).Row
        If wslastrow >= 5 Then
            ws.Range("a5:r" & wslastrow).Delete
        Else
            ws.Range("a5:r" & 6).Delete
        End If
    End If
Next ws

Set rawdata = ThisWorkbook.Worksheets("Raw_Data")
lastrow = rawdata.Cells(Rows.count, "a").End(xlUp).Row
Set rng = rawdata.Range("a5:a" & lastrow)

For Each i In rng
    pname = Cells(i.Row, "a").Value

    For Each ws In ThisWorkbook.Worksheets
        If pname = ws.Name Then
            wslastrow = ws.Cells(Rows.count, "a").End(xlUp).Row + 1
            i.EntireRow.Copy
            If wslastrow >= 5 Then
                ws.Cells(wslastrow, "a").PasteSpecial
            Else
                ws.Cells(5, "a").PasteSpecial
            End If
        End If
    Next ws

        If pname = "South Carolina" Then
            i.EntireRow.Copy
            wslastrow = ThisWorkbook.Worksheets("SC").Cells(Rows.count, "a").End(xlUp).Row + 1
            If wslastrow >= 5 Then
                ThisWorkbook.Worksheets("SC").Cells(wslastrow, "a").PasteSpecial
            Else
                ThisWorkbook.Worksheets("SC").Cells(5, "a").PasteSpecial
            End If
        End If

        If pname = "Saudi Arabia" Then
            i.EntireRow.Copy
            wslastrow = ThisWorkbook.Worksheets("KSA").Cells(Rows.count, "a").End(xlUp).Row + 1
            If wslastrow >= 5 Then
                ThisWorkbook.Worksheets("KSA").Cells(wslastrow, "a").PasteSpecial
            Else
                ThisWorkbook.Worksheets("KSA").Cells(5, "a").PasteSpecial
            End If
        End If

        If pname = "United Arab Emirates" Then
            i.EntireRow.Copy
            wslastrow = ThisWorkbook.Worksheets("UAE").Cells(Rows.count, "a").End(xlUp).Row + 1
            If wslastrow >= 5 Then
                ThisWorkbook.Worksheets("UAE").Cells(wslastrow, "a").PasteSpecial
            Else
                ThisWorkbook.Worksheets("UAE").Cells(5, "a").PasteSpecial
            End If
        End If
Next i

Call FixWSFormulas

End Sub

To close this question out:

There's an implicit ActiveSheet in pname = Cells(i.Row, "a").Value , which means this is probably running against different sheets depending on whether you run it from the macro list or a command button.

Change to pname = rawdata.Cells(i.Row, "a").Value .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM