简体   繁体   English

Excel VBA:如何解决If Else For Loop错误

[英]Excel VBA: How to fix an If Else For Loop error

This is my first vba project so please be patient. 这是我的第一个vba项目,请耐心等待。 I have created an Excel Userform to add info to a database. 我创建了一个Excel用户窗体,将信息添加到数据库。 When I click "add" I have vba script that should find the first open worksheet for that year (ex. Name_2018_1, Name_2018_2, Name_2018_3) or create a new consecutively named worksheet. 当我单击“添加”时,我具有vba脚本,该脚本应查找该年的第一个打开的工作表(例如Name_2018_1,Name_2018_2,Name_2018_3)或创建一个新的连续命名的工作表。

I keep receiving Compile Errors based on where I place my "then" and "exit for" statements. 我会根据放置“ then”和“ exit for”语句的位置不断收到编译错误。

I want the counter to either find the first worksheet with an available row or loop all the way through i and then make a new sheet based on the i value. 我希望计数器要么找到具有可用行的第一个工作表,要么一直循环到i,然后根据i值制作一个新工作表。

How do I correctly set up a for loop after my else? 在其他情况之后,如何正确设置for循环? If anyone could point out what I am doing wrong it would be appreciated. 如果有人指出我做错了,将不胜感激。

'Dim myFirstBlankRow As Long
'Dim i               As Long
'Dim WsName          As String
'Dim WsNamei         As String
'Dim ws              As Worksheet
'Dim counter         As Integer

'Declare worksheet names
WsName = "Name_" & Year(Me.DTPicker1.value) & "*" 
WsNamei = "Name_" & Year(Me.DTPicker1.value) & "_" & i

For Each ws In Sheets
    If ws.Name = WsName Or ws.Name Like WsName & "*" Then i = i + 1
Next

'If no worksheet exists, then make a new one, else loop through until an empty row is found

If i = 0 Then 'No worksheet with the year selected by DTPicker
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add new worksheet
        ActiveSheet.Name = WsName & "_1"
    End With

*If I place the "Then Exit For" on one line I receive a Compile Error "Else without If" *如果将“ Then Exit For”放在一行上,则会收到编译错误“ Else if if”

Else:
    For counter = 1 To i Step 1
        Worksheets(WsName & "_" & counter).Activate
        If IsEmpty(Range("A1048576").value) = True Then Exit For
    Next counter


Else 'No recognizing the original If statement
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add worksheet
        ActiveSheet.Name = WsName & "_" & i
    End With
End If

*However, when I place the "Then" & "Exit For" on separate lines I receive a Compile Error"Next without For" *但是,当我将“ Then”和“ Exit For”放在单独的行上时,会收到一个编译错误“ Next for For”

Else:
    For counter = 1 To i Step 1
        Worksheets(WsName & "_" & counter).Activate
        If IsEmpty(Range("A1048576").value) = True Then
        Exit For
    Next counter

Else
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add worksheet
        ActiveSheet.Name = WsName & "_" & i
    End With
End If

'Added for my modifications I want the code to (1st) check for an original worksheet and make one if there is not. 补充说,我希望代码(第1个)检查原始工作表,如果没有,请制作一个。 (2nd) If that original worksheet exists but is full, I want to make sure there is not already a new worksheet. (第二个)如果原始工作表存在但已满,我要确保没有新的工作表。 If there is it becomes the active sheet. 如果有,它将成为活动工作表。 (3rd) if there is not another available worksheet I need the script to make one. (3rd)如果没有其他可用的工作表,我需要脚本来制作一个。

I was originally having issues with a new worksheet being created and only one entry being applied to that worksheet prior to another worksheet being created, hence the 3 steps. 我最初在创建新工作表时遇到问题,在创建另一个工作表之前仅将一个条目应用于该工作表,因此需要3个步骤。

Should I split off the first If and then have the last 2 as their own entity for if the first worksheet is full? 如果第一个工作表已满,我是否应该拆分第一个If,然后将最后2个作为自己的实体?

If i = 0 Then 'No worksheet with the year selected by DTPicker
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add worksheet
        i = 1
        ActiveSheet.Name = WsName & "_" & i
    End With
ElseIf
    For counter = 1 To i Step 1
        Worksheets(WsName & "_" & counter).Activate
        If IsEmpty(Range("A1048576").value) = True Then
        Exit For
        End If
    Next counter
Else
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add worksheet
        ActiveSheet.Name = WsName & "_" & i
    End With
End If

If Then isn't followed by a statement, say, like this: 如果“ Then后面没有声明,则说像这样:

If IsEmpty(Range("A1048576").value) Then Exit For

Then it's the block syntax and since it's a block, it needs to be closed, in this case with End If : 然后是块语法 ,由于它是一个块,因此需要将其关闭,在这种情况下,请使用End If

If IsEmpty(Range("A1048576").value) Then
    Exit For
End If

That's why you get "next without for", because then without the End If , the compiler encounters Next before it gets to any End If , so the For counter loop block is considered not terminated. 这就是为什么会得到“ next without for”的原因,因为End If没有End If ,编译器在到达任何End If之前会遇到Next ,因此For counter循环块被视为未终止。

Next problem, you can't have an If...Else...End If block with more than 1 Else . 下一个问题,您将无法获得If...Else...End If块,且其中的Else超过1个。

Else:
    For counter = 1 To i Step 1
        Worksheets(WsName & "_" & counter).Activate
        If IsEmpty(Range("A1048576").value) = True Then Exit For
    Next counter


Else 'No recognizing the original If statement

VBA can't parse this; VBA无法解析此内容; I can't parse this either. 我也无法解析。 I've no idea what you intended to do here. 我不知道你打算在这里做什么。 If i is 0 you execute something, otherwise you execute something else - all bases are covered, an additional "else" just doesn't fit anywhere. 如果i0 ,则执行某项操作,否则执行其他操作-覆盖所有基数,一个额外的“ else”仅适用于任何地方。

This would a template to work with: 这将与以下模板一起使用:

If {bool-expression} Then
    {statements}
ElseIf {bool-expression} Then
    {statements}
Else
    {statements}
End If

The Else token identifies the "fallback" case. Else令牌标识“备用”情况。 Having two makes no sense. 有两个没有任何意义。 Note that the instruction separator token : is redundant in Else: , and makes it look like a line label: avoid that. 请注意,指令分隔符token :Else: :是多余的,并使它看起来像行标签:避免这种情况。


How to avoid this 如何避免这种情况

When you type an If statement, type the corresponding End If immediately : 当您键入If语句时, 立即键入相应的End If

If something Then
    'type here
End If

When you later nest things, keep immediately closing whatever blocks you open: 以后嵌套事物时,请立即关闭打开的任何块:

If something Then
    For i = 0 To 10
        'type here now
    Next
Else
    'get back here later
End If

If things start being too hard to follow, grab the entire block and pull it into its own private procedure, passing whatever parameters are needed. 如果事情开始变得太难了,请抓住整个块并将其放入自己的私有过程中,并传递所需的任何参数。

Next without For translates to missing End If 没有For的下一个表示缺少End If

In the first case the Else: should be replaced with Else and the second Else should be removed. 在第一种情况下,应将Else:替换为Else并删除第二个Else

In the second case you additionally are missing an End If , regardless of the Error. 在第二种情况下,无论错误如何,您都将丢失End If The End If should be below the Exit For (above Next Counter ). End If应该在Exit ForNext Counter上方)下方。

They are equivalent, you are just using two different ways of the If statement. 它们是等效的,您只是使用If语句的两种不同方式。

Case 1 情况1

If i = 0 Then 'No worksheet with the year selected by DTPicker
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add new worksheet
        ActiveSheet.Name = WsName & "_1"
    End With
  Else
    For counter = 1 To i Step 1
        Worksheets(WsName & "_" & counter).Activate
        If IsEmpty(Range("A1048576").value) Then Exit For
    Next counter
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add worksheet
        ActiveSheet.Name = WsName & "_" & i
    End With
End If

Case 2 情况二

If i = 0 Then 'No worksheet with the year selected by DTPicker
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add new worksheet
        ActiveSheet.Name = WsName & "_1"
    End With
  Else
    For counter = 1 To i Step 1
        Worksheets(WsName & "_" & counter).Activate
        If IsEmpty(Range("A1048576").value) Then
            Exit For
        End If
    Next counter
    With Sheets("Template")
        .Copy After:=Sheets(Sheets.Count) 'Add worksheet
        ActiveSheet.Name = WsName & "_" & i
    End With
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM