使用IsEmpty和合并空单元格

时间:2019-06-12 17:58:52

标签: excel vba excel-formula

我正在编写代码,该代码将搜索具有很多重复项的非常大的ex​​cel工作表,因为它们都具有1个匹配列和ID列,因此我可以轻松地将重复项排序在一起。我要坚持的是为什么IsEmpty无法查看当前单元格并查看其是否为空以及下一个单元格(重复的单​​元格)是否具有数据。然后,包含数据的单元格将合并到空单元格中。

我尝试使用Range作为参数以及无济于事的单元格。

Private Sub CountDuplicates()
Dim i As Integer

'The cell it will use to search
Dim idCheck As Range

'The cell it will use to compare text
Dim currentCell As Range

'This will be used to format the "Changes" column
Dim rowNumberValue As Integer, columnNumberValue As Integer, rowBelow As Integer
placement = 0
colNum = 3

rowNumberValue = ActiveCell.Row
columnNumberValue = ActiveCell.Column
rowBelow = ActiveCell.Row + 1

'Searches by ID column
For Each idCheck In Worksheets("Sheet1").Range("B2:B1000")

   'This checks to find duplicate ID rows
   If idCheck.Value = idCheck.Offset(-1, 0).Value Then

         'Goes from each column starting from the ID column (H = 7th letter in alphabet and H is the last column)
         For colNum = 3 To 7

            'Checks to see if the cell has no value but the duplicate does
            If IsEmpty(Range(Cells(rowNumberValue, colNum))) = True And IsEmpty(Range(Cells(rowNumberValue + 1, colNum))) = False Then
               Range(Cells(rowNumberValue, colNum), Cells(rowBelow, colNum)).Merge

因此,理想情况下,如果一行中有一个单元格缺少数据,而另一行有它,则将数据合并/复制到缺少数据单元格中。

1 个答案:

答案 0 :(得分:1)

替换测试,例如:

IsEmpty(Range(Cells(rowNumberValue, colNum))) = True

具有:

Cells(rowNumberValue, colNum) = ""