【问题标题】:Multiple columna and rows to Single Rows.. in ExcelExcel中的多列和多行到单行..
【发布时间】:2011-05-09 15:56:55
【问题描述】:

我有一张 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 函数吗?

【问题讨论】:

    标签: excel vba


    【解决方案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 或选择工具/宏/ 并运行它。确保它指向数据表中的正确位置。

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多