【问题标题】:AHK Excel COM - Change color within a sheetAHK Excel COM - 在工作表中更改颜色
【发布时间】:2019-03-06 16:48:21
【问题描述】:

你好 stackoverflow 成员,

我有一个 Excel 工作表,其中特定单元格有红色边框和红色字体大小。 目前,我正在寻找一种方法将红色替换为另一种颜色,例如蓝色。

由于我没有任何 VBA 知识,我正在尝试通过 AutoHotkey 及其 COM 支持来实现这一点。 我已经取得了一些进展! 但是,即使它有效,它也不会改变几个单元格的边界。 字体颜色也一样 - 有些会改变,有些不会改变。

这是我的代码:

    F1::
xl := ComObjActive("Excel.Application") ; Connect to Excel
ws := xl.ActiveSheet ; Connect to worksheet
column = 65 ; ASCCI code
Loop, 800
{
  r++ ; row 1
  if (r = 41) ; If row 41 was reached:
  {
    r = 1 ; Go back to row 1
    column := column + 1 ; Next column
  }
  c := Chr(column) ; ASCCI to String
  cell = %c%%r% ; Cell = e.g. A1, etc.
  ;if xl.range(cell).Borders(xlEdgeLeft).Color = 0x0000FF ; If border color is red - TYPE CONFLICT!
  if xl.range(cell).Borders.Color = 0x0000FF ; If border color is red: - WORKS PARTLY
  {
    ;xl.range(cell).Borders(xlEdgeLeft).Color := 0xFFD700 ; Set blue color - I DOUBT IT WILL WORK!
    xl.range(cell).Borders.Color := 0xFFD700 ; Set to blue - WORKS PARTLY
  }
  if xl.range(cell).Font.Color = 0x0000FF ; If font color is red:  - WORKS PARTLY
  {
    xl.range(cell).Font.Color := 0xFFD700 ; Set to blue - WORKS PARTLY
  }
}
return

如您所见,我已经尝试过 if xl.range(cell).Borders(xlEdgeLeft).Color = 0x0000FF 这似乎不起作用。 上面写着“类型冲突”。

在此屏幕截图中,您可以看到所有成功的颜色调整(蓝色)。 其余部分为红色,未更改。 Result

任何帮助将不胜感激!

【问题讨论】:

    标签: excel com autohotkey


    【解决方案1】:

    问题解决了。

    for xlCell in xl.ActiveSheet.UsedRange.Cells ; Used cells
    {
      for xlBorder in xlCell.Borders ; Where borders are used
      {
        if (xlBorder.Color = 0x0000FF) ; Red
        {
          xlBorder.Color := 0x5FD200 ; Green
        }
        if (xlCell.Font.Color = 0x0000FF) ; Red
        {
          xlCell.Font.Color := 0x5FD200 ; Green
        }
      } 
    }
    for xlShape in xl.ActiveSheet.Shapes ; Used shapes
    {
      if (xlShape.Line.ForeColor.RGB = 0x0000FF) ; Red
      {
        xlShape.Line.ForeColor.RGB := 0x5FD200 ; Green
      }
    }
    

    还是谢谢。

    附:对于文本中的字体颜色没有解决方案。

    【讨论】:

      猜你喜欢
      • 2016-10-09
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多