(VBA EXCEL)IF功能

时间:2016-02-02 18:52:44

标签: vba excel-vba excel

我正在尝试让我的代码根据我textbox2上输入的数字删除一些行。首先它将删除一些标题单元格,然后它将删除更多的单元格。

问题出现在标题单元格部分,我的代码忽略了整个if语句。我不知道为什么。这是:

Private Sub CommandButton3_Click()

'\/ DSE

If Range("A5").Value <> "1" Then
GoTo PortalDSI
End If
MsgBox ("DSE")
T = 337
Y = TextBox2.Value



If Y < 1 Then
MsgBox ("Ué?")
End
ElseIf Y < 40 And Y > 0 Then
    Rows("302:305").Delete Shift:=xlUp
    Rows("259:262").Delete Shift:=xlUp
    Rows("216:219").Delete Shift:=xlUp
    Rows("173:176").Delete Shift:=xlUp
    Rows("130:133").Delete Shift:=xlUp
    Rows("87:90").Delete Shift:=xlUp
    Rows("44:47").Delete Shift:=xlUp
    s = 28
    D = 5
ElseIf Y < 79 And Y > 39 Then
    Rows("302:305").Delete Shift:=xlUp
    Rows("259:262").Delete Shift:=xlUp
    Rows("216:219").Delete Shift:=xlUp
    Rows("173:176").Delete Shift:=xlUp
    Rows("130:133").Delete Shift:=xlUp
    Rows("87:90").Delete Shift:=xlUp
     s = 24
     D = 9
ElseIf Y < 118 And Y > 78 Then
    Rows("302:305").Delete Shift:=xlUp
    Rows("259:262").Delete Shift:=xlUp
    Rows("216:219").Delete Shift:=xlUp
    Rows("173:176").Delete Shift:=xlUp
    Rows("130:133").Delete Shift:=xlUp
     s = 20
     D = 13
ElseIf Y < 157 And Y > 117 Then
    Rows("302:305").Delete Shift:=xlUp
    Rows("259:262").Delete Shift:=xlUp
    Rows("216:219").Delete Shift:=xlUp
    Rows("173:176").Delete Shift:=xlUp
     s = 16
     D = 17
ElseIf Y < 196 And Y > 156 Then
    Rows("302:305").Delete Shift:=xlUp
    Rows("259:262").Delete Shift:=xlUp
    Rows("216:219").Delete Shift:=xlUp
     s = 12
     D = 21
ElseIf Y < 235 And Y > 195 Then
    Rows("302:305").Delete Shift:=xlUp
    Rows("259:262").Delete Shift:=xlUp
     s = 8
     D = 25
ElseIf Y < 274 And Y > 234 Then
    Rows("302:305").Delete Shift:=xlUp
     s = 4
     D = 29
ElseIf Y > 274 And Y < 305 Then
s = 4
D = 29
ElseIf Y > 305 Then
MsgBox ("Na mão")
End
End If

'B is fixed (:

Z = T - Y
A = Y + D
B = T - s

Rows(A & ":" & B).Delete Shift:=x1down

End

' DSE /\

PortalDSI:
MsgBox ("DSI")

Y = TextBox2.Value

If Y < 1 Then
MsgBox ("Como Assim?")
End
End If

If Y = 1 Then

Rows("9:9").Delete Shift:=x1down
End If
If Y > 2 Then

Dim I
For I = 1 To Y - 2
Rows("9:9").Insert Shift:=x1down
Next I
Else
End
End If

T = Y - 1
Range("A8").Value = 1
Range("A9").Select
Dim J
For J = 1 To T
activecell.Offset(-1, 0).Select
v = Selection.Value
activecell.Offset(1, 0).Select
Selection.Value = v + 1
activecell.Offset(1, 0).Select
Next J

Range("B9").Select
Dim O
For O = 1 To T
activecell.Value = "1 CX"
activecell.Offset(1, 0).Select
Next O

Range("D8").Select
Unload Me

End Sub

我真的迷失在这里,我尝试了所有我能找到的东西。求你帮帮我。

2 个答案:

答案 0 :(得分:0)

我只是稍微清理了一下,但你必须尝试一下,我不太确定它是否能按你的意愿运作:

If ActiveWorkbook.ActiveWorksheet.Range("A5").Value <> "1" Then
   GoTo PortalDSI
End If

MsgBox ("DSE")
T = 305
Y = TextBox2.Value

With ActiveWorkbook.ActiveSheet
    If Y < 1 Then
        MsgBox ("Ué?")
    ElseIf Y < 40 And Y > 0 Then
        .Rows("302:305").Delete Shift:=xlUp
        .Rows("259:262").Delete Shift:=xlUp
        .Rows("216:219").Delete Shift:=xlUp
        .Rows("173:176").Delete Shift:=xlUp
        .Rows("130:133").Delete Shift:=xlUp
        .Rows("87:90").Delete Shift:=xlUp
        .Rows("44:47").Delete Shift:=xlUp
    ElseIf Y < 79 And Y > 39 Then
        .Rows("302:305").Delete Shift:=xlUp
        .Rows("259:262").Delete Shift:=xlUp
        .Rows("216:219").Delete Shift:=xlUp
        .Rows("173:176").Delete Shift:=xlUp
        .Rows("130:133").Delete Shift:=xlUp
        .Rows("87:90").Delete Shift:=xlUp
    ElseIf Y < 118 And Y > 78 Then
        .Rows("302:305").Delete Shift:=xlUp
        .Rows("259:262").Delete Shift:=xlUp
        .Rows("216:219").Delete Shift:=xlUp
        .Rows("173:176").Delete Shift:=xlUp
        .Rows("130:133").Delete Shift:=xlUp
    ElseIf Y < 157 And Y > 117 Then
        .Rows("302:305").Delete Shift:=xlUp
        .Rows("259:262").Delete Shift:=xlUp
        .Rows("216:219").Delete Shift:=xlUp
        .Rows("173:176").Delete Shift:=xlUp
    ElseIf Y < 196 And Y > 156 Then
        .Rows("302:305").Delete Shift:=xlUp
        .Rows("259:262").Delete Shift:=xlUp
        .Rows("216:219").Delete Shift:=xlUp
    ElseIf Y < 235 And Y > 195 Then
        .Rows("302:305").Delete Shift:=xlUp
        .Rows("259:262").Delete Shift:=xlUp
    ElseIf Y < 274 And Y > 234 Then
        .Rows("302:305").Delete Shift:=xlUp
    End If
End With

Z = T - Y
A = Y + 4
B = T + 4

ActiveWorkbook.ActiveSheet.Rows(A & ":" & B).Delete Shift:=x1down

End

将来尽量避免选择和激活。即使未选择范围,代码仍可以运行。

*编辑*

更具体地说明您正在处理的工作簿和工作表。我将使用&#34; ActivWorkbook.Sheets(&#34; NameOfYourSheet&#34;)&#34;而不是ActiveSheet。也可以设置为像

这样的变量
Dim ShtXY As Worksheet
ShtXY = ActiveWorkbook.Sheets("NameOfYourSheet")

答案 1 :(得分:0)

请试试这个

Private Sub CommandButton3_Click()

'\/ DSE

If Range("A5").Value <> "1" Then
GoTo PortalDSI
End If
MsgBox ("DSE")
T = 305
Y = TextBox2.Value



If Y < 1 Then
MsgBox ("Ué?")


`enter code here`' else if's are doing nothing, but they are being ran.
ElseIf Y < 40 And Y > 0 Then
        Rows("302:305").Delete Shift:=xlUp
        Rows("259:262").Delete Shift:=xlUp
        Rows("216:219").Delete Shift:=xlUp
        Rows("173:176").Delete Shift:=xlUp
        Rows("130:133").Delete Shift:=xlUp
        Rows("87:90").Delete Shift:=xlUp
        Rows("44:47").Delete Shift:=xlUp
    ElseIf Y < 79 And Y > 39 Then
        Rows("302:305").Delete Shift:=xlUp
        Rows("259:262").Delete Shift:=xlUp
        Rows("216:219").Delete Shift:=xlUp
        Rows("173:176").Delete Shift:=xlUp
        Rows("130:133").Delete Shift:=xlUp
        Rows("87:90").Delete Shift:=xlUp
    ElseIf Y < 118 And Y > 78 Then
        Rows("302:305").Delete Shift:=xlUp
        Rows("259:262").Delete Shift:=xlUp
        Rows("216:219").Delete Shift:=xlUp
        Rows("173:176").Delete Shift:=xlUp
        Rows("130:133").Delete Shift:=xlUp
    ElseIf Y < 157 And Y > 117 Then
        Rows("302:305").Delete Shift:=xlUp
        Rows("259:262").Delete Shift:=xlUp
        Rows("216:219").Delete Shift:=xlUp
        Rows("173:176").Delete Shift:=xlUp
    ElseIf Y < 196 And Y > 156 Then
        Rows("302:305").Delete Shift:=xlUp
        Rows("259:262").Delete Shift:=xlUp
        Rows("216:219").Delete Shift:=xlUp
    ElseIf Y < 235 And Y > 195 Then
        Rows("302:305").Delete Shift:=xlUp
        Rows("259:262").Delete Shift:=xlUp
    ElseIf Y < 274 And Y > 234 Then
        Rows("302:305").Delete Shift:=xlUp
    End If



Z = T - Y
A = Y + 4
B = T + 4

Rows(A & ":" & B).Delete Shift:=x1down



' DSE /\


'From here on everything is fine \/


PortalDSI:
MsgBox ("DSI")

Y = TextBox2.Value

If Y < 1 Then
MsgBox ("Como Assim?")

End If

If Y = 1 Then

Rows("9:9").Delete Shift:=x1down
End If
If Y > 2 Then

Dim I
For I = 1 To Y - 2
Rows("9:9").Insert Shift:=x1down
Next I
Else

End If

T = Y - 1
Range("A8").Value = 1
Range("A9").Select
Dim J
For J = 1 To T
activecell.Offset(-1, 0).Select
v = Selection.Value
activecell.Offset(1, 0).Select
Selection.Value = v + 1
activecell.Offset(1, 0).Select
Next J

Range("B9").Select
Dim O
For O = 1 To T
activecell.Value = "1 CX"
activecell.Offset(1, 0).Select
Next O

Range("D8").Select
Unload Me

End Sub

正如辛迪所说,由于 END IF 之前的 END 命令,它没有进入 IF 条件。使用END命令,您想要退出当前 SUB