【问题标题】:How to cycle through merged cells (and populate values from a one-dimensional array)?如何循环通过合并的单元格(并从一维数组中填充值)?
【发布时间】:2019-01-22 15:23:57
【问题描述】:

我想找到一种方法如何循环通过合并的单元格,例如使用 For...Next 循环。

我只能设法让它像这样工作:

Cells(1,1).Select

For i=1 to 6
      Selection.Value = arrData(i)
      Selection.Offset(0,1).Select
Next i

我讨厌使用.Select - 但如果我使用.Offset(0,i),它不会从合并的单元格移动到合并的单元格,而只是原始单元格的列数。

更多细节 - 我将值从 csv-like 格式复制到格式更好的输出表中,然后应该与一堆合并的单元格一起导出。

工作表有多个部分,但在每个部分中,每行的单元格数量是已知的。

没有.Select,我唯一可行的解​​决方案是使用.Cells

例子:

For row=0 to 12
   with rng.Offset(1,0)
      .cells(row+1,1)=arrdata(1+(row*6))
      .cells(row+1,3)=arrdata(2+(row*6))
      .cells(row+1,7)=arrdata(3+(row*6))
      .cells(row+1,9)=arrdata(4+(row*6))
      .cells(row+1,14)=arrdata(1+(row*6))
      .cells(row+1,16)=arrdata(1+(row*6))
   End with
Next row

但这是相当艰巨的。

编辑:这是截图: target area

这个想法是行的数量是完全灵活的,具体取决于事务。所以有时只有一行,但实际上可以是任何东西。

我的代码使用基于命名范围的相对引用生成此部分。

然后从丑陋的表格(所有信息都存储在一行中)将值输入一维数组,然后将数组输入到漂亮的表格中。

如果工作表没有合并单元格,则公式看起来很简单:

Dim i as integer, j as integer
Dim ws as worksheet: set ws = Worksheets("Printable")

'data array has already been filled with info in a linear order beforehand

k=1
For i=1 to item_qt 'number of cost items lines
   For j=1 to item_col 'number of detail columns (in this section)
      ws.Range("item_title").Offset(1,0).Cells(i,j).Value=data(k)
      k=k+1
   Next j
Next i

但是由于这张表的性质——应该是可打印的并且在眼睛上更好看——我不能这样做,并且必须找到一种方法来在合并的单元格之间切换。

希望这次编辑能解决一些问题。

我现在也在研究这些建议,看看我是否可以以某种方式应用这些建议,但如果有人知道更好的东西,我愿意接受一切。

【问题讨论】:

  • 如果您发布工作表的图像然后解释您正在尝试做的事情可能会更容易,例如我想跳转到 C5 中的合并单元格取消合并范围并将单元格的颜色更改为右侧...您可以编辑您的答案。您的帖子下方有一个“编辑”按钮。
  • 添加了一个编辑 - 感谢您的关注!

标签: excel vba


【解决方案1】:

如果您正在逐步浏览合并的列,您可以使用类似

For i = startColumn To endColumn
    If Cells(row,StartColumn).MergeArea.Columns.Count > 1 Then
        'Do Stuff
        i = i + Cells(row,StartColumn).MergeArea.Columns.Count - 1
    End If
    Debug.Print i
Next i

这将测试合并的列,然后在合并后跳转到下一列。

编辑:

看到您在编辑中添加的数据结构,您可以将 MergeArea.Columns.Count 方法合并到您的 For j-Next j 循环中

k=1
For i=1 to item_qt 'number of cost items lines
   For j=1 to item_col 'number of detail columns (in this section) <-this will need to
                       'be the total number of columns, not just the number of 
                       'detail fields
      ws.Range("item_title").Offset(1,0).Cells(i,j).Value=data(k)
      j = j + ws.Range("item_title").Offset(1,0).Cells(i,j).MergeArea.Columns.Count - 1
      k=k+1
   Next j
Next i

【讨论】:

  • 嘿,这是一个有趣的方法!我会看看我是否可以弯曲它以适应我的项目!谢谢!顺便说一句,我添加了一个编辑,以更好地解释自己 - 我的词不是很好。
  • 添加到我的答案中,希望这有助于阐明如何在您的编辑中将 MergeArea 方法添加到 simple 循环中。
  • 这非常有效——我会亲吻你的眼睛。非常感谢!
  • 哈哈,不需要亲吻。很高兴能帮上忙!
【解决方案2】:

通过搜索“excel 查找合并的单元格 vba”Google 提出:

如何在 Excel 中识别和选择所有合并的单元格?
https://www.extendoffice.com/documents/excel/962-excel-select-merged-cells.html

Sub FindMergedcells()
'updateby Extendoffice 20160106
    Dim x As Range
    For Each x In ActiveSheet.UsedRange
        If x.MergeCells Then
            x.Interior.ColorIndex = 8
        End If
    Next
End Sub




2 在 Excel 中查找合并单元格的实用方法
https://www.datanumen.com/blogs/2-practical-methods-find-merged-cells-excel/

Sub FindMerge()
    Dim cel As Range
    For Each cel In ActiveSheet.Range(“A1:G13”)
        If cel.MergeCells = True Then
            ‘change the color to make it different
            cel.Interior.Color = vbYellow
        End If
    Next cel
End Sub

【讨论】:

  • 嘿,谢谢你这么快的回复!这不是我所追求的,我添加了更多信息和图片来尝试更好地解释它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多