在Excel中单行和多行的多列和行

时间:2011-05-09 01:24:53

标签: excel-vba vba excel

我有一张20行5列的工作表。我必须让它出现在一行。

以下是一个示例:

147818  5674    96.30%  4722438 367902
153838  5208    96.73%  4201230 275551
56169   2168    96.28%  3631067 154791
9757    783 92.57%  554416  60998
15063   693 95.60%  792926  52139
7066    399 94.66%  398510  25428
205000  5801    97.25%  7911647 362787
54350   2333    95.88%  2539515 180496
20078   1499    93.05%  1126588 198589
18017   1529    92.18%  822311  81328
37197   1588    95.91%  2119084 194785
62698   3029    95.39%  2335887 166912
79456   2706    96.71%  3085981 327617
15849   958 94.30%  905078  71673
43315   2276    95.01%  2598093 227995
41327   2797    93.66%  1655517 152436
50671   2697    94.95%  2058479 254505
41164   1695    96.05%  1648804 91573

我必须将这个显示放在一行:

147818  5674    96.30%  4722438 367902 153838   5208    96.73%  4201230 275551 56169    2168    96.28%  3631067 154791   ....

你能告诉我任何公式或VBA功能吗?

2 个答案:

答案 0 :(得分:1)

如何通过所有行逐个复制它们。

Sub CombineRows()

Dim i As Integer, N As Integer, M As Integer
Dim row_values() As Variant
Dim r_read As Range, r_write As Range
'Set to top-left of input values
Set r_read = Range("A1")
'Set to top-left of output row
Set r_write = Range("A21")
'Count the rows to read
N = Range(r_read, r_read.End(xlDown)).Rows.Count
'Count the columns to read
M = Range(r_read, r_read.End(xlToRight)).Columns.Count

For i = 1 To N
    'Read an entire row into an array
    row_values = r_read.Offset(i - 1, 0).Resize(1, M).Value
    'Write the row from the array
    r_write.Offset(0, (i - 1) * M).Resize(1, M).Value = row_values
Next i

End Sub

将其粘贴在某处的工作表中,按Alt-F8或选择工具/宏/ 并运行它。确保它指向数据表中的正确位置。

答案 1 :(得分:0)

这会奏效。您需要首先在电子表格中突出显示您的数据:

Sub ConvertToRow()
    Dim cl As Range, cnt As Long, targetRow As Range
    Set targetRow = Range("A1") 'change this for where you want the ouput
    cnt = 0

    For Each cl In Selection
        targetRow.Offset(0, cnt) = cl
        cnt = cnt + 1
    Next cl
End Sub