demi asked in 電腦與網際網路軟體 · 8 years ago

EXCEL VBA 合併語法

圖片參考:http://imgcld.yimg.com/8/n/AC05357371/o/1512052102...

請問各位大大, EXCEL的C欄遇到@的話,在B欄合併兩列的VBA語法如何寫,謝謝,例如:C2有@,則B2和B3 合併成一格,C4有@,則B4和B5 合併成一格

Update:

大家都好厲害哦...也謝謝准提部林和Dianel大大提供的意見作法,若要再把合併的儲存格B欄靠上對齊,要再加哪個語法呢,謝謝

Update 2:

謝謝准提部林大大..

3 Answers

Rating
  • 8 years ago
    Best Answer

    Sub 合併()

    Dim a, b As String

    Range("C2").Select

    b = 1

    ActiveCell.End(xlDown).Select

    a = Selection.Row

    Do Until b = a

    If Left(Cells(b, 3).Value, 1) = "@" Then

    Range("B" & b & ":B" & (b + 1)).Select

    Selection.Merge

    End If

    b = b + 1

    Loop

    End Sub

    Source(s): 自己
  • 8 years ago

    Sub 合併()

    For Each xR In Range("C2", [C65536].End(xlUp))

     If Left(xR, 1) = "@" Then

      xR(2, 0) = "": xR(1, 0).Resize(2).Merge

     End If

    Next

    End Sub

    2012-05-21 19:05:47 補充:

    Sub 合併()

    For Each xR In Range("C2", [C65536].End(xlUp))

     If Left(xR, 1) = "@" Then

      xR(2, 0) = ""

    With xR(1, 0): .Resize(2).Merge: .VerticalAlignment = xlTop: End With

     End If

    Next

    End Sub

  • Daniel
    Lv 7
    8 years ago

    Sub TT()

    Application.DisplayAlerts = False

    R = [C65536].End(3).Row - 1

    For i = 2 To R

    If Cells(i, 3) Like "@*" Then Cells(i, 2).Resize(2).Merge

    Next

    Application.DisplayAlerts = True

    End Sub

Still have questions? Get your answers by asking now.