声明范围时遇到问题(从合并单元格中获取值)

时间:2014-10-14 07:19:47

标签: excel vba excel-vba merge range

我试图从合并的单元格(在.find gcell结果的偏移处)中获取值,通过循环逐个遍历行(并在另一个工作表中查找特定值)。这段代码如果我们正在寻找的单元格被合并并执行以下操作:

-Unmerges - 通过使用.Address获取合并单元格的限制 - 查看这些单元格中包含的值 -re-将值写在它们上面

此时它不起作用,我相信因为变量格式为.address给出一个字符串,属性rows.value和.MergeArea需要一个范围我的问题是,如何从一个地址字符串更改为一个范围?也许这样我可以解决这个问题。 如果你有任何其他的叹息,我会很高兴听。

非常感谢。

代码:

(循环代码不显示,因为它工作正常)

lineasemergidasCont = gCell.Offset(, 4).MergeArea.Rows.count
                      emergidainicio = gCell.Offset(, 4).Address
                      emergidafinal = gCell.Offset(lineasemergidasCont, 4).Address
  goes well until here------->  Range(emergidainicio, emergidafinal).MergeArea.UnMerge
                                 filainicio = emergidainicio.Rows.Value
                                  filafinal = emergidafinal.Rows.Value
                                       For p = filainicio To filafinal
                                            If  .Cells(p, 10).Value <> "" Then
                                                 .Cells(p, 10).Value = datoemergido
                                            End If
                                       Next p
                                        For s = filainicio To filafinal
                                            datoemergido = .Cells(s, 10).Value
                                       Next s

(CODE继续有效)

1 个答案:

答案 0 :(得分:0)

@ rory,@ patrick lapelletier,@ therak,谢谢大家!在这里,我删除最终适用的代码:

- 找到合并的单元格 - 淹没这些 - 在其中一个上复制找到的值并将其发布到所有合并的值 - 将值转移到另一张表

它还考虑了可以复制的值,将其余结果添加到旧值之后。 这在带有.find函数的循环中工作,该函数在gcell上给出结果

          do
            If gCell.Offset(, 4).MergeArea.Address = gCell.Offset(, 4).Address Then
                              'Not a merged cell
                          If HojaNueva.Cells(F2, 20).Value = "" Then
                               HojaNueva.Cells(F2, 20).Value = gCell.Offset(, 4).Value
                          Else
                            hojito1 = HojaNueva.Cells(F2, 20).Value
                            HojaNueva.Cells(F2, 20).Value = hojito1 & "," & gCell.Offset(, 4).Value
                           End If

                     Else
                            'is a merged cell
                           lineasemergidasCont = gCell.Offset(, 4).MergeArea.Rows.count
                           emergidainicio = gCell.Offset(, 4).Address
                           emergidafinal = gCell.Offset(lineasemergidasCont - 1, 4).Address
                           Range(emergidainicio).MergeArea.UnMerge
                            Range(emergidafinal).MergeArea.UnMerge
                           numeros1 = Range(emergidainicio).Row
                           numeros2 = Range(emergidafinal).Row

                              For p = numeros1 To numeros2
                                  If Not ws.Cells(p, 10).Value = "" Then
                                     datoemergido = ws.Cells(p, 10).Value
                                   End If
                               Next p
                               For s = numeros1 To numeros2
                                       ws.Cells(s, 10).Value = datoemergido
                                Next s
                                 numeros1 = ""
                                 numeros2 = ""

                                If HojaNueva.Cells(F2, 20).Value = "" Then
                                  HojaNueva.Cells(F2, 20).Value = datoemergido
                                 Else
                                  hojito3 = HojaNueva.Cells(F2, 20).Value
                                  HojaNueva.Cells(F2, 20).Value = hojito3 & "," & datoemergido
                                 End If

                             End If

                           Set gCell = ws.Columns("F").FindNext(gCell)

                          Loop While firstAddress <> gCell.Address