2017-07-27 47 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