0

只有当我从 A22-78 中选择并 ctrl C 行时,此宏才有效。我希望它在我不必这样做的情况下工作。此函数从 (#rows)*(每行中的#columns) 中生成一列

Sub RowsToColumn()
Dim RN As Range
Dim RI As Range
Dim r As Long
Dim LR As Long
Dim WS As Worksheet
Set WS = Sheets.Add
Application.ScreenUpdating = False

Columns(1).Insert
r = 0
LR = Range("B" & Rows.Count).End(xlUp).Row
For Each RN In Range("B22:B" & 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
Application.ScreenUpdating = True
End Sub

感谢你

4

1 回答 1

1

我尝试了您的代码,它似乎做的是从左到右读取值并将它们按相应顺序全部放在 A 列中。这可以帮助:

Sub Test()

Dim oRange          As Range
Dim oSheet          As Excel.Worksheet
Dim vArray()        As Variant

Dim lCnt_A          As Long
Dim iCnt_B          As Integer
Dim lCnt_C          As Long
Dim iCnt_Cols       As Integer


Set oSheet = ThisWorkbook.Sheets(1)

Columns(1).Insert
Set oRange = oSheet.UsedRange
iCnt_Cols = oRange.Columns.Count

vArray = oRange
oRange.ClearContents

For lCnt_A = 1 To UBound(vArray)
    For iCnt_B = 1 To iCnt_Cols
        lCnt_C = lCnt_C + 1
        ThisWorkbook.Sheets(2).Cells(lCnt_C, 1).Value = vArray(lCnt_A, iCnt_B)

    Next iCnt_B
Next lCnt_A

Set oSheet = Nothing
Set oRange = Nothing

End Sub

请告诉我我是否理解错了你的意图。

于 2012-06-13T08:24:33.047 回答