首页 > 解决方案 > 寻找如何通过循环将用户输入放入我的代码中

问题描述

Sub Delete0()
Dim myRow As Variant
myRow = InputBox("What row does the data start in?")

Dim myColumn As Variant
myColumn = InputBox("What Column does the data start in? (number format, i.e A=1, B=2)")

Dim LR As Long
Dim FR As Long

LR = 1 'where to have myRow be entered
FR = Cells(Rows.Count, 1).End(xlUp).Row

Do Until LR > FR
    If Cells(LR, 17).Value > 0 Then 'Where to have myColumn be entered
        LR = LR + 1
    Else
        Cells(LR, 17).EntireRow.Delete
        FR = FR - 1
    End If
Loop
End Sub

这是我当前的代码,但我想在数据上添加用户输入可能会从不同的行开始每周更改,并且在不同的列中删除值。我如何将其输入到我当前的代码中?

标签: excelvba

解决方案


鉴于您提供的代码,下面的代码段应该是最简单的方法。我已将 myRow 和 myColumn 的数据类型更改为 Long 以在输入时引发错误。

Sub Delete0()
Dim myRow As Long
myRow = InputBox("What row does the data start in?")

Dim myColumn As Long
myColumn = InputBox("What Column does the data start in? (number format, i.e A=1, B=2)")

Dim LR As Long
Dim FR As Long

LR = myRow 'where to have myRow be entered
FR = Cells(Rows.Count, 1).End(xlUp).Row

Do Until LR > FR
    If Cells(LR, myColumn).Value > 0 Then 'Where to have myColumn be entered
        LR = LR + 1
    Else
        Cells(LR, myColumn).EntireRow.Delete
        FR = FR - 1
    End If
Loop
End Sub

推荐阅读