将所有行列值转换为仅列格式

时间:2016-11-09 18:45:01

标签: excel csv vbscript

我有excel sheet。请查看"示例更正"片材。

我有多个包含值的行和列。我需要以列为单位格式的所有值。也就是说,我需要2465,2503等所有值,直到331806为单个新列。 (请在colunm" K"表格中)之后忽略任何值。

由于间歇性空行以及中间区域中不需要的文本,我无法应用任何公式或VBA脚本。

参考:https://www.extendoffice.com/documents/excel/1172-excel-transpose-multiple-columns-into-one-column.html

我尝试过VBA脚本,但只有当所有行都连续出现时才有效。

这是我的代码:

Sub TableToColumn()
    Dim Rng As Range, LR As Long, i As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        Set Rng = Range("A" & i, "J" & i) 'Change range to suit needs
        Range("M" & Rows.Count).End(xlUp)(2).Resize(Rng.Count) =     Application.WorksheetFunction.Transpose(Rng)
    Next i
End Sub

此VBA代码提取从A列到J列的所有条目,直到数据存在于行中。如何将此应用到包含更多空行和不需要的原始文本值行的条目表中?

或者,通过CSV迁移然后正则表达式解析是可行的吗?

我需要最简单的解决方案。

1 个答案:

答案 0 :(得分:1)

好吧那我们试试吧......

Public Sub Answer()

Dim RowCnt as Integer 
Dim Output as Variant 
Dim Data as Variant
Data = ThisWorkbook.Worksheets("EXAMPLE CORRECTION").Range("B7:K1384")

' How many rows will we have ?
RowCnt=0
for Row = LBound(Data, 1) to UBound(Data, 1)
    for Col = LBound(Data, 2) to UBound(Data, 2)
        if Not IsEmpty(Data(Row, Col)) and IsNumeric(Data(Row, Col)) Then RowCnt=RowCnt+1;
    Next Col
Next Row    
' Resize Output
Redim Output(1 to RowCnt, 1) as Variant
' Fill Output
RowCnt=1
for Row = LBound(Data, 1) to UBound(Data, 1)
    for Col = LBound(Data, 2) to UBound(Data, 2)
        if Not IsEmpty(Data(Row, Col)) and IsNumeric(Data(Row, Col)) Then
            Output(RowCnt, 1) = Data(Row, Col)
            RowCnt=RowCnt+1
        end if
    next
next
' Write to Some Column (Column L) for now
ThisWorkbook.Worksheets("EXAMPLE CORRECTION").Range("L7:L" & RowCnt+6) = Output

End Sub
相关问题