在vba中使用AND condtion

时间:2017-03-10 05:40:48

标签: excel-vba vba excel

当我使用“AND”条件时,出现错误“运行时错误1004,对象全局失败的方法范围”。我的代码工作正常,如果我删除和条件可能是错误的使用“和”请任何帮助将是伟大的支持。感谢

<form class="form-inline" action="form.php">
            <div class="form-group">
            <label for="exampleInputName2">Name</label>
            <input type="text" name="username" class="form-control" id="exampleInputName2" placeholder="Jane Doe">
            </div><br/><br/>
            <div class="form-group">
            <label for="exampleInputEmail2">Email</label>
            <input type="email" name="email" class="form-control" id="exampleInputEmail2" placeholder="jane.doe@example.com">
            </div><br/><br/>
            <button type="submit" class="btn btn-default">Subscribe</button>
            </form>




<?php echo $_GET["username"]; ?><br/><br/>  : This is working.
<?php echo $_POST["username"]; ?><br/><br/> : this is not working.

3 个答案:

答案 0 :(得分:2)

由于VBA没有short circuit你应该将你的AND分成两个IF,所以如果第一次测试不是{0}},你就不会进行第二次测试。过去了。

代码也会移除Select并关闭ScreenUpdating,如果使用AutoFilter而不是范围循环,代码会更快。

Sub Recut()
Dim i As Double, dt1 As String
Dim dtt1 As Date
Dim dt2 As String
Dim dtt2 As Date

dt1 = ComboBox1.Value
dtt1 = CDate(dt1)
dt2 = ComboBox2.Value
dtt2 = CDate(dt2)

Application.ScreenUpdating = False

For i = 2 To 6724
    If Range("A" & i).Value >= dtt1 Then
        If Range("A" & i).Value <= dtt2 Then
            With Rows(i).Interior
            .ColorIndex = 36
            .Pattern = xlSolid
            End With
         End If
    End If
Next

Application.ScreenUpdating = True
End Sub

答案 1 :(得分:1)

你在这一行中有一个小错字:

 If Range("A" & i).Value >= dtt1 And Range("A", i).Value <= dtt2 Then

错误是因为Range("A", i).Value不是有效范围,因为"A"不是范围或单元格。

更改为:

If Range("A" & i).Value >= dtt1 And Range("A" & i).Value <= dtt2 Then

答案 2 :(得分:0)

您在if条件下使用逗号(,)请替换&#39;,&#39;用&#39;&amp;&#39; 已更正代码请参考以下代码:

Dim i As Double, dt1 As String
Dim dtt1 As Date
Dim dt2 As String
Dim dtt2 As Date

dt1 = ComboBox1.Value
dtt1 = CDate(dt1)
dt2 = ComboBox2.Value
dtt2 = CDate(dt2)
Debug.Print dtt2

For i = 2 To 6724
    If Range("A" & i).Value >= dtt1 And Range("A" & i).Value <= dtt2 Then
        Rows(i).Select
        With Selection.Interior
        .ColorIndex = 36
        .Pattern = xlSolid
        End With
    End If
Next