Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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:Msgbox循环,对两个数字(包括两个)之间的值求和_Vba_Excel - Fatal编程技术网

VBA:Msgbox循环,对两个数字(包括两个)之间的值求和

VBA:Msgbox循环,对两个数字(包括两个)之间的值求和,vba,excel,Vba,Excel,我对VBA很陌生,完全被难住了。我有一系列的价值(销售)在A2:A40范围内。我需要它,以便要求用户输入2个阈值,假设threshold1“&threshold2&”,然后 总和=总和+单元格值 如果结束 下一个细胞 总数=总和 MsgBox“&threshold1&”和“&threshold2&”之间的总销售额为&total&” 这将适用于您正在尝试的操作。然而,在你的问题中,你说你的值在A列,但是你的销售额设置在B列。我的代码是,如果你的值在B2:B40中。如果值在A中,则只需更改该值 Su

我对VBA很陌生,完全被难住了。我有一系列的价值(销售)在
A2:A40
范围内。我需要它,以便要求用户输入2个阈值,假设threshold1“&threshold2&”,然后 总和=总和+单元格值 如果结束 下一个细胞 总数=总和 MsgBox“&threshold1&”和“&threshold2&”之间的总销售额为&total&”
这将适用于您正在尝试的操作。然而,在你的问题中,你说你的值在A列,但是你的销售额设置在B列。我的代码是,如果你的值在B2:B40中。如果值在A中,则只需更改该值

Sub test()

Sheet1.Activate
Dim cell As Range, sales As Range
Dim threshold1 As Integer, threshold2 As Integer, placeHolder As Integer
Dim sum As Integer, Total As Integer

Set sales = Sheet1.Range("B2:B40")

threshold1 = InputBox("Enter threshold 1", "Threshold 1")
threshold2 = InputBox("Enter threshold 2", "Threshold 2")

If threshold1 < threshold2 Then
    placeHolder = threshold1
    threshold1 = threshold2
    threshold2 = placeHolder
End If

For i = 2 To 40
    If Sheets("Sheet1").Range("B" & i).Value < threshold1 And Sheets("Sheet1").Range("B" & i).Value > threshold2 Then
        sum = sum + Sheets("Sheet1").Range("B" & i).Value
    End If
Next i

Total = sum

MsgBox "The total sales between " & threshold1 & " and " & threshold2 & " is " & Total & ""
End Sub
子测试()
表1.激活
暗淡单元格作为范围,销售作为范围
将阈值1设置为整数,将阈值2设置为整数,将占位符设置为整数
点心为整数,总计为整数
设置销售额=表1.范围(“B2:B40”)
阈值1=输入框(“输入阈值1”、“阈值1”)
阈值2=输入框(“输入阈值2”,“阈值2”)
如果阈值1<阈值2,则
占位符=阈值1
阈值1=阈值2
阈值2=占位符
如果结束
对于i=2到40
如果板材(“板材1”).范围(“B”和i).值<阈值1,板材(“板材1”).范围(“B”和i).值>阈值2,则
总和=总和+表格(“表格1”)。范围(“B”和“i”)。数值
如果结束
接下来我
总数=总和
MsgBox“&threshold1&”和“&threshold2&”之间的总销售额为&total&”
端接头

这听起来比
InputBox
MsgBox
更适合自定义
UserForm
。3个单独弹出窗口的用户体验非常糟糕。或者只需使用两个单元格作为输入,使用一个SUMIFS()作为总数。您的变量threshold1和threshold2是整数,不能用字符串引号(“)。去掉引号并删除-,然后它会起作用,您应该在代码中进行某种检查,以确保threshold1大于threshold2。类似于“If threshold1threshold1=threshold1和cell.Value您应该为threshold变量添加某种验证,因为如果用户在threshold1上输入的值大于threshold2,我认为总和不起作用
Sub test()

Sheet1.Activate
Dim cell As Range, sales As Range
Dim threshold1 As Integer, threshold2 As Integer, placeHolder As Integer
Dim sum As Integer, Total As Integer

Set sales = Sheet1.Range("B2:B40")

threshold1 = InputBox("Enter threshold 1", "Threshold 1")
threshold2 = InputBox("Enter threshold 2", "Threshold 2")

If threshold1 < threshold2 Then
    placeHolder = threshold1
    threshold1 = threshold2
    threshold2 = placeHolder
End If

For i = 2 To 40
    If Sheets("Sheet1").Range("B" & i).Value < threshold1 And Sheets("Sheet1").Range("B" & i).Value > threshold2 Then
        sum = sum + Sheets("Sheet1").Range("B" & i).Value
    End If
Next i

Total = sum

MsgBox "The total sales between " & threshold1 & " and " & threshold2 & " is " & Total & ""
End Sub