Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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
Excel 如何在遇到更改之前自动填充列中的内容。以“;停止”;在牢房里_Excel_Vba - Fatal编程技术网

Excel 如何在遇到更改之前自动填充列中的内容。以“;停止”;在牢房里

Excel 如何在遇到更改之前自动填充列中的内容。以“;停止”;在牢房里,excel,vba,Excel,Vba,我需要一个代码,动态填充包含单词“Apple”的列中的数字 搜索第2行中的单词:Apple。(这是查找数据所在列的方式。) 然后,在“苹果”所在的列中,检查数字0到X 0。。。。。。。。1.3.4.5.X(不规则)。数字之间有空行。 复制每个数字,直到数字更改。从0开始。 将结果粘贴到下面的rov中。 重复此步骤,直到在列中找到“停止”一词 结果应该在列中 答:BM是要搜索的列。 “苹果”这个词在第二排,但我不知道在哪里。因此,必须首先搜索包含数据的列。 下面是一个在“C”列中查找单词Apple

我需要一个代码,动态填充包含单词“Apple”的列中的数字

搜索第2行中的单词:Apple。(这是查找数据所在列的方式。) 然后,在“苹果”所在的列中,检查数字0到X 0。。。。。。。。1.3.4.5.X(不规则)。数字之间有空行。 复制每个数字,直到数字更改。从0开始。 将结果粘贴到下面的rov中。 重复此步骤,直到在列中找到“停止”一词

结果应该在列中

答:BM是要搜索的列。 “苹果”这个词在第二排,但我不知道在哪里。因此,必须首先搜索包含数据的列。 下面是一个在“C”列中查找单词Apple的示例 然后垂直搜索数字。如果它在我们的例子C4中搜索0,那么它会在0下面再添加一个0,然后继续向下搜索,直到找到1为止。搜索1后,它在下面添加1并重复,直到在最后一行找到单词“Stop”

     Before             After
     A B C              A B C
1    - - Apple      1   - - Apple
2    - - -          2   - - -
3    - - -          3   - - -
4    - - 0          4   - - 0 
5    - - -          5   - - 0
6    - - 1          6   - - 1
7    - - -          7   - - 1
8    - - -          8   - - 1
9    - - 2          9   - - 2
10   - - -          10  - - 2
11   - - -          11  - - 2
12   - - -          12  - - 2
13   - - 3          13  - - 3
14   - - -          14  - - 3
15   - - 4          15  - - 4
16   - - -          16  - - 4
17   - - -          17  - - 4
18   - - -          18  - - 4
19   - - -          19  - - 4
20   - - 5          20  - - 5
21   - - 6          21  - - 6
22   - - -          22  - - 6
23   - - Stop       23  - - Stop
24   - - -          24  - - -
25   - - -          25  - - -

我不知道怎么开始。请帮忙。 此代码(不起作用)。我正在测试它应该是什么样子。这只是一个建筑。最后,我想在这个伟大的论坛上为其他人添加代码。如果我解释得不够充分,我会要求管理员不要向我关闭这个问题,因为我在宏中迷失了方向

Sub Macro5()

'1. Select Workbook 
Sheets("Sheet1").Select
'2 Select a column
Columns("C:C").Select
'3 Search Word "Apple"
Vlookup?
'Look up the number 0 in this column
Selection.Copy
' Paste the copied cell below                 
Loop
' (continue copying the number below.)         
If Columns("XY:YX").Value = "Stop" Then
End If
End Sub

欢迎提供任何建议

我已经这样做了,专栏只会在第2行找到Apple的第一个实例上运行。如果您需要更改此行,请更改我评论的位置

Sub test()
Dim Wb As Excel.Workbook
Dim Sh As Excel.Worksheet
Dim i As Integer
Dim Num As String
Dim iCol As Integer
Dim Col As String
Dim ColChecker As Boolean

Set Wb = ThisWorkbook
Set Sh = Wb.Sheets(1)

iCol = 1
Do While ColChecker = False
    If Sh.Cells(2, iCol).Value = "Apple" Then 'change the 2 here [row number]
        ColChecker = True
        Col = Split(Cells(1, iCol).Address, "$")(1)
    End If
    iCol = iCol + 1
Loop

i = 3 'change the 3 here [row number]
Num = ""
Do While Sh.Range(Col & i).Value <> "Stop" and i < 300 'and the row is less than 300
    If Sh.Range(Col & i).Value = "" Or Sh.Range(Col & i).Value = "-" Then
        Sh.Range(Col & i).Value = Num
    ElseIf Sh.Range(Col & i).Value <> "Stop" And Sh.Range(Col & i).Value <> "" And Sh.Range(Col & i).Value <> "-" And Sh.Range(Col & i).Value <> "Apple" Then
        Num = Sh.Range(Col & i).Value
    End If
    i = i + 1
Loop

End Sub

子测试()
将Wb设置为Excel.工作簿
将Sh设置为Excel.工作表
作为整数的Dim i
Dim Num作为字符串
作为整数的Dim-iCol
像线一样暗的颜色
Dim ColChecker作为布尔值
设置Wb=ThisWorkbook
设置Sh=Wb.Sheets(1)
iCol=1
Do While ColChecker=False
如果Sh.Cells(2,iCol).Value=“Apple”则“在此处更改2[行号]
ColChecker=True
Col=拆分(单元格(1,iCol)。地址,“$”(1)
如果结束
iCol=iCol+1
环
i=3'在此处更改3[行号]
Num=“”
当Sh.Range(Col&i)值为“Stop”且i<300'且该行小于300时,执行此操作
如果Sh.Range(Col&i).Value=“”或Sh.Range(Col&i).Value=“-”则
Sh.Range(Col&i).值=Num
ElseIf Sh.Range(Col&i).值“Stop”和Sh.Range(Col&i).值“”和Sh.Range(Col&i).Value“-”和Sh.Range(Col&i).值“Apple”
Num=Sh.Range(Col&i).值
如果结束
i=i+1
环
端接头

我已经这样做了,列将只在第2行中查找Apple的第一个实例上运行。如果您需要更改此行,请更改我评论的位置

Sub test()
Dim Wb As Excel.Workbook
Dim Sh As Excel.Worksheet
Dim i As Integer
Dim Num As String
Dim iCol As Integer
Dim Col As String
Dim ColChecker As Boolean

Set Wb = ThisWorkbook
Set Sh = Wb.Sheets(1)

iCol = 1
Do While ColChecker = False
    If Sh.Cells(2, iCol).Value = "Apple" Then 'change the 2 here [row number]
        ColChecker = True
        Col = Split(Cells(1, iCol).Address, "$")(1)
    End If
    iCol = iCol + 1
Loop

i = 3 'change the 3 here [row number]
Num = ""
Do While Sh.Range(Col & i).Value <> "Stop" and i < 300 'and the row is less than 300
    If Sh.Range(Col & i).Value = "" Or Sh.Range(Col & i).Value = "-" Then
        Sh.Range(Col & i).Value = Num
    ElseIf Sh.Range(Col & i).Value <> "Stop" And Sh.Range(Col & i).Value <> "" And Sh.Range(Col & i).Value <> "-" And Sh.Range(Col & i).Value <> "Apple" Then
        Num = Sh.Range(Col & i).Value
    End If
    i = i + 1
Loop

End Sub

子测试()
将Wb设置为Excel.工作簿
将Sh设置为Excel.工作表
作为整数的Dim i
Dim Num作为字符串
作为整数的Dim-iCol
像线一样暗的颜色
Dim ColChecker作为布尔值
设置Wb=ThisWorkbook
设置Sh=Wb.Sheets(1)
iCol=1
Do While ColChecker=False
如果Sh.Cells(2,iCol).Value=“Apple”则“在此处更改2[行号]
ColChecker=True
Col=拆分(单元格(1,iCol)。地址,“$”(1)
如果结束
iCol=iCol+1
环
i=3'在此处更改3[行号]
Num=“”
当Sh.Range(Col&i)值为“Stop”且i<300'且该行小于300时,执行此操作
如果Sh.Range(Col&i).Value=“”或Sh.Range(Col&i).Value=“-”则
Sh.Range(Col&i).值=Num
ElseIf Sh.Range(Col&i).值“Stop”和Sh.Range(Col&i).值“”和Sh.Range(Col&i).Value“-”和Sh.Range(Col&i).值“Apple”
Num=Sh.Range(Col&i).值
如果结束
i=i+1
环
端接头

谢谢。它完全符合我的需要。我还在考虑当超过第300行时如何添加“停止”。因为如果我忘记手动停车,它就会继续。真高兴它能工作。使用
do while
中的
i
来获得
和i<300
是,它同样有效。我通过创建一个辅助宏,在表格下方添加一个“stop”,自动在底部添加了一个“stop”。然后它停止得更早。当宏低于表中最后一行的级别时,我找不到停止宏的方法:)谢谢。它完全符合我的需要。我还在考虑当超过第300行时如何添加“停止”。因为如果我忘记手动停车,它就会继续。真高兴它能工作。使用
do while
中的
i
来获得
和i<300
是,它同样有效。我通过创建一个辅助宏,在表格下方添加一个“stop”,自动在底部添加了一个“stop”。然后它停止得更早。当宏低于表中最后一行的级别时,我找不到停止宏的方法:)