通过VBA更改Excel中的字体颜色和对齐方式

时间:2017-07-28 20:27:30

标签: excel vba excel-vba csv vbscript

要求是:

我有一个固定的EXCEL模板,从第4行到第10行(将来可能更多)将被删除并每天从CSV添加。 现在我的VBS正在从Excel中删除现有记录并从CSV中放入数据。这是完美的。在需要进行小的更改之后,是否可以动态删除现有行 即今天我有10行,所以它正在清除10,明天如果有20行可以清除那些记录并按照CSV的输入行号插入。

这是我现有的代码:

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 objWorkbookSrc = objExcel.Workbooks.Open(srccsvfile)
Set objWorksheetSrc = objWorkbookSrc.Worksheets(1)
Set objWorkbookTgt = objExcel.Workbooks.Open(tgtxlsfile)
Set objWorksheetTgt = objWorkbookTgt.Worksheets("Report")
'Adjust width of columns
Set objRange = objWorksheetSrc.UsedRange
objRange.Borders.LineStyle = 1
Set objRangeToCopy = objRange.Resize(objRange.Rows.Count - 1).offset(1)
objWorksheetTgt.Rows(4).Resize(10).Clear

objRangeToCopy.Copy objWorksheetTgt.Range("A4")

aList = Array("NOT ", "NO ", "NONE", "!")
For Each Item In aList
            For Each c In objWorksheetTgt.UsedRange
                If InStr(1, c.Value, Item) > 0 Then
                    c.Interior.ColorIndex = 6
                End If
            Next
Next

'Save Spreadsheet, 51 = Excel 2007-2010
objWorkbookTgt.Save
objWorkbookTgt.Close(False)
objWorkbookSrc.Close(False)

'Release Lock on Spreadsheet
objExcel.Quit()
Set objWorksheetSrc = Nothing
Set objWorkbookSrc = Nothing
Set objWorksheetTgt = Nothing
Set objWorkbookTgt = Nothing
Set objExcel = Nothing

最后有些点睛之笔。 需要在Excel中以字体编写这些数据: ABC_Font 和尺寸: 10 并且基于某些column_name需要中心/右,即名称和位置值应为" 中心"和电话号码应该是" 对齐中心"和" WrapText " 。还需要新的线条角色。 这也可能吗?

我尝试使用excel宏来完成这项工作。并发现下面的代码,认为它会使我的脚本最终,但它不起作用。

现在我需要在我现有的上述代码中下面的示例代码,我作为txt文件附加,供您参考。我附上了excel和csv样本。

Range("A4:F13").Select
    With Selection.Font
        .Name = "Trebuchet MS"
        .Size = 10
    End With

Range("A4:A13").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = True
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

这是我的CSV

Name,Location,Phone,Comment1,Comment2,comment3
"ABC!","Pune",123,"Expert Value","! Easy","Popular"
"XYZ","Kol",567,"! Expert value",Easy,"!Credit"
"PQR","Mum",234,"NOT value","Value for money","Debit"
"RST","DEL",0,"Value","NO value","N/A"
"Ram","KOL",100,"NO Value","value","N/A"
"XYZ","Kol",567,"! Expert value","!Easy","!Credit"
"qwer","DEL",567,"Expert value","Easy","!Credit"
"cvbn","Pune",567,"! Expert value","!Easy","!Debit"
"rtyu","DEL",567,"! Expert value","Easy","!Credit"
"kllo","Pune",567,"Expert value","NOT Easy","!Bad"

这是我目前输出的Excel截图

enter image description here

但我希望输出像这样。

enter image description here

2 个答案:

答案 0 :(得分:0)

只需将格式代码复制并粘贴到主代码中的保存点

即可

答案 1 :(得分:0)

Cell set .WrapText = True。

     With Range("A4:F13")
        With .Font
            .Name = "Trebuchet MS"
            .Size = 10
        End With
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .EntireRow.AutoFit
    End With
    With Range("A4:A13")
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
    End With