2013-07-01 135 views
0

嘿,我一直在試圖改變Excel表格中的單元格的顏色從紅色到黑色。該代碼使用一個txt文件來讀取文件路徑,然後將它們放入一個數組中。然後使用數組檢查Excel表格中的紅色字體顏色並將其更改爲黑色。可悲的是,它不工作,我有我的VBscript調試知識非常有限,所以任何人都可以看一看,看看我做錯了什麼?試圖改變excel表格列表中所有單元格的字體顏色

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 
+0

你是什麼意思「不工作」?請具體說明? – shahkalpesh

+1

'xlDown'將會是未知的,因爲你是晚期綁定,在'ConstForReading = 1'後面加上'Const xlDown = -4121'。除了描述它是如何失敗的。 –

+0

對不起,太模糊了。當我運行它時,它不會將字體顏色從紅色更改爲黑色。因此pathlist.txt中有一個文件路徑列表,它應該讀取數組中的文件路徑,然後進入它們並查找紅色字體並將其更改爲黑色。 – Cmasterd

回答

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 

另一件事:

012:您可以通過使用路徑的數組打開多個工作簿
Set objWorkbook = objExcel.Workbooks.Open(arrServiceList) 

但在這種情況下,該數組應該只包含Excel工作簿的路徑,沒有別的。

+0

感謝你,我需要它只改變紅色的字體黑色不只是把所有的字體顏色變成黑色。 – Cmasterd

+0

您使用的代碼將單元格顏色更改爲黑色(如果它是紅色的,並且它不是紅色)。 –

+0

哦,對不起,這只是試圖看到它的工作並不意味着在那裏。 – Cmasterd

0

我假設你只想改變列A中的字體顏色。以下是正確的VBA,我希望VBScript能夠正確的修改爲wll。

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 
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 
+0

嘿,謝謝你這是偉大的,是否有任何方式然後輸入數組'arrServiceList',以便它通過文件路徑列表? – Cmasterd

+0

使用你所擁有的'RedColor = RGB(255,0,0)'然後用我的答案完成,但將工作簿和工作表(objWorkbook,objWorksheet)引用添加到我的範圍。 – user2140261

+0

非常感謝,我會放棄並回復你。 – Cmasterd

相關問題