基于VBScript中单元格值的颜色Excel

我有一个VBS将我的CSV转换为Excel。 现在我的CSV有一些字符,如“!” 我想在转换为excel之后在那些具有该字符的单元格上使用黄色。

Sample.CSV:

Name,Location,Phone,Comment1,Comment2 "ABC","Pune",123,"Expert Value","! Easy" "XYZ","Kol",567,"! Expert value",Easy" 

要求:转换到Excel后,我需要E2和D3 Cell应该是黄色的

MyScript.vbs:需要两个参数才能执行

 cscript C:\Test\MyScript.vbs \\C:\Test\Sample.CSV \\C:\Test\Sample.xlsx 

这是原始的脚本

 '====================================== ' Convert CSV to XLS ' ' arg1: source - CSV path\file ' arg2: target - Excel path\file '====================================== srccsvfile = Wscript.Arguments(0) tgtxlsfile = Wscript.Arguments(1) 'Create Spreadsheet 'Look for an existing Excel instance. On Error Resume Next ' Turn on the error handling flag Set objExcel = GetObject(,"Excel.Application") 'If not found, create a new instance. If Err.Number = 429 Then '> 0 Set objExcel = CreateObject("Excel.Application") End If objExcel.Visible = false objExcel.displayalerts=false 'Import CSV into Spreadsheet Set objWorkbook = objExcel.Workbooks.open(srccsvfile) Set objWorksheet1 = objWorkbook.Worksheets(1) 'Adjust width of columns Set objRange = objWorksheet1.UsedRange objRange.EntireColumn.Autofit() 'This code could be used to AutoFit a select number of columns 'For intColumns = 1 To 17 ' objExcel.Columns(intColumns).AutoFit() 'Next 'Make Headings Bold objExcel.Rows(1).Font.Bold = TRUE 'Freeze header row With objExcel.ActiveWindow .SplitColumn = 0 .SplitRow = 1 End With objExcel.ActiveWindow.FreezePanes = True 'Add Data Filters to Heading Row objExcel.Rows(1).AutoFilter 'set header row gray objExcel.Rows(1).Interior.ColorIndex = 15 '-0.249977111117893 'Save Spreadsheet, 51 = Excel 2007-2010 objWorksheet1.SaveAs tgtxlsfile, 51 'Release Lock on Spreadsheet objExcel.Quit() Set objWorksheet1 = Nothing Set objWorkbook = Nothing Set ObjExcel = Nothing 

在这里输入图像说明

在代码中包含下面的“For”循环。 它会search“!” 并将细胞染成黄色。 我在Notepad ++编辑器中编写了这个代码。 因此,您可能需要一些debugging。

 'set header row gray objExcel.Rows(1).Interior.ColorIndex = 15 '-0.249977111117893 For intRowCounter = 2 to objWorksheet1.usedRange.Rows.Count 'Skip header row For intColumnCounter = 1 to objWorksheet1.usedRange.Columns.Count If instr(1, objWorksheet1.Cells(intRowCounter, intColumnCounter).Value, "!", 1) > 0 Then objWorksheet1.Cells(intRowCounter, intColumnCounter).Interior.ColorIndex = 6 'Shade of Yellow 27, 44 , 36 also can be used End If Next Next 'Save Spreadsheet, 51 = Excel 2007-2010 objWorksheet1.SaveAs tgtxlsfile, 51