[英]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.
如果我 select 命令功能区中的宏(开发人员选项卡/宏/从列表中选择),则宏可以完美运行。 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.
如果我使用分配了宏的命令按钮,或者使用调用宏的 button_click 方法,则结果只是部分的。 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.
我什至可以 select 列表中的 button_click 方法,它工作正常。 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. pname = Cells(i.Row, "a").Value
中有一个隐含的ActiveSheet
,这意味着这可能针对不同的工作表运行,具体取决于您是从宏列表还是命令按钮运行它。
Change to pname = rawdata.Cells(i.Row, "a").Value
.更改为
pname = rawdata.Cells(i.Row, "a").Value
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.