【问题标题】:VBA Merge Cells with loop through ColumnsVBA通过列循环合并单元格
【发布时间】:2020-04-23 12:15:04
【问题描述】:

我的 WS 看起来像这样

我有这个代码:

Dim w As Long

Dim wb As Workbook
Dim sht As Worksheet

Set wb = ActiveWorkbook
Set sht = wb.ActiveSheet

Dim lRow As Long
Dim lCol As Long

lRow = Cells(Rows.Count, 1).End(xlUp).Row
lCol = Cells(4, Columns.Count).End(xlToLeft).Column

For w = 1 To lCol

   Range(.Cells(1, w), .Cells(2, w), .Cells(3, w)).Merge

Next w

我想让单元格像这样合并和居中:

但我找不到正确的方法。我想合并每列中的前 3 个单元格 示例:A1:A3,但列太多,所以我想遍历它们。 谢谢!

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我想通了!这就是答案

      For w = 1 To lCol
    
         With sht
             Range(Cells(1, w), Cells(3, w)).Merge
    
         End With
    
      Next w
    

    【讨论】:

    • .Range(.Cells(1, w), .Cells(3, 2)).Merge。期间很重要:-)
    • 无需使用 With 语句 - 您可以使用 RowsColumns 属性:sht.Rows("1:3").Columns(w).Merge
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2018-09-12
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    相关资源
    最近更新 更多