【问题标题】:EXCEL VBA - formatting : merge, wrap textEXCEL VBA - 格式化:合并,换行
【发布时间】:2016-06-09 19:43:39
【问题描述】:

我从一位非常好的 SO 先生那里获得了有关此代码的帮助。这部分代码基本上编写了一个跨越两列的标题,合并和换行文本。我为不同长度的标题生成这个 excel 提取。对于某些报告,如果标题很短,则高度是常规单元格,对于其他报告,其高度为 x3,即使标题的长度不需要那么多高度。有没有办法让我指定我希望 ROW 始终是……比如常规行 HEIGHT 的高度的 3 倍。我不希望它因报告而异。无论字符串的长度如何,我都希望它看起来一样。如果我使用 .merge 和 .wraptext=true

,这可能吗?
Do While Not g_RS3.EOF
    With xlSheetInsurance.Cells(xlRow, xlCol)
        .Value = g_RS3("ShortLabel")
        With .Resize(1, 2)
            .WrapText = True
            .Merge
        End With
        .Offset(1, 0).Resize(1, 2) = Array("# Clients", "# Students")
        .Offset(2, 0).Resize(1, 2).ClearContents
        With .Offset(0, 1)
            .Resize(1, 2).Merge
            .Value = "TOTAL"
            .Offset(1, 0).Resize(1, 2) = Array("# Clients", "# Students")
            .Offset(2, 0).Resize(1, 2).Formula = _
                "=SUMIFS(" & xlSheetInsurance.Range(.Parent.Cells(xlRow + 2, xlStartCol), .Parent.Cells(xlRow + 2, xlCol + 1)).Address(0, 1) & Chr(44) & _
                             xlSheetInsurance.Range(.Parent.Cells(xlRow + 1, xlStartCol), .Parent.Cells(xlRow + 1, xlCol + 1)).Address(1, 1) & Chr(44) & _
                             .Parent.Cells(xlRow + 1, xlCol).Address(1, 0) & Chr(41)
            .Offset(2, 0).Resize(1, 2).AutoFill .Offset(2, 0).Resize(7, 2) ' AutoFill formula for all Types
            .Offset(2, 0).Resize(7, 2).Borders(xlEdgeRight).LineStyle = xlContinuous
        End With
        With .Resize(2, 4)
            .Font.Bold = True
            .WrapText = True
            .VerticalAlignment = xlCenter
            .HorizontalAlignment = xlCenter
            .Borders.Weight = xlThin
        End With
    End With
    xlCol = xlCol + 2
    g_RS3.MoveNext
Loop

【问题讨论】:

  • .Row.Height = 45 15 是默认行高
  • 但我会将它包含在此代码中的什么位置。我正在循环浏览它并随时调整它的大小。我什至不确定为什么它给了我不同的行高
  • 在您指定WrapTextMergeWith .Resize(1, 2) 块内
  • 给我一个错误 - 需要对象
  • 抱歉 - 语法错误,它是 .RowHeight = 45

标签: vba excel merge


【解决方案1】:

这将为您提供所需的:

    With .Resize(1, 2)
        .WrapText = True
        .Merge
        .RowHeight = 45
    End With

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-17
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多