2013-07-31 32 views
-2

我有一個Excel工作表,看起來像這樣:串聯每隔一行在Excel

3 | latitude | 46.142737 

3 | longitude| -57.608968 

8 | latitude | 43.142737 

8 | longitude| -52.608968 

15 | latitude | 41.142737 

15 | longitude| -59.608968 

我需要最終的結果看起來是這樣:

3 | 46.142737, -57.608968 

8 | 43.142737, -52.608968 

15 | 41.142737, -59.608968 

它可以是基於每隔一行連接,或基於第一列中的整數值。

VBA建議?謝謝。

編輯:沒有實際的「|」在我的Excel工作表中。 「|」旨在成爲代表新專欄的視覺提示。

+0

僅對公式感興趣? – pnuts

+0

您可以使用過濾器並過濾「緯度」。或者你想寫一個可重用的代碼? – Juliusz

回答

1

你可以將數據讀入一個數組,然後寫一個範圍

原始數據:
enter image description here

結果:
enter image description here

代碼:

Sub Example() 
    Dim i As Long 
    Dim x As Long 
    Dim arry As Variant 

    ReDim arry(1 To 2, 1 To 1) As Variant 

    For i = 1 To ActiveSheet.UsedRange.Rows.Count 
     If Cells(i, 1).Row Mod 2 = 1 Then 
      x = x + 1 

      ReDim Preserve arry(1 To 2, 1 To x) As Variant 
      arry(1, x) = Cells(i, 1).Value 
      arry(2, x) = Cells(i, 3).Value & ", " & Cells(i + 1, 3).Value 
     End If 
    Next 

    arry = WorksheetFunction.Transpose(arry) 
    Sheets("Sheet2").Select 
    Range(Cells(1, 1), Cells(UBound(arry), UBound(arry, 2))).Value = arry 
End Sub 
+0

嘿,完全工作!謝謝! – user2364550