2017-07-27 48 views
-2

我已經得到了組織一個這樣的龐大的Excel(2010)文件:Excel中如何組織重複數據

  • 列1列2
    1. 名稱約翰
    2. 姓伊
    3. 地址smth1
    4. ....
    5. 名稱Janet
    6. 姓史密斯
    7. 地址smth2
    8. ....

現在我想將其轉換成適當頭普通表,所以它看起來是這樣的:



個 名姓地址....
李四smth1
珍妮特·史密斯smth2

PS這是我第一次來這裏發帖。大家好!

+3

本網站不是腳本撰寫服務。請閱讀[如何提出一個好問題](https://stackoverflow.com/help/how-to-ask),然後編輯您的帖子並在關閉之前將其改爲一個很好的問題。 – teylyn

+0

你對每個人有多少項數據?這是相同的數額? –

回答

0

您可以通過將以下公式單元格A1在一個新的工作表,並橫跨上下複製它所有的數據複製到另一個工作表在工作簿中。

=OFFSET(Sheet1!$A$1,3*(ROW(A1)-1)+COLUMN(A1)-1,1) 

我已經假設您的數據在Sheet1中,並且它從單元格A1開始。將工作表1的實際名稱更改爲Sheet1,並更改$ A $ 1作爲工作表中數據的實際開始($符號很重要)。

我還假設每個人都有3條數據。如果還有更多,請將公式中的數字3更改爲數據的數量。

一旦使用了這些公式,如果您想使這些更改永久生效,請複製新數據並粘貼爲值。這會破壞公式,但保留完整的數據。

0

如果您的數據非常具體地包含您詢問的數據,則下面的內容應該可以工作。運行代碼之前,您需要:

  1. 指定輸出範圍
  2. 指定數據是否已經按照您的例子
  3. 編號指定的列數你的數據有
  4. 突出顯示輸入數據範圍

此代碼僅基於我根據您的問題中提供的有限信息所做的假設工作。

希望這會有所幫助。

Sub TextToTable() 

    Dim Temp As Variant, x As Long, i As Long, j As Long 
    Dim MyInputRange As Range 
    Dim MyOutputRange As Range 
    Dim MyOutput As Variant 
    Dim HasNumbering As Boolean 

    '=============================================================== 
    'You MUST update the below before running the code 
    '=============================================================== 

    'Highlight the range you want to convert before running this macro 
    Set MyInputRange = Selection 

    'Put in the output address (sheet name and range) 
    Set MyOutputRange = ThisWorkbook.Worksheets("SheetNameHere").Range("A1") 

    'Set this to false if the data doesn't have "1." and "2." etc at the start of each row 
    HasNumbering = True 

    'Tell the script how many columns your data has 
    Const ColumnCount As Long = 4 
    '=============================================================== 

    'The below doesn't require your updating 

    Temp = MyInputRange.Value 'Temp is used to input the data from sheet 

    ReDim MyOutput(1 To UBound(Temp, 1), 1 To ColumnCount) 

    'Scrubs out numbering as required 
    If HasNumbering Then 
    For x = 1 To UBound(Temp, 1) 
     j = InStr(1, Temp(x, 1), ". ") 
     Temp(x, 1) = Right(Temp(x, 1), Len(Temp(x, 1)) - (j + 1)) 
    Next x 
    End If 

    'Sets the table heading 
    i = 1 
    For x = 1 To ColumnCount 
    MyOutput(i, x) = Left(Temp(x, 1), WorksheetFunction.Max(InStr(1, Temp(x, 1), " ") - 1, 0)) 
    Next x 

    'Builds the table data 
    j = 0: i = 2 
    For x = 1 To UBound(Temp, 1) 
    j = j + 1 
    If j > ColumnCount Then j = 1: i = i + 1 
    MyOutput(i, j) = Mid(Temp(x, 1), Len(MyOutput(1, j)) + 2, 9999) 
    Next x 

    'Outputs the data 
    MyOutputRange.Resize(i, ColumnCount).Value = MyOutput 

End Sub