VBA转置循环并在符合条件时开始新行

时间:2015-10-27 19:11:18

标签: excel vba excel-vba

我的数据按列分隔,每天由该列中的空行分隔。基本上我需要一个VBA宏来制作这些数据:

1995 (1)
(23:00)

Math 0630
0830 Break 0930
1000 English 1200
1200 Lunch 1300

1995 (2)
(12:45)

Chemistry 0630
0830 Lab 0930
1000 Bio 1200
1200 Lunch 1300

在新表中显示如下:

1995 (1)    (23:00) Math 0630   0830 Break 0930 1000 English 1200   1200 Lunch 1300 
1995 (2)    (12:45) Chemistry 0630  0830 Lab 0930   1000 Bio 1200   1200 Lunch 1300 

我还需要vba代码在新的一天开始时分隔每一行。有人可以帮忙吗?

这是我到目前为止所拥有的......

    Sub blnkrows()
Do
    p = p + 20
    If Rows(p).Find("*") Is Nothing Then Exit Do
Loop
    y = Range(Rows(1), Rows(p))
    With Sheets("Sheet2")
    Range(.Rows(1), .Rows(p)) = y
    End With
End Sub

但是这只会将数据复制到新工作表中。

1 个答案:

答案 0 :(得分:0)

这应该按照你的要求进行

编辑此代码基于与OP的私人对话。对于需要更多审核的模式存在特质。

Sub blnkrows()
Dim arr() As Variant
Dim p As Integer, i&
Dim ws As Worksheet
Dim tws As Worksheet
Dim t As Integer
Dim c As Long
Dim u As Long



Set ws = ActiveSheet
Set tws = Worksheets("Sheet2")
i = 1
With ws
Do Until i > 100000
    u = 0
    For c = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column
        ReDim arr(0) As Variant
        p = 0
        t = 0
            Do Until .Cells(i + p, c) = "" And t = 1
                If .Cells(i + p, c) = "" Then
                    t = 1

                Else
                    arr(UBound(arr)) = .Cells(i + p, c)
                    ReDim Preserve arr(UBound(arr) + 1)
                End If
                p = p + 1
            Loop

        If p > u Then
            u = p

        End If
        If c = .Cells(1, .Columns.Count).End(xlToLeft).Column Then
            If .Cells(i + p, c).End(xlDown).Row > 100000 And .Cells(i + p, 1).End(xlDown).Row < 100000 Then
                i = .Cells(i + u, 1).End(xlDown).Row
            Else
                i = .Cells(i + p, c).End(xlDown).Row
            End If

        End If
        tws.Cells(tws.Rows.Count, 1).End(xlUp).Offset(1).Resize(, UBound(arr) + 1) = arr

    Next c

Loop
End With
With tws
    .Rows(1).Delete
    For i = .Cells(1, 1).End(xlDown).Row To 2 Step -1
        If Left(.Cells(i, 1), 4) <> Left(.Cells(i - 1, 1), 4) Then
            .Rows(i).EntireRow.Insert
        End If
    Next i
End With
End Sub