Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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_Loops - Fatal编程技术网

Vba 添加附加循环

Vba 添加附加循环,vba,excel,loops,Vba,Excel,Loops,我有以下VBA代码: Sub test() Dim w1 As Worksheet Dim w2 As Worksheet Dim k As Long Dim c As Range Dim d As Range Dim strFA As String Set w1 = Sheets("a") Set w2 = Sheets("b") w2.Cells.Clear k = 1 With w1.Rang

我有以下VBA代码:

Sub test()
    Dim w1 As Worksheet
    Dim w2 As Worksheet

    Dim k As Long

    Dim c As Range
    Dim d As Range
    Dim strFA As String

    Set w1 = Sheets("a")
    Set w2 = Sheets("b")

    w2.Cells.Clear
    k = 1

    With w1.Range("A:A")
        Set c = .Cells.Find("Order" After:=.Cells(.Cells.Count), lookat:=xlWhole)
        strFA = ""
        While Not c Is Nothing And strFA <> c.Address
            If strFA = "" Then strFA = c.Address
            If IsError(Application.Match(c.Offset(1, 0).value, w2.Range("A:A"), False)) Then
                Set d = .Cells.Find("Item", c, , xlWhole)
            w2.Range("A" & k).value = c.Offset(0, 1).value
            w2.Range("B" & k).value = d.Offset(0, 2).value
            w2.Range("C" & k).value = d.Offset(0, 3).value
            w2.Range("D" & k).value = d.Offset(0, 4).value
            w2.Range("E" & k).value = d.Offset(0, 5).value
            w2.Range("F" & k).value = d.Offset(1, 1).value
            w2.Range("G" & k).value = d.Offset(1, 2).value
            w2.Range("H" & k).value = d.Offset(1, 3).value
            w2.Range("I" & k).value = d.Offset(1, 4).value
            w2.Range("J" & k).value = d.Offset(1, 5).value
                k = k + 1
            End If
            Set c = .Cells.Find("Item", After:=c, lookat:=xlWhole)
        Wend
    End With

End Sub
我想要的基本上是以下输出:

Col1   Col2  Col3     Col4
Order1  A     Item1    23
Order1  B     Item3    24
Order1  C     Item4    57
Order1  C     Item5    89 
Order2  A     Item2    145
Order2  C     Item3    4
Order2  A     Item5    42

有没有人有有效的方法来做到这一点?谢谢

这在您的样本数据上对我有效:

Sub ExtractOrderItems()
    Const MAX_BLANK As Long = 100
    Dim c As Range, numBlank As Long, d As Range
    Dim sOrder As String, tmp, inItems As Boolean

    Set c = ActiveSheet.Range("A1")
    Set d = ThisWorkbook.Sheets("Items").Range("A2")
    'If putting the items in a different workbook from the one
    '  containing the code you'd use:
    'Set d = Workbooks("ListBook.xlsx").Sheets("Items").Range("A2")

    numBlank = 0
    sOrder = ""

    'loop until we've run through MAX_BLANK empty cells....
    Do While numBlank < MAX_BLANK
        tmp = c.Value
        If Len(tmp) > 0 Then

            If tmp Like "Order*" Then
                sOrder = tmp
                inItems = False
            Else
                If Trim(c.Value) = "Item" Then
                    inItems = True
                Else
                    If inItems Then
                        d.Resize(1, 4).Value = Array(sOrder, c.Value, c.Offset(0, 1).Value, _
                                                  c.Offset(0, 2).Value)
                        Set d = d.Offset(1, 0)
                    End If
                End If
            End If

            numBlank = 0
        Else
            numBlank = numBlank + 1
        End If
        Set c = c.Offset(1, 0)
    Loop

End Sub
Sub-ExtractOrderItems()
常数最大值为空,长度=100
尺寸c为射程,长度d为射程
Dim排序为字符串,tmp,inItems为布尔值
设置c=ActiveSheet.Range(“A1”)
集合d=此工作簿。工作表(“项目”)。范围(“A2”)
'如果将项目放在不同的工作簿中
'包含您将使用的代码:
'集合d=工作簿(“ListBook.xlsx”).工作表(“项目”).范围(“A2”)
numBlank=0
sOrder=“”
'循环,直到我们运行完最大空白单元格。。。。
当numBlank0,则
如果tmp喜欢“订单*”,那么
排序=tmp
inItems=False
其他的
如果修剪(c.值)=“项目”,则
inItems=True
其他的
如果是这样的话
d、 调整大小(1,4).Value=数组(排序、c.值、c.偏移量(0,1).Value、_
c、 偏移量(0,2)。值)
设置d=d偏移量(1,0)
如果结束
如果结束
如果结束
numBlank=0
其他的
numBlank=numBlank+1
如果结束
设置c=c.偏移量(1,0)
环
端接头

我昨天做了类似的事情,你可能会适应吗?您只需要第二部分,因为列A已经填充。这对于显示输入数据的示例很有用。您可以显示一些输入数据吗。所以,我们可以想得更多。@findwindow谢谢,我来看看这个@TimWilliams编辑了一个有点混乱但准确的例子。正如您可能注意到的那样,间距是不可靠的,并且永远不清楚何时会出现“项”或其中会出现多少项。提供的文档的布局很大程度上就是我这样做的原因。我在“Set d”行上得到一个错误“subscript out out range”。我已将项目更改为正确的工作表名称,但仍然没有运气。是否有简单的修复方法?
此工作簿
包含VBA代码:如果这不是目标工作表的父项,则需要调整该行。谢谢,修复了它。不幸的是,当我尝试遍历代码时,没有显示任何内容(也就是说,循环迭代,但单元格留空)。我将尝试进一步调试。您是从“输入”表作为活动表开始的吗?非常感谢,我是,我太傻了,它现在工作得很好。最后一个问题:如果我真的想通过PERSONAL.XLSB运行这个,我会怎么做?这就是我运行它的地方,之前我遇到了第一个错误。
Sub ExtractOrderItems()
    Const MAX_BLANK As Long = 100
    Dim c As Range, numBlank As Long, d As Range
    Dim sOrder As String, tmp, inItems As Boolean

    Set c = ActiveSheet.Range("A1")
    Set d = ThisWorkbook.Sheets("Items").Range("A2")
    'If putting the items in a different workbook from the one
    '  containing the code you'd use:
    'Set d = Workbooks("ListBook.xlsx").Sheets("Items").Range("A2")

    numBlank = 0
    sOrder = ""

    'loop until we've run through MAX_BLANK empty cells....
    Do While numBlank < MAX_BLANK
        tmp = c.Value
        If Len(tmp) > 0 Then

            If tmp Like "Order*" Then
                sOrder = tmp
                inItems = False
            Else
                If Trim(c.Value) = "Item" Then
                    inItems = True
                Else
                    If inItems Then
                        d.Resize(1, 4).Value = Array(sOrder, c.Value, c.Offset(0, 1).Value, _
                                                  c.Offset(0, 2).Value)
                        Set d = d.Offset(1, 0)
                    End If
                End If
            End If

            numBlank = 0
        Else
            numBlank = numBlank + 1
        End If
        Set c = c.Offset(1, 0)
    Loop

End Sub