将三行中的地址数据转置为列?

Transpose address data in three rows to columns?

我有这样的数据:

Pharmacy
200 Main Street
San Diego, CA 11111
Liquor store
315 South 4th
Minot, ND 22222
County courthouse
111 West Hwy 45
West Allis, WI 33333
Bowling alley
16542 North Park St
Huntsville, AL 01010
...

但我希望它看起来像这样:

Name                 Address           City/state
Pharmacy         200 Main Street    San Diego, CA 11111
Liquor store      315 South 4th       Minot, ND 22222
...

数据从 A1:A1620 开始以这种方式继续。我需要将这种格式的数据转置为三个变量:名称、地址、city/state。例如,"Pharmacy" 将是第一个案例的名称,“200 Main Street”将是其地址,而 "San Diego, CA 11111" 将是其 city/state.

怎么样 - 这是一个 VBA 解决方案。 (在 Excel 中,右键单击您的 sheet 名称,然后单击 "View Code" 并将其放置在那里):

Sub fixText()
Dim i As Integer, lastRow As Integer
Dim addressCol As Integer, cityCol As Integer

'Insert header row and add headers
Rows(1).EntireRow.Insert
Cells(1, 1).Value = "Name"
Cells(1, 2).Value = "Address"
Cells(1, 3).Value = "City/State"
addressCol = 2
cityCol = 3


'Find the last used row
lastRow = ActiveSheet.UsedRange.Rows.Count

For i = 2 To lastRow
    If i > lastRow Then Exit For
    Cells(i, addressCol).Value = Cells(i, 1).Offset(1, 0).Value
    Cells(i, cityCol).Value = Cells(i, 1).Offset(2, 0).Value

    ' Delete the two rows you just copied info from
    Range(Cells(i + 1, 1), Cells(i + 2, 1)).EntireRow.Delete

    'Get new last row
    lastRow = ActiveSheet.UsedRange.Rows.Count
Next i

End Sub

注意:我假设您的数据从 A1 开始,并且在一个大块中,因此最后使用的行是相关数据。如果这需要调整,请告诉我!