【发布时间】: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的引用。