代码之家  ›  专栏  ›  技术社区  ›  Stormer

从Access格式化Excel单元格无效

  •  1
  • Stormer  · 技术社区  · 2 年前

    我试图简单地创建一个Excel表格,其中包含许多格式,可以通过VBA访问。

    在这个例子中,我只是尝试选择A1到E1,并将单元格的颜色变成蓝色,每个单元格都有一个左边框,但什么都没有发生,Excel文件打开了,但它只是放在那里,什么都不做。我错过了什么?

    Dim xlApp As Excel.Application
    Dim xlSheet As Excel.Worksheet
    Dim xlWorkbook As Excel.Workbook
    
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlWorkbook = xlApp.Workbooks.Add
    Set xlSheet = xlWorkbook.Sheets(1)
    
    With xlSheet.Range("A1:E1").Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 5
    End With
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   BigBen    2 年前
    With xlSheet.Range("A1:E1").Borders(xlEdgeLeft)
    

    这仅适用于 A1 .

    With xlSheet.Range("A1,B1,C1,D1,E1").Borders(xlEdgeLeft)
    

    或者使用循环:

    Dim cell As Excel.Range
    For Each cell in xlSheet.Range("A1:E1")
       With cell.Borders(xlEdgeLeft)
           .LineStyle = xlContinuous
           .Weight = xlThin
           .ColorIndex = 5       
       End With
    Next
    

    .ColorIndex 是调色板的索引,而不是绝对颜色。请尝试以下操作:

    .Color = vbBlue