循环遍历每个COLUMN并找到突出显示的单元格

时间:2017-02-22 22:20:00

标签: excel vba excel-vba

在循环到下一行之前,我很难遍历每一列。列数是固定的(A:K),行数未知。目标是找到突出显示的单元格(没有明显的颜色..我认为最好的方法是编写“If Not No Fill”)并将整行复制到另一个工作簿。这就是我到目前为止所遇到的问题:

Option Explicit
Sub Approval_Flow()
Dim AppFlowWkb As Workbook, ConfigWkb As Workbook
Dim AppFlowWkst As Worksheet, ConfigWkst As Worksheet
Dim header As Range, headerend As Range
Dim row As Long, column As Long

Set AppFlowWkb = Workbooks.Open("C:\Users\clara\Documents\Templates and Scripts\Approval Flow Change Log.xlsx")
Set ConfigWkb = ThisWorkbook
Set AppFlowWkst = AppFlowWkb.Sheets("Editor")
Set ConfigWkst = ConfigWkb.Worksheets("Approval Flows")

With ConfigWkb
    Set header = Range("A7").Cells
    If Not header Is Nothing Then
        Set headerend = header.End(xlDown).row
    For row = 7 To headerend
        For j = 1 To 11
            'if cell is filled (If Not No Fill), copy that whole row to another workbook

End With


End Sub

我收到了设置headerend行的错误,但我试图选择最后一行在我的for循环中使用它。我感谢任何帮助和指导。提前致谢!

2 个答案:

答案 0 :(得分:0)

你正在混合这些类型。

看起来您只想使用Header数据结束的Row。

取出.Row,因为您将headerend设置为单元格地址,而不是特定值。然后将For row = 7 To headerend更改为For row = 7 To headerend.Row

或者,将Dim Headerend as Range更改为...as Long,然后执行headerEnd = header.End(xlDown).Row(不要使用Set

答案 1 :(得分:0)

您应该能够根据您的工作簿进行调整,查看详细信息的评论

Dim aCell as Range
' Use UsedRange to get the variable number of rows, 
' cycle through all the cells in that range
For Each aCell In ActiveSheet.Range("A1:K" & ActiveSheet.UsedRange.Rows.Count)
    ' Test if fill colour is white (none)
    If Not aCell.Interior.Color = RGB(255,255,255) Then
        ' Insert new row in target sheet (could find last row instead)
        ActiveWorkbook.Sheets("ThisOtherSheet").Range("A1").EntireRow.Insert
        ' Paste entire row into target sheet
        aCell.EntireRow.Copy Destination:=ActiveWorkbook.Sheets("ThisOtherSheet").Range("A1")
    End If
Next aCell

或者找到最后一行,如果您知道范围是连续的(没有空白)那么您可以像使用End(xlDown)那样使用,如下所示

For Each aCell In ActiveSheet.Range("A1:K" & ActiveSheet.Range("K1").End(xlDown))

如果你已经复制了它,我猜你不想多次复制同一行。您可以通过将数组或字符串与先前复制的行号保持一致并在再次复制之前进行检查,或使用Excel的唯一函数在复制后删除列表来完成此操作。

希望这有帮助。

除了:

您正在使用With阻止但未充分利用它,您需要在.个对象前添加一个点Range,以指明它们位于您的With中1}}表。像这样

Dim myRange as Range
With ActiveSheet
    Set myRange = .Range("A1:C10")
End With