简体   繁体   English

Excel VBA-如何将数据从一列转置到不同列,直到指定字符

[英]Excel VBA - how can I transpose data from one column to different columns until a specified characters

I have a worksheet 1 with multiple rows in column A. I need to transpose them to worksheet 2, then go down 1 row and repeat every time a cell in sheet 1 that begins with "icode: " followed by a code in the same cell. 我有一个工作表1,其中A列中有多行。我需要将它们转置到工作表2中,然后向下移1行,并在工作表1中以“ icode:”开头的单元格中的每个单元格都重复一次。

This is what I have so far to transpose and go down 1 row, but stuck at automating it so it selects all rows in column A of Worksheet 1, until it comes across "icode:" followed by random number and then repeats the entire process. 到目前为止,这是我要转置并向下移动1行的方法,但是一直坚持使其自动化,因此它选择了工作表1的A列中的所有行,直到遇到“ icode:”,然后是随机数,然后重复了整个过程。

Sub Macro6()
'
' Macro6 Macro
'
' Keyboard Shortcut: Ctrl+e
'

    Sheets("Sheet2").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=True
    ActiveCell.Offset(1).Select


End Sub

The data from Worksheet 1, column A looks like this(every line is a different row, including the blank lines): 来自工作表1,A列的数据如下所示(每一行都是不同的行,包括空行):

icode: iA4, type: question

Label: Bu

Responses

1: N

2: G

3: S

4: W

5: F

6: We

icode: iA8, type: question

Label: Red

Responses

1: O

2: R

3: T

4: B

5: On

6: Ont

7: A

icode: iA13, type: question

Label: T
Codeer de laatste keer in de LAATSTE 90 DAGEN

Responses

etc... 等等...

Any ideas? 有任何想法吗?

You can use Power Query it is an add-in for Excel 2010+. 您可以使用Power Query,它是Excel 2010+的加载项。 In Excel 2016 it is known as Get & Transform and comes by default. 在Excel 2016中,它默认为Get&Transform

Put your data as Format as Table and then in the Query Editor use this Power M Code that you can copy and paste in Advance Editor 将数据以“ 格式为表格”形式放置,然后在“ 查询编辑器”中使用此Power M代码 ,您可以将其复制并粘贴到“ 高级编辑器”中

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([Column1] <> null)),
    #"Added Conditional Column" = Table.AddColumn(#"Filtered Rows", "Custom", each if Text.Contains([Column1], "icode") then [Column1] else null),
    #"Filled Down" = Table.FillDown(#"Added Conditional Column",{"Custom"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Custom"}, {{"Group", each _, type table}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Group"}),
    RemoveCustom = Table.TransformColumns(#"Removed Other Columns", {"Group", each Table.RemoveColumns(_,{"Custom"})}),
    #"Added Custom" = Table.TransformColumns(RemoveCustom, {"Group", each Table.Transpose(_)}),
    #"Expanded Group" = Table.ExpandTableColumn(#"Added Custom", "Group", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10"}, {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10"})
in
    #"Expanded Group"

In this video you can find and example about how to use some features in Power Query 在此视频中,您可以找到有关如何使用Power Query中的某些功能的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM