Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA转置循环并在满足条件时开始新行_Vba_Excel - Fatal编程技术网

VBA转置循环并在满足条件时开始新行

VBA转置循环并在满足条件时开始新行,vba,excel,Vba,Excel,我的数据由列分隔,每天由该列中的一个空行分隔。基本上,我需要一个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 083

我的数据由列分隔,每天由该列中的一个空行分隔。基本上,我需要一个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

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

这应该满足您的要求

编辑此代码基于与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
子blnkrows()
Dim arr()作为变量
Dim p作为整数,i&
将ws设置为工作表
将tws设置为工作表
作为整数的Dim t
尺寸c与长度相同
把你的头发调暗
设置ws=ActiveSheet
设置tws=工作表(“表2”)
i=1
与ws
直到我超过100000为止
u=0
对于c=1到.Cells(1,.Columns.Count).End(xlToLeft).Column
ReDim arr(0)作为变量
p=0
t=0
直到。单元格(i+p,c)=“和t=1
如果.Cells(i+p,c)=“那么
t=1
其他的
arr(UBound(arr))=.单元(i+p,c)
重播保留arr(UBound(arr)+1)
如果结束
p=p+1
环
如果p>u那么
u=p
如果结束
如果c=.Cells(1,.Columns.Count).End(xlToLeft).Column,则
如果.Cells(i+p,c).End(xlDown).Row>100000和.Cells(i+p,1).End(xlDown).Row<100000,则
i=.Cells(i+u,1).End(xlDown).Row
其他的
i=.Cells(i+p,c).End(xlDown).Row
如果结束
如果结束
tws.Cells(tws.Rows.Count,1).End(xlUp).Offset(1).Resize(,UBound(arr)+1)=arr
下一个c
环
以
与tws
。行(1)。删除
对于i=.Cells(1,1).End(xlDown).Row到2步骤-1
如果左(.Cells(i,1),4)左(.Cells(i-1,1),4),则
.行(i).EntireRow.Insert
如果结束
接下来我
以
端接头

列表是否始终采用相同的模式—2行数据,1行空白,4行数据,1行空白?还是会变?会变的。当新的一天开始时,总是有空行。有时有5行数据,有时有10行。这取决于它总是以同样的方式开始。2行数据1个空白,但它变了我得到了以下错误:“下标超出范围”这是什么原因?此外,它还省略了第4行数据,并且只对这两列进行循环。在那之后,它不会吐出正确的data@Ben哪一行抛出错误,我猜是
Set tws=Worksheets(“Sheet2”)
。如果是这样,则表示您没有“sheet2”,或者在代码中将其重命名为所需的图纸,或者添加“sheet2”Yup。我明白了。另一个问题是第一列的操作是否正确,但我有6列数据。是否有一个简单的加法可以使这条命令适用于所有6列。@Ben我在原来的问题中遗漏了这一部分。我已经更新了代码。更新后的代码实际上让事情变得更糟。它中途切断了我的数据,并将数据放在错误的位置。