|
1, 设表中A1:B2区域已合并,则有如下位移操作区别:- Sub bbb() '对合并区域的位移操作
- MsgBox Range("a1")(2, 2).Address '显示b2地址
- MsgBox Range("a1").Offset(1, 1).Address '显示c3地址,但当a1非合并单元格,则显示b2地址
- End Sub
复制代码 2, 合并一列中连续相同的单元格并保留单元格中的数据:
当然, 这个目的可以用手动操作实现, 论坛里有方法, 此帖不再叙述.
下面贴出用VBa实现的方法(核心思想为拷贝格式:PasteSpecial xlPasteFormats)
代码1:- Sub bbb1() '适用于单列区域
- With [e3:e19]
- .Offset(0, 1).EntireColumn.Insert
- For i = 1 To .Count - 1
- If .Cells(i) = .Cells(i + 1) Then .Cells(i).Offset(0, 1).Resize(2, 1).Merge
- Next
- .Offset(0, 1).Copy
- .PasteSpecial xlPasteFormats
- .Offset(0, 1).EntireColumn.Delete
- End With
- End Sub
复制代码 代码2:- Sub bbb3() '高速合并
- Dim i%, y%, arr1(), arr2()
- With [e3:e19]
- On Error Resume Next
- Application.DisplayAlerts = False
- arr1 = [e3:e19].Value
- ReDim arr2(1 To UBound(arr1), 1 To 1)
- For i = 1 To UBound(arr1) - 1
- If arr1(i, 1) = arr1(i + 1, 1) Then
- If y Mod 2 = 0 Then
- arr2(i, 1) = 1: arr2(i + 1, 1) = 1
- Else
- arr2(i, 1) = "#N/A": arr2(i + 1, 1) = "#N/A"
- End If
- Else
- y = y + 1
- End If
- Next
- .Offset(0, 1).EntireColumn.Insert
- .Offset(0, 1).Value = arr2
- .Offset(0, 1).SpecialCells(2, 1).Merge
- .Offset(0, 1).SpecialCells(2, 16).Merge
- .Offset(0, 1).Copy
- .PasteSpecial xlPasteFormats
- .Offset(0, 1).EntireColumn.Delete
- Application.DisplayAlerts = True
- If Err.Number <> 0 Then Err.Clear: On Error GoTo 0
- End With
- End Sub
复制代码 3, 操作用附件:
合并单元格并保留数据.zip
(11.41 KB, 下载次数: 832)
[ 本帖最后由 lb_bn 于 2009-12-22 18:08 编辑 ] |
|