นี่คือแมโคร VBA อื่นที่ควรดำเนินการอย่างรวดเร็วเช่นเดียวกับที่ทำงานในอาร์เรย์ VBA แทนบนแผ่นงาน
มันถือว่าข้อมูลต้นฉบับเริ่มต้นใน A1 หรือ A2; เขตข้อมูลต่อเนื่องกันและอีเมลของแต่ละ บริษัท จะต่อเนื่องกัน (เพื่อให้เซลล์ว่างแรกในแถวอยู่หลังที่อยู่อีเมลสุดท้าย) รหัสจะต้องมีการปรับเปลี่ยนเล็กน้อยหากข้อสันนิษฐานเหล่านั้นไม่เป็นความจริง
นอกจากนี้ยังมีข้อสันนิษฐานว่าไม่มีป้ายชื่อคอลัมน์พร้อมคำแนะนำในการแสดงความคิดเห็นเกี่ยวกับวิธีการชดเชยรหัส
Option Explicit
Sub RowsToColumns()
Dim vSrc As Variant
Dim COL As Collection
Dim wsSrc As Worksheet, wsRes As Worksheet, rRes As Range
Dim S(0 To 1) As String
Dim I As Long, J As Long
'Define source and result worksheets and ranges
'Alter as necessary
Set wsSrc = Worksheets("sheet3")
Set wsRes = Worksheets("sheet4")
Set rRes = wsRes.Cells(1, 1)
'Read source data into array
' This method assumes data starts in A2, and is
' contained in a contiguous array.
'But other methods could be used
vSrc = wsSrc.Cells(2, 1).CurrentRegion
'Collect the results into Collection object
'Assumes no header row, if there is, then start
' with for I = 2 to ...
Set COL = New Collection
For I = 1 To UBound(vSrc, 1) 'the rows
For J = 2 To UBound(vSrc, 2) 'the columns
S(0) = vSrc(I, 1) 'company name
S(1) = vSrc(I, J) 'email
If S(1) <> "" Then
COL.Add S
Else
Exit For 'assumes first blank in email list is end of list
End If
Next J
Next I
'Create results array
ReDim vres(1 To COL.Count, 1 To 2)
For I = 1 To COL.Count
With COL(I)
vres(I, 1) = COL(I)(0)
vres(I, 2) = COL(I)(1)
End With
Next I
'Write the results to worksheet
Set rRes = rRes.Resize(rowsize:=UBound(vres, 1), columnsize:=UBound(vres, 2))
With rRes
.EntireColumn.Clear
.Value = vres
.EntireColumn.AutoFit
End With
End Sub