检查合并的单元格并比较相邻,以便从比较的单元格值设置唯一值

我在Excel 2010中编写了一个macros,其问题如下所示:
我有两列,一个是Keystring值,另一个是uuidstring。 这个想法是,每个键应该只有一个uuid,但现在的表格,关键单元可以是合并的单元格或单个单元格。 macros需要识别哪些单元格被合并,哪些不合并,所以我有两个select:

  • 如果单元格被合并,检查所有的相邻单元格,select第一个uuid值并复制/粘贴到其他相邻的单元格,也就是说,单元格下面(可以用Offset()
  • 如果没有合并单元格,但是键值在多个单元格中重复,请将uuid值复制/粘贴到相邻单元格。

所以基本上是检查合并的单元格MergeArea但我不知道是否需要迭代通过它的地址或检查偏移量为Offset(0,1)或范围内的单元格。 用我的代码,我可以知道如果单元格合并,但现在,我怎么迭代通过它的相邻单元格值?

现在的代码是:

 Sub CopyUUID() Dim lRow As Long Dim rng As Range Dim ws As Worksheet Dim rMerged As Range Dim value As Variant Set ws = Sheets(ActiveSheet.Name) On Error GoTo ExitProgram 'If an error happens within the execution, skips it and continue in next step Application.DisplayAlerts = False 'We can cancel the procedure without errors With ws lRow = .Range("F" & .Rows.count).End(xlUp).row Set rng = .Range(.Cells(3, 6), .Cells(lRow, 6)) rng.Select For Each cell In rng If cell.MergeCells Then 'Code for merged cells Else 'Code to use for single cells End If Next cell End With ExitProgram: Exit Sub End Sub 

表格示例

尝试下面的代码。 请注意,这将覆盖UUID的当前内容,因此请在testing之前制作备份副本。 如果你不想修改UUID列,你可以修改这个来适应你的需要。

 Sub CopyUUID() Dim lRow As Long Dim rng As Range Dim c As Range Dim ws As Worksheet Dim rMerged As Range Dim value As Variant Set ws = Sheets(ActiveSheet.Name) On Error GoTo ExitProgram 'If an error happens within the execution, skips it and continue in next step ' Application.DisplayAlerts = False 'We can cancel the procedure without errors With ws lRow = .Range("F" & .Rows.Count).End(xlUp).Row Set rng = .Range(.Cells(3, 6), .Cells(lRow, 6)) ' rng.Select For Each c In rng If c.MergeCells Then 'Code for merged cells c.Offset(0, 1).Formula = c.MergeArea.Cells(1, 1).Offset(0, 1).Formula Else 'Code to use for single cells If c.Formula = c.Offset(-1, 0).Formula Then c.Offset(0, 1).Formula = c.Offset(-1, 1).Formula End If End If Next c End With ExitProgram: Exit Sub End Sub 

在MergedCell中时,它使UUID与合并区域中第一个单元格的UUID相同。 不在MergedCell中时,如果Key与上面的行相同,则从上面的行复制UUID。

我改变你的variablescellc (我不喜欢使用可以与内置插件混淆的variables名称),并注释了几行。

希望这可以帮助

 Option Explicit Sub CopyUUID() Const UUID As Long = 31 'col AE Dim lRow As Long, cel As Range, isM As Boolean, copyID As Boolean, kCol As Long With ActiveSheet kCol = -25 'col F lRow = .Cells(.Rows.Count, UUID + kCol).End(xlUp).Row For Each cel In .Range(.Cells(3, UUID), .Cells(lRow, UUID)) isM = cel.Offset(0, kCol).MergeCells copyID = isM And Len(cel.Offset(0, kCol)) = 0 copyID = copyID Or (Not isM And cel.Offset(0, kCol) = cel.Offset(-1, kCol)) If copyID Then cel = cel.Offset(-1) Next End With End Sub 

我采取了一个简单的方法来解决这个问题,正如我所做的一样。

  1. 样本表显示合并单元格和未合并单元格的数据。 样本数据

  2. 运行程序代码以取消合并单元格。 该程序的输出如下所示。

未合并样本数据第一阶段

  1. 如果这种数据结构与您的情况相匹配,那么为B列添加2行代码将会按照下面的图像保留数据。

通过程序代码删除列之后未合并的数据

  1. 程序代码如下:

'没有列删除:

 Sub UnMergeRanges() Dim cl As Range Dim rMerged As Range Dim v As Variant For Each cl In ActiveSheet.UsedRange If cl.MergeCells Then Set rMerged = cl.MergeArea v = rMerged.Cells(1, 1) rMerged.MergeCells = False rMerged = v End If Next End Sub 'With coumn deletion Sub UnMergeRangesB() Dim cl As Range Dim rMerged As Range Dim v As Variant For Each cl In ActiveSheet.UsedRange If cl.MergeCells Then Set rMerged = cl.MergeArea v = rMerged.Cells(1, 1) rMerged.MergeCells = False rMerged = v End If Next Columns("B:B").Select Selection.Delete Shift:=xlToLeft End Sub