按钮返回有条件格式的顺序数据

时间:2015-12-19 18:53:46

标签: arrays excel vba sorting conditional

需要VBA使用"位置"返回ECR> 30天ECR在。当你点击简单按钮。程序需要扫描Red单元格并创建一个数组并将数组放入另一个工作簿。 enter image description here

到目前为止

代码:

Sub easy_button_2()
Dim rw As Long, c As Long, fast As String, X
fast = "Y"

With Workbooks("ECR Log w_fast.xlsm").Sheets("Sheet 3")

With Workbooks("ECR Log w_fast.xlsm").Sheets("Sheet 2")

    'clear any previous ECR #s/Location results
    rw = Application.Match("ECR #s", .Range(.Cells(3, 1), .Cells(Rows.Count, 1).End(xlUp)), 0)
    With .Range(.Cells(rw + 24, 1), .Cells(Rows.Count, 1).End(xlUp))
        .Resize(.Rows.Count, 2).Offset(1, 0).ClearContents
    End With
    'reset the Locations named range
    With .Range(.Cells(3, 1), .Cells(3, 1).End(xlDown))
        .Resize(.Rows.Count, .Cells(1, Columns.Count).End(xlToLeft).Column).Name = "Locations"
    End With

    'cycle through the ECRs in Locations' column 1
    With .Range("Locations")
        For rw = 2 To .Rows.Count
            If .Cells(rw, 3) > 30 Or .Cells(rw, 2) = fast Then
                For c = 3 To .Columns.Count
                    If .Cells(rw, c).DisplayFormat.Interior.Color = vbRed Then
                            .Parent.Cells(Rows.Count, 1).End(xlUp).Resize(1, 2).Offset(1, 0) = _
                            Array(.Cells(rw, 1).Value2, .Cells(1, c).Value2)
                        Exit For
                    End If
                Next c
            End If
       Next rw
    End With
End With
End With

' Workbooks.Open文件名:=" C:\ Users \ MJ \ Desktop \ ECR Monitor.xlsm" ' ThisWorkbook.Activate 结束子

1 个答案:

答案 0 :(得分:0)

如果我想将此程序运行的值返回到另一个工作表或其他工作簿,该怎么办?我可以在另一个工作簿中引用该数组吗?也许将数组声明为引用它的变量?

或者我是否必须将该数组放在另一个工作表中并引用另一个工作簿?

Sub easy_button_2()
Dim rw As Long, c As Long, fast As String
fast = "Y"
Dim ws3 As Worksheet
Set ws3 = Workbooks("ECR Log w_fast.xlsm").Sheets("Sheet 3")
With Workbooks("ECR Log w_fast.xlsm").Sheets("Sheet 2")

    'clear any previous ECR #s/Location results
    rw = Application.Match("ECR #s", .Range(.Cells(3, 1), .Cells(Rows.Count, 1).End(xlUp)), 0)
    With .Range(.Cells(rw + 100, 1), .Cells(Rows.Count, 1).End(xlUp))
         .Resize(.Rows.Count, 2).Offset(1, 0).ClearContents
    End With
    'reset the Locations named range
    With .Range(.Cells(3, 1), .Cells(3, 1).End(xlDown))
        .Resize(.Rows.Count, .Cells(1, Columns.Count).End(xlToLeft).Column).Name = "Locations"
    End With

    'cycle through the ECRs in Locations' column 1
    With .Range("Locations")
        For rw = 2 To .Rows.Count
            If .Cells(rw, 3) > 30 Or .Cells(rw, 2) = fast Then
                For c = 3 To .Columns.Count
                    If .Cells(rw, c).DisplayFormat.Interior.Color = vbRed Then
                            ws3.Cells(Rows.Count, 1).End(xlUp).Resize(1, 2).Offset(1, 0) = _
                            Array(.Cells(rw, 1).Value2, .Cells(1, c).Value2)
                        Exit For
                    End If
                Next c
            End If
       Next rw
    End With
End With

End Sub