【问题标题】:Change the font colour in all cells of a list of Excel sheets更改 Excel 工作表列表的所有单元格中的字体颜色
【发布时间】:2019-12-19 03:59:36
【问题描述】:

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

【问题讨论】:

  • “不工作”是什么意思?请问具体点?
  • xlDown 将是未知的,因为您是后期绑定,在Const ForReading = 1 之后添加Const xlDown = -4121。除此之外,请描述它是如何失败的。
  • 抱歉含糊不清。当我运行它时,它不会将字体颜色从红色更改为黑色。所以 pathlist.txt 中有一个文件路径列表,它应该从数组中读取文件路径,然后进入它们并查找红色字体并将它们更改为黑色。
  • 如果有错误,请打开excel,按ALT+F11,粘贴上面的代码,排除与objExcel相关的行。即Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True 并从代码中省略对objExcel 的引用。

标签: excel vbscript


【解决方案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】:

    您不能使用*.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 工作簿的路径,没有其他内容。

    【讨论】:

    • 谢谢你,我只需要将红色字体改为黑色,而不仅仅是将所有字体颜色改为黑色。
    • 您拥有的代码将单元格颜色更改为黑色,如果它是红色的,如果它不是红色的。
    • 哦,抱歉,这只是想看看它是否正常工作,它不应该在那里。
    • 不,最后一个不起作用,可能是路径列表部分不起作用。
    【解决方案3】:

    这会将所有且只有红色字体的单元格变为黑色字体。并且将在不使用循环或任何比较语句的情况下执行此操作,从而提供非常快速可靠的代码。

    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
    

    【讨论】:

    • 嘿,这太好了,有什么办法可以输入数组 arrServiceList 以便它通过文件路径列表?
    • 使用你所拥有的直到RedColor = RGB(255, 0, 0) 行,然后完成我的回答,但将工作簿和工作表(objWorkbook,objWorksheet)引用添加到我的范围。
    • 非常感谢,我会试一试并回复您。
    • 我按照你说的做了,但在Dim lngLastFilePath As long 所在的行出现预期语句结束错误。有什么想法吗?
    • @user1839084 代码是 VBA,而不是 VBScript。 VBScript 不支持类型化变量声明。
    猜你喜欢
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    相关资源
    最近更新 更多