将每2列转置到新行

时间:2016-09-23 22:27:32

标签: excel vba excel-vba transpose

我已经得到了我需要的代码,但我需要一些帮助..这段代码只能用于1行但是。我有多行的数据,如第一行是在A1,A101,A201等等......但它没有使用多行......任何人都可以提供帮助。谢谢! 在屏幕截图1中它是运行脚本之前的状态..它需要每两列并将它放在新行..但它不运行多行...就像我在行A1上有1个可转换数据然后另一个在行A101,A201等拥有..

输入数据:

Input data

Sub dividde_16()

    No_of_columns = Cells(1, Columns.Count).End(xlToLeft).Column
    No_of_rows = Int(No_of_columns / 2) + 1

    For i = 1 To No_of_rows
        For j = 1 To 2
            Cells(i + 1, j) = Cells(i * 2 + j)
        Next
    Next
    Range(Cells(1, 3), Cells(1, No_of_columns)) = ""

End Sub

预期输出:

Expected output

3 个答案:

答案 0 :(得分:0)

我对代码有一点乐趣,但它很有效。

enter image description here

{{1}}

答案 1 :(得分:0)

通过这个通用公式,你可以这样做:

========== RESTART: C:/Users/The Family/Desktop/FireFoxProxyBot.py ==========
Traceback (most recent call last):
  File "C:/Users/The Family/Desktop/FireFoxProxyBot.py", line 4, in <module>
    profile = webdriver.FirefoxProfile()
AttributeError: 'module' object has no attribute 'FirefoxProfile'
>>> 

&#34; cols&#34;是列数。这假设源数据在第1行,结果从第2行开始,如屏幕截图所示。

答案 2 :(得分:0)

从以下示例数据开始。请注意&lt; div&gt;标签成对出现。

linkedin_before

运行此子程序。

Sub wqewqwer()
    Dim rw As Long, iCOLs As Long, iROWs As Long
    Dim a As Long, aTMP1 As Variant, aTMP2 As Variant

    With Worksheets("Sheet12")
        With .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
            If CBool(Application.CountBlank(.Cells)) Then
                .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            End If
        End With

        For rw = .Cells(.Rows.Count, 1).End(xlUp).Row To 1 Step -1
            aTMP1 = .Range(.Cells(rw, 2), .Cells(rw, .Columns.Count).End(xlToLeft)).Value2
            ReDim aTMP2(1 To Int(UBound(aTMP1, 2) / 2), 1 To 2)
            For a = LBound(aTMP1, 2) To UBound(aTMP1, 2) Step 2
                aTMP2(Int(a / 2) + 1, 1) = aTMP1(1, a)
                aTMP2(Int(a / 2) + 1, 2) = aTMP1(1, a + 1)
            Next a
            .Cells(rw + 1, 1).Resize(UBound(aTMP2, 1), 1).EntireRow.Insert
            .Cells(rw + 1, 1).Resize(UBound(aTMP2, 1), UBound(aTMP2, 2)) = aTMP2
            .Range(.Cells(rw, 2), .Cells(rw, .Columns.Count).End(xlToLeft)).Clear
        Next rw
    End With
End Sub

您的结果应与以下内容类似。

linkedin_after