0

我从来没有做过 Excel VBA 宏。

我试图组织成单列的数据在 excel 行 22-78 中。

    0   0.04    0.08    0.12    0.16    0.2 0.24    0.28    0.32    0.36    0.4 0.44    0.48    0.52    0.56    0.6 0.64    0.68    0.72    0.76    0.8 0.84    0.88    0.92    0.96    1   1.04    1.08    1.12    1.16    1.2 1.24    1.28    1.32    1.36    1.4 1.44    1.48    1.52    1.56    1.6 1.64    1.68    1.72    1.76    1.8 1.84    1.88    1.92    1.96    2   2.04    2.08    2.12    2.16    2.2 2.24    2.28    2.32    2.36    2.4 2.44    2.48    2.52    2.56    2.6 2.64    2.68    2.72    2.76    2.8 2.84    2.88    2.92    2.96    3   3.04    3.08    3.12    3.16    3.2 3.24    3.28    3.32    3.36    3.4 3.44    3.48    3.52    3.56    3.6 3.64    3.68    3.72    3.76    3.8 3.84    3.88    3.92    3.96    4   4.04    4.08    4.12    4.16    4.2 4.24    4.28    4.32    4.36    4.4 4.44    4.48    4.52    4.56    4.6 4.64    4.68    4.72    4.76    4.8 4.84    4.88    4.92    4.96    5   5.04    5.08    5.12    5.16    5.2 5.24    5.28    5.32    5.36    5.4 5.44    5.48    5.52    5.56    5.6 5.64    5.68    5.72    5.76    5.8 5.84    5.88    5.92    5.96    6   6.04    6.08    6.12    6.16    6.2 6.24    6.28    6.32    6.36    6.4 6.44    6.48    6.52    6.56    6.6 6.64    6.68    6.72    6.76    6.8 6.84    6.88    6.92    6.96    7   7.04    7.08    7.12    7.16    7.2 7.24    7.28    7.32    7.36    7.4 7.44    7.48    7.52    7.56    7.6 7.64    7.68    7.72    7.76    7.8 7.84    7.88    7.92    7.96    8   8.04    8.08    8.12    8.16    8.2 8.24    8.28    8.32    8.36    8.4 8.44    8.48    8.52    8.56    8.6 8.64    8.68    8.72    8.76    8.8 8.84    8.88    8.92    8.96    9   9.04    9.08    9.12    9.16    9.2 9.24    9.28    9.32    9.36    9.4 9.44    9.48    9.52    9.56    9.6 9.64    9.68    9.72    9.76    9.8 9.84    9.88    9.92    9.96    10  10.04   10.08   10.12   10.16   10.2    10.24   10.28   10.32   10.36   10.4    10.44   10.48   10.52   10.56   10.6    10.64   10.68   10.72   10.76   10.8    10.84   10.88   10.92   10.96   11  11.04   11.08   11.12   11.16   11.2    11.24   11.28   11.32   11.36   11.4    11.44   11.48   11.52   11.56   11.6    11.64   11.68   11.72   11.76   11.8    11.84   11.88   11.92   11.96   12  12.04   12.08   12.12   12.16   12.2    12.24   12.28   12.32   12.36   12.4    12.44   12.48   12.52   12.56   12.6    12.64   12.68   12.72   12.76   12.8    12.84   12.88   12.92   12.96   13  13.04   13.08   13.12   13.16   13.2    13.24   13.28   13.32   13.36   13.4    13.44   13.48   13.52   13.56   13.6    13.64   13.68   13.72   13.76   13.8    13.84   13.88   13.92   13.96   14  14.04   14.08   14.12   14.16   14.2    14.24   14.28   14.32   14.36   14.4    14.44   14.48   14.52   14.56   14.6    14.64   14.68   14.72   14.76   14.8    14.84   14.88   14.92   14.96   15  15.04   15.08   15.12   15.16   15.2    15.24   15.28   15.32   

这是一行中的数据。我从第 22-78 行就有这样的。最终文件的列数相似,但行数更多。我不确定在excel中将其组织成单个列的好方法是什么

我让这个工作了 1 行。这是代码

Sub RowsToColumn()
   Dim RN As Range
   Dim RI As Range
   Dim r As Long
   Dim LR As Long
   Application.ScreenUpdating = False
   Columns(1).Insert
   r = 0

   LR = Range("A" & Rows.Count).End(xlUp).row
   For Each RN In Range("A1:A" & LR)
       r = r + 1
       For Each RI In Range(RN, Range("XFD" & RN.row).End(xlToLeft))
           r = r + 1
           Cells(r, 1) = RI
           RI.Clear
       Next RI
   Next RN
   Columns("A:A").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
End Sub

但要将其扩展到 A22-78 行

Sub RowsToColumn_Second()
   Dim RN As Range
   Dim RI As Range
   Dim r As Long
   Dim LR As Long
   Dim row As Range
   Dim rng As Range
   Dim cell As Range

   Application.ScreenUpdating = False
   Set rng = Range("A22:A78")
   For Each row In rng.Rows
      Columns(1, rng).Insert
   r = 0
   LR = Range("A" & Rows.Count).End(xlUp).row
   LR = Range("A" & Rows.Count).End(xlUp).row
   For Each RN In Range("A1:A" & LR)
       r = r + 1
       For Each RI In Range(RN, Range("XFD" & RN.row).End(xlToLeft))
           r = r + 1
           Cells(r, 1) = RI
           RI.Clear
       Next RI
   Next RN
   Next row
   Columns("A:A").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
End Sub

这是它说应用程序定义错误1004的地方。它不喜欢 Columns(1, rng).Insert

4

1 回答 1

0

复制数据和Paste Special-> Transpose,这将从行变为列,反之亦然

于 2012-06-11T18:26:18.670 回答