Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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/0/vba/17.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/7/symfony/6.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 VBA-轻松循环_Excel_Vba - Fatal编程技术网

Excel VBA-轻松循环

Excel VBA-轻松循环,excel,vba,Excel,Vba,我想写一个脚本,看起来像一个循环。我的数据在A栏下 我想把脚本放在B列。例如B2=A2 在B3下输入“xxxx”,在B4下输入“yyyy”,然后重复步骤1和2,直到数据结束 到目前为止,我有以下内容。我怎么能循环它,因为我必须再打上几百次。。。。 谢谢 尝试将代码转换为“循环逻辑”时,您会得到如下结果: 'beginning of your code here Dim i As Long for i=2 to DL Range("B" & i*3-4).Value = Rang

我想写一个脚本,看起来像一个循环。我的数据在A栏下

  • 我想把脚本放在B列。例如B2=A2
  • 在B3下输入“xxxx”,在B4下输入“yyyy”,然后重复步骤1和2,直到数据结束
  • 到目前为止,我有以下内容。我怎么能循环它,因为我必须再打上几百次。。。。 谢谢


    尝试将代码转换为“循环逻辑”时,您会得到如下结果:

    'beginning of your code here
    Dim i As Long
    for i=2 to DL
    
        Range("B" & i*3-4).Value = Range("A" & i)
        Range("B" & i*3-3).Value = Script1
        Range("B" & i*3-2).Value = Script2
    
    next i
    'the end of your sub here
    
    Sub-tgr()
    Dim arrData()作为变量
    Dim varAcell作为变体
    将strScript1设置为字符串
    将strScript2设置为字符串
    将数据索引变长
    我想我会坚持多久
    strScript1=“键结束”
    strScript2=“密钥等待”
    带范围(“A2”,单元格(行数,“A”)。结束(xlUp))
    如果.Row<2,则退出Sub“无数据”
    重拨数据(1到.Rows.Count*3)
    对于.Value中的每个varAcell
    对于i=1到3
    DataIndex=DataIndex+1
    arrData(DataIndex)=选择(i、varAcell、strScript1、strScript2)
    接下来我
    下一个瓦拉塞尔
    以
    范围(“B2”).Resize(UBound(arrData)).Value=Application.Transpose(arrData)
    删除ARR数据
    端接头
    
    [编辑]

    对于@KazJaw:


    您已经知道需要一个循环。执行循环。
    擦除
    ??。。。不是VBA@KazJaw我已编辑了我的答案,以包含Excel VBA帮助中的VBA Erase语句。我不知道你说的“不是VBA”是什么意思。谢谢你的帮助。@tigeravatar,谢谢你提供的信息。有趣的是,我从未使用过它,也不知道存在
    erase语句。我不认为我在不知不觉中失去了什么,是吗?谢谢你的帮助。
    
    'beginning of your code here
    Dim i As Long
    for i=2 to DL
    
        Range("B" & i*3-4).Value = Range("A" & i)
        Range("B" & i*3-3).Value = Script1
        Range("B" & i*3-2).Value = Script2
    
    next i
    'the end of your sub here
    
    Sub tgr()
    
        Dim arrData() As Variant
        Dim varAcell As Variant
        Dim strScript1 As String
        Dim strScript2 As String
        Dim DataIndex As Long
        Dim i As Long
    
        strScript1 = "KEY END"
        strScript2 = "KEY WAIT"
    
        With Range("A2", Cells(Rows.Count, "A").End(xlUp))
            If .Row < 2 Then Exit Sub   'No data
            ReDim arrData(1 To .Rows.Count * 3)
            For Each varAcell In .Value
                For i = 1 To 3
                    DataIndex = DataIndex + 1
                    arrData(DataIndex) = Choose(i, varAcell, strScript1, strScript2)
                Next i
            Next varAcell
        End With
    
        Range("B2").Resize(UBound(arrData)).Value = Application.Transpose(arrData)
    
        Erase arrData
    
    End Sub