2017-06-06 90 views
0

enter image description here 我想合併那章重複章只到一個單元格。Excel VBA:合併循環內的範圍

這是我的代碼如何循環。

 Dim label As Control 
     Dim itm As Object 
     For ctr = 1 To InfoForm.Chapter.ListCount - 1 
      For Each label In InfoForm.Controls 
       If TypeName(label) = "Label" Then 
        With ActiveSheet 
         i = i + 1 

         lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + IIf(i = 1, 1, 0) 
         lastColumn = .Cells(i, .Columns.Count).End(xlToLeft).Column 

         If label <> "Chapter" Then 
          .Cells(lastColumn, i).Value = "Chapter " & ctr 

          .Cells(lastRow, i).Value = label.Caption 
         End If 
        End With 
       End If 
      Next 
     Next 

我試圖合併它像這樣

.Range(Cells(1, lastColumn), Cells(1,i)).Merge 

不過它把所有重複的章節爲一個單元,而不是

預期結果: enter image description here

+0

你能提供預期輸出的例子嗎? – MiguelH

+0

這是我的預期結果 –

+0

我發現有關表單控件的代碼有點混亂......你只是試圖合併一堆保持相同值的單元格,不是嗎? –

回答

1

我的方法是波紋管

Dim label As Control 
    Dim itm As Object 
    For ctr = 1 To InfoForm.Chapter.ListCount - 1 
     For Each label In InfoForm.Controls 
      If TypeName(label) = "Label" Then 
       With ActiveSheet 
        i = i + 1 

        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + IIf(i = 1, 1, 0) 
        lastColumn = .Cells(i, .Columns.Count).End(xlToLeft).Column 

        If label <> "Chapter" Then 
         .Cells(lastColumn, i).Value = "Chapter " & ctr 

         .Cells(lastRow, i).Value = label.Caption 
        End If 
       End With 
      End If 
     Next 
    Next 

    'this is merge method 
    Dim rngDB As Range, rng As Range, n As Integer 

    Application.DisplayAlerts = False 
    Set rngDB = Range("a1", Cells(1, Columns.Count).End(xlToLeft)) 
    For Each rng In rngDB 
     If rng <> "" Then 
      n = WorksheetFunction.CountIf(rngDB, rng) 
      rng.Resize(1, n).Merge 
      rng.HorizontalAlignment = xlCenter 
     End If 
    Next rng 
    Application.DisplayAlerts = True 
+0

這有效。非常感謝。 –

+0

Hi @ Dy.Lee,如果你可以,你可以問一下你的代碼的解釋。我的意思是發生了什麼,它是如何工作的。 –

+0

@HydesYase:代碼原理非常簡單。當單元格合併時,單元格爲空。因此,具有相同值的其他單元是空單元,也就是第一單元。合併方法應用於第一個單元格(如果rng <>「」)。在該範圍內,您可以通過worksheetfunctoion.countif來計算具有相同值的單元格。您可以合併通過調整大小(行,列)方法計算的單元格。 –

0

如果你知道然後你可以調整下面的代碼。我已經通過錄制宏創建了這個功能,然後根據需要禁用/啓用警報。我已經包含了一個函數來將整數列值轉換爲alph等值。MainLoopIntcol1intcol2應該是基於來自原始表單的輸入提供的值。

Sub MainLoop() 
Dim StrMycol_1 As String 
Dim StrMycol_2 As String 
Dim intcol1 As Integer 
Dim intcol2 As Integer 

    intcol1 = 5: intcol2 = 7 

    StrMycol_1 = WColNm(intcol1) ' mycell.column is numeric. Function returns integer 
    StrMycol_2 = WColNm(intcol2) ' mycell.column is numeric. Function returns integer 
' 
    do_merge_centre StrMycol_1, StrMycol_2 
End Sub 

Sub do_merge_centre(col1, col2) 
Range(col1 + "1:" + col2 + "1").Select 
Application.DisplayAlerts = False 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlBottom 
    .WrapText = False 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
Application.DisplayAlerts = True 
End Sub 
' 
Public Function WColNm(ColNum) As String 
    WColNm = Split(Cells(1, ColNum).Address, "$")(1) 
End Function 
+0

我有一個用戶表單,我正在循環其中的控件來確定範圍。將來,我可能想要爲該用戶表單添加更多控件,並且我認爲如果每次都必須更改範圍,可能會很麻煩。這就是爲什麼我要循環控制,以便它自動執行此操作。 –

+0

那麼如果你知道有多少次重複,那麼你可以將上面的代碼作爲一個子程序,並傳入所需的範圍值(即將列號更改爲等效的字母字符) – MiguelH

+0

@HydesYase。查看將數字列轉換爲alpha列範圍的更新答案 – MiguelH

0

這個怎麼樣?

With ActiveSheet 
    firstCol = 1 
    lastCol = .Cells(1, .Cells.Columns.Count).End(xlToLeft).Column 
    For i = 1 To lastCol 
    If .Cells(1, i) = "" Then GoTo NextCol 'skip blank cell 

    If firstCol = 0 And .Cells(1, i) <> "" Then firstCol = i 'set first column 

    If .Cells(1, i) = .Cells(1, i + 1) Then 
     LastColDup = i 'remember last duplicate column 
    Else 
     Application.DisplayAlerts = False 
     With .Range(Cells(1, firstCol), Cells(1, LastColDup + 1)) 
      .Merge 
      .HorizontalAlignment = xlCenter 
     End With 
     Application.DisplayAlerts = True 
     firstCol = 0 
     LastColDup = 0 
    End If 
NextCol: 
    Next i 
End With