0

我正在尝试将 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
4

3 回答 3

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 工作簿的路径,仅此而已。

于 2013-07-01T15:58:41.663 回答
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
于 2013-07-01T16:36:39.280 回答
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
于 2013-07-01T18:15:48.357 回答