尝试更改Excel工作表列表的所有单元格中的字体颜色

时间:2013-07-01 15:34:37

标签: excel vbscript

嘿我一直在尝试将excel表单中的单元格的字体颜色从红色更改为黑色。代码使用txt文件读取文件路径,然后将它们放入数组中。然后使用数组检查excel表格的红色字体颜色并将其更改为黑色。可悲的是它没有用,我对VBscript的调试知识非常有限,所以有人可以看看我做错了吗?

REM Attribute VB_Name = "Module1"
Sub SimpleMacro()
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = True

    Const ForReading = 1 

    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objTextFile = objFSO.OpenTextFile _ 
    ("pathlist.txt", ForReading) 

    Do Until objTextFile.AtEndOfStream 
        strNextLine = objTextFile.Readline 
        arrServiceList = Split(strNextLine , ",") 
        Wscript.Echo "Server name: " & arrServiceList(0) 
        For i = 1 to Ubound(arrServiceList) 
            Wscript.Echo "Service: " & arrServiceList(i) 
        Next
    Loop

    Set objWorkbook = objExcel.Workbooks.Open(arrServiceList)
    Set objWorksheet = objWorkbook.Worksheets(1)

    RedColor = RGB(255, 0, 0)
    BlackColor = RGB(0, 0, 0)

    'Get number of rows in the specified column
    RowsCount = Range("A1" *.End(xlDown)).Rows.Count

    'Select cell
    Range("A1" *.End(xlDown)).Select

    'Loop the cells
    For x = 1 To RowsCount
        If ActiveCell.Font.Color = RedColor Then
            'Change the text color
            ActiveCell.Font.Color = BlackColor
        Else
            ActiveCell.Font.Color = BlackColor
        End If

        ActiveCell.Offset(1, 0).Select
    Next
End Sub

3 个答案:

答案 0 :(得分:1)

您无法使用*.End(xlDown)之类的内容。不仅VBScript中的常量未定义,而且还没有关键字/变量*。您可以将特定列的字体颜色设置为黑色,如下所示:

objWorksheet.Columns(1).EntireColumn.Font.Color = BlackColor

或使用范围的字体颜色如下:

objWorksheet.UsedRange.Font.Color = BlackColor

要更改字体颜色为红色的所有单元格的颜色,您可以使用以下内容:

For Each cell In objWorksheet.Cells
  If cell.Font.Color = RedColor Then cell.Font.Color = BlackColor
Next

另一件事:您可以使用一系列路径打开多个工作簿:

Set objWorkbook = objExcel.Workbooks.Open(arrServiceList)

但是在这种情况下,数组应该只包含Excel工作簿的路径,没有别的。

答案 1 :(得分:1)

我假设您只想更改A列中的字体颜色。以下是正确的VBA,我希望VBScript是正确的。

lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastrow
   If Range("A" & i).Font.Color = RGB(255, 0, 0) Then
        Range("A" & i).Font.Color = RGB(0, 0, 0)      'you can substitute your color variables in
   End If
Next

答案 2 :(得分:0)

这会将所有且只有红色字体的单元格变为黑色字体。并且不使用循环或任何比较语句就可以做到这一点,提供非常快速可靠的代码。

Sub Sample()
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

Dim lngLastFilePathRow As Long

lngLastFilePathRow = Cells(Rows.Count, 1).End(xlUp).Row

With Range("A1:A" & lngLastFilePathRow)
     'Filter Out Everything that does NOT have Red font
    .AutoFilter Field:=1, Criteria1:=RGB(255, 0 _
        , 0), Operator:=xlFilterFontColor
    'With only the cells that have Red font change the color to black
    .SpecialCells(xlCellTypeVisible).Font.ColorIndex = xlAutomatic
    .AutoFilter
End With

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With
End Sub