简体   繁体   English

Excel宏导致异常行为

[英]Excel macro causes strange behaviour

Hi there quite new to Excel macros im working on one here that sorts using the autofilter. 嗨,Excel宏中有相当新的内容,我正在使用自动过滤器对其进行排序。 It works fine and does what I want just when I try to resort the data it shows it within the wrong sheet. 当我尝试使用错误的工作表中显示的数据时,它可以正常工作并满足我的要求。 Anyway here is my macro 反正这是我的宏

Sub Hide_Unassigned()
Worksheets("Resource View (2)").Activate
Dim LastRow As Long, c As Range
Application.EnableEvents = False
LastRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row

ActiveWorkbook.Worksheets("Master Data").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Master Data").AutoFilter.Sort. _
    SortFields.Add Key:=Range("Z1:Z200"), SortOn:=xlSortOnValues, Order:= _
    xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Master Data").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
On Error Resume Next
For Each c In Range("D1:D" & LastRow)
If c.Value = "Unassigned" Then
    c.EntireRow.Hidden = True
Else
    c.EntireRow.Hidden = False
End If

Next
    ActiveWorkbook.Worksheets("Master Data").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Master Data").AutoFilter.Sort. _
    SortFields.Add Key:=Range("D1:D200"), SortOn:=xlSortOnValues, Order:= _
    xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Master Data").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
On Error GoTo 0
Application.EnableEvents = True
End Sub

As i said everything works fine except when I attempt to re-sort the data in master data it shows up in the resource view (2) sheet. 正如我所说,一切正常,除了当我尝试对主数据中的数据进行重新排序时,它显示在资源视图(2)工作表中。 It can then be removed by simply dragging over it but I dont think thats sufficient. 然后只需将其拖动即可将其删除,但我认为这还不够。

Thanks in advance for any suggestions or help 预先感谢您的任何建议或帮助

遇到了麻烦,当我访问主数据表本身时,通过简单地移动要调用的宏的最后部分,就设法对自己进行了排序。

Don't use Active(anything) . 不要使用Active(anything) This is what will happen when you do. 这就是您要做的事情。

Add: 加:

Dim MstrDataWS as Worksheet
Dim ResWS as Worksheet
Set ResWS = Worksheets("Resource View (2)")
Set MstrDataWS = ActiveWorkbook.Worksheets("Master Data")

Then, every time you want to reference one worksheet or the other, use MstrDataWS or ResWS , as appropriate. 然后,每次您要引用一个或另一个工作表时,请根据需要使用MstrDataWSResWS That will ensure you're referencing the correct sheet. 这将确保您引用正确的工作表。 It also allows you to eliminate all references to .Activate and .Select . 它还允许您消除对.Activate.Select所有引用。

Also, Cells() and Range() refer to whatever the active sheet is, so you can get lost there. 另外, Cells()Range()引用的是活动工作表,因此您可能会迷路。 Adjust those commands to use ResWS.Cells() or MstrDataWS.Cells() as appropriate. 调整这些命令以适当使用ResWS.Cells()MstrDataWS.Cells()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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