2012-02-22 202 views
8

是否有可能在中循環合併單元格。在VBA中循環合併單元格

  • 我有6合併單元格區域B4:B40
  • 我需要在這些6個細胞僅6次迭代的值。
+2

你知道有多少個單元格被合併?你爲什麼只需要6次迭代?你能否向我們解釋一下你打算做什麼**? – JMax 2012-02-22 08:27:05

回答

5

這裏是第一捅向您的問題:

Option Explicit 

Sub loopOverCells() 
    Dim rCell As Range 
    Dim i As Integer 

    Set rCell = [B1] 
    For i = 1 To 6 
     Debug.Print rCell.Address 
     Set rCell = rCell.Offset(1, 0) ' Jump 1 row down to the next cell 
    Next i 
End Sub 
+0

+1工作溶劑 – brettdj 2012-02-26 10:02:36

2

只是有點緊張,類似的想法:

Option Explicit 

Sub ListValues() 
Dim i As Long 

    For i = 4 To 40 Step 6 
     Debug.Print Range("B" & i).Value 
    Next i 

End Sub 
6

以上答案看看有你排序。

如果您不知道合併的單元格在哪裏,那麼您可以使用以下例程來快速檢測它們。

當我建立Mappit!我意識到,當我開發了合併單元格的xlBlanks

是一部分,所以你可以使用代碼通過爲MergedCells屬性中的每個單元測試,以檢測合併單元格立即而非循環單元格合併報告是真的。

Sub DetectMerged() 
Dim rng1 As Range 
Dim rng2 As Range 
On Error Resume Next 
Set rng1 = Intersect(Cells.SpecialCells(xlFormulas), Cells.SpecialCells(xlBlanks)) 
Set rng2 = Intersect(Cells.SpecialCells(xlConstants), Cells.SpecialCells(xlBlanks)) 
On Error GoTo 0 
If Not rng1 Is Nothing Then MsgBox "Merged formulae cells in " & rng1.Address(0, 0) 
If Not rng2 Is Nothing Then MsgBox "Merged constant cells in " & rng2.Address(0, 0) 
End Sub 
+1

真是一招! :)至少得到+1 – JMax 2012-02-26 17:54:05