VBA Shade Alternate Rows

时间:2015-04-06 13:10:44

标签: excel-vba excel-2013 vba excel

使用this网站作为来源,我将this工作簿放在一起,提取并列出给定文件夹中的文件。

代码工作正常,但我尝试通过在C,D和E列中对替换行进行着色来稍微调整一下。

我已经研究过这个并找到了一个例子here

我遇到的问题是我只能设法遮蔽列E,我不知道为什么。我也喜欢遮挡另一排,但我对如何解决这个问题有些不确定。

这是提取文件并对行进行着色的代码。

Public Sub ListFilesInFolder(SourceFolder As Scripting.folder, IncludeSubfolders As Boolean)
    Dim lngLastRow As Long
    On Error Resume Next
    For Each FileItem In SourceFolder.Files
        ' display file properties
        Cells(iRow, 3).Formula = iRow - 13
        Cells(iRow, 4).Formula = FileItem.Name
        Cells(iRow, 5).Select
        Selection.Hyperlinks.Add Anchor:=Selection, Address:= _
        FileItem.Path, TextToDisplay:="Click Here to Open"
        iRow = iRow + 1 ' next row number

        lngLastRow = Sh.Cells(Cells.Rows.Count, "C").End(xlUp).Row
        Range("C14:E" & lngLastRow).Activate
        Selection.FormatConditions.Delete
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"

        Selection.FormatConditions(1).Interior.ColorIndex = 24

    Next FileItem

    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder, True
        Next SubFolder
    End If
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
End Sub

我只是想知道某人是否能够看到这个并让我知道我哪里出错了。

3 个答案:

答案 0 :(得分:0)

尝试一下,寻找"添加"评论。另外,请注意我刚为其他颜色条纹选择了另一种颜色 - 您可以根据需要进行更改。

Public Sub ListFilesInFolder(SourceFolder As Scripting.folder, _
           IncludeSubfolders As Boolean)
Dim lngLastRow As Long
Dim Toggle as integer           'added this here

On Error Resume Next
Toggle = 0
For Each FileItem In SourceFolder.Files
' display file properties
    Cells(iRow, 3).Formula = iRow - 13
    Cells(iRow, 4).Formula = FileItem.Name
    Cells(iRow, 5).Select
    Selection.Hyperlinks.Add Anchor:=Selection, Address:= _
          FileItem.Path, TextToDisplay:="Click Here to Open"
    iRow = iRow + 1 ' next row number
    lngLastRow = Sh.Cells(Cells.Rows.Count, "C").End(xlUp).Row
    Range("C14:E" & lngLastRow).Activate
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlExpression, _
               Formula1:="=MOD(ROW(),2)=0"
    '-----------------------------------
    'Add this section here
    if toggle = 0 then
      Selection.FormatConditions(1).Interior.ColorIndex = 24
      toggle = 1
    Else
      Selection.FormatConditions(1).Interior.ColorIndex = 42
      toggle = 0
    end if

Next FileItem

If IncludeSubfolders Then
    For Each SubFolder In SourceFolder.SubFolders
        ListFilesInFolder SubFolder, True
    Next SubFolder
End If
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub

答案 1 :(得分:0)

除非我遗漏了某些内容,否则您不需要单元格中的公式来创建VBA控制的备用着色方案。在没有文件目录代码的情况下,我创建了一个快速例程,仅为C,D和E列着色交替行。

如果您可以从上面的例程中删除FormatConditions代码,这可能是一个可接受的替代品。

Sub ReShade(startRow As Integer, endRow As Integer)
    '--- begin by "erasing" the previous row coloring
    ActiveSheet.Range(Cells(startRow, 3), Cells(endRow, 5)).Interior.ColorIndex = xlNone
    '--- shades alternate rows in columnd C, D, E
    Dim r As Integer
    Dim rowCells As Range
    For r = startRow To endRow Step 2
        Set rowCells = ActiveSheet.Range(Cells(r, 3), Cells(r, 5))
        With rowCells
            .Interior.ColorIndex = 24
        End With
    Next r
End Sub

'--- call ReShade at the end of your routine, as in...
Sub test()
    ReShade 5, 20  
End Sub

答案 2 :(得分:0)

对于那些感兴趣的人,这是我的工作代码:

Public Sub ListFilesInFolder(SourceFolder As Scripting.folder,IncludeSubfolders As Boolean)

    Dim LastRow As Long

    On Error Resume Next
    For Each FileItem In SourceFolder.Files
        ' display file properties
        Cells(iRow, 3).Formula = iRow - 12
        Cells(iRow, 4).Formula = FileItem.Name
        Cells(iRow, 5).Select
        Selection.Hyperlinks.Add Anchor:=Selection, Address:= _
        FileItem.Path, TextToDisplay:="Click Here to Open"
        iRow = iRow + 1 ' next row number

        With ActiveSheet
            LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
            LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        End With

        For Each Cell In Range("C13:E" & LastRow) ''change range accordingly
            If Cell.Row Mod 2 = 1 Then ''highlights row 2,4,6 etc|= 0 highlights 1,3,5
                Cell.Interior.Color = RGB(232, 232, 232) ''color to preference
            Else
                Cell.Interior.Color = RGB(141, 180, 226) 'color to preference or remove
            End If
        Next Cell
    Next FileItem

    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder, True
        Next SubFolder
    End If
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
End Sub