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
Excel 在Inputbox中保留多个条目_Excel_Vba_Loops_User Input - Fatal编程技术网

Excel 在Inputbox中保留多个条目

Excel 在Inputbox中保留多个条目,excel,vba,loops,user-input,Excel,Vba,Loops,User Input,下面是我的代码。 我在一张工作表上选择了一些数据,代码将转到这些数据。基于第一个userinput,数据将在列中过滤该数据,如果用户希望添加另一个要过滤掉的数据段,我会生成一个提示来选择第二个数据段 我想做但不知道如何做的是循环询问用户是否希望继续添加更多筛选数据,同时保留以前选择/输入的任何数据。如有任何想法/想法,将不胜感激 Sub Macro4() Dim strUserInput As String Dim strUserInput1 As Integer Dim strUserInp

下面是我的代码。 我在一张工作表上选择了一些数据,代码将转到这些数据。基于第一个userinput,数据将在列中过滤该数据,如果用户希望添加另一个要过滤掉的数据段,我会生成一个提示来选择第二个数据段

我想做但不知道如何做的是循环询问用户是否希望继续添加更多筛选数据,同时保留以前选择/输入的任何数据。如有任何想法/想法,将不胜感激

Sub Macro4()

Dim strUserInput As String
Dim strUserInput1 As Integer
Dim strUserInputX As String
Dim strUserInput2 As String

strUserInput = MsgBox("Would fund would you like to search for?: ")

    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.AutoFilter
    Range("C5").Select
    ActiveSheet.Range("$A$1:$H$699").AutoFilter Field:=3, Criteria1:=strUserInput

strUserInput1 = MsgBox("Would you like to add another fund to your selection?: ", vbYesNo) 'i want to loop through multiple selections to add more criteria in the filter if the answer is yes

If strUserInput1 = vbYes Then

strUserInput2 = InputBox("What other fund would you like to add to your selection?: ")

   ActiveSheet.Range("$A$1:$H$699").AutoFilter Field:=3, Criteria1:=strUserInput, _
        Operator:=xlOr, Criteria2:=strUserInput2
End If

MsgBox ("Your selections are complete")


End Sub

如果我理解正确,您希望每次都使用以前的输入填充新的输入框(如果用户单击了“是”)

注意:我没有测试以下内容,但这会让您了解如何构建逻辑

要做到这一点,您可以利用
[Inputbox()][1]
方法的
Default
属性

像这样:

strUserInput2 = InputBox("What other fund would you like to add to your selection?: ", ,strUserInput) 

至于循环一个
[for…Next][2]
循环应该可以做到这一点,其中结束部分将是您的标准计数(可能)。
比如:

Dim LoopCounter as Long
'Your 1st InputBox goes here
For LoopCounter = 1 to ActiveSheet.Range("$A$1:$H$699").Count
    'Your MsgBox code goes here
    If strUserInput1 = vbNo Then
        Exit For
    Else
        'Your 2nd InputBox goes here
    End If
Next LoopCounter

尽管使用
Range.Count
属性(如本例中计算结果为
5592
)可能看起来有些过分,但这确保了用户有机会搜索搜索范围内的每个单元格。如果用户单击
MsgBox
上的“否”按钮,则
的退出可确保循环停止

您已经知道需要添加一个循环。这一点,再加上过滤器可以接收阵列的事实,提供了一个潜在的解决方案

Public Sub Macro4()
   Dim strUserInput As String
   Dim res As VbMsgBoxResult
   Dim crit() As String

   Range("A1").Select
   Range(Selection, Selection.End(xlToRight)).Select
   Selection.AutoFilter
   Range("C5").Select

   strUserInput = InputBox("What fund would you like to search for?: ")
   ReDim crit(0)
   crit(UBound(crit)) = strUserInput

   res = vbYes

   Do While res = vbYes
      res = MsgBox("Would you like to add another fund to your selection?: ", vbYesNo)

      If res = vbYes Then
         strUserInput = InputBox("What other fund would you like to add to your selection?: ")
         ReDim Preserve crit(UBound(crit) + 1)
         crit(UBound(crit)) = strUserInput
      End If
   Loop

   ActiveSheet.Range("$A$1:$H$699").AutoFilter Field:=3, Criteria1:=crit
   MsgBox "Your selections are complete"
End Sub

循环接受用户输入并将其添加到数组中。一旦退出循环,数组将作为标准应用,其中数组的每个元素都是或。

嗨,塞缪尔,谢谢你回到这里。我想做的是有一个输入框,使用此输入对列执行筛选,询问用户是否希望向筛选中添加更多数据,如果愿意,使用另一个输入框将输入的数据输入筛选中,等等,因此,我可能需要有7个输入框,筛选中有7位数据,或者根据用户的要求添加更多数据。希望这能让你sense@markmore我不想继续,也许可以在你的问题中添加一些示例截图,包括示例数据集,用户可能输入的过滤器,然后,如果他们在一个消息框上单击yes来添加另一个消息框,它应该如何显示?我不确定,但它可能会通过移动调用inputbox的位置来工作,但我需要明确说明您首先想要实现的目标。
Public Sub Macro4()
   Dim strUserInput As String
   Dim res As VbMsgBoxResult
   Dim crit() As String

   Range("A1").Select
   Range(Selection, Selection.End(xlToRight)).Select
   Selection.AutoFilter
   Range("C5").Select

   strUserInput = InputBox("What fund would you like to search for?: ")
   ReDim crit(0)
   crit(UBound(crit)) = strUserInput

   res = vbYes

   Do While res = vbYes
      res = MsgBox("Would you like to add another fund to your selection?: ", vbYesNo)

      If res = vbYes Then
         strUserInput = InputBox("What other fund would you like to add to your selection?: ")
         ReDim Preserve crit(UBound(crit) + 1)
         crit(UBound(crit)) = strUserInput
      End If
   Loop

   ActiveSheet.Range("$A$1:$H$699").AutoFilter Field:=3, Criteria1:=crit
   MsgBox "Your selections are complete"
End Sub