Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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 VBA中的For循环_Vba_Excel - Fatal编程技术网

Excel VBA中的For循环

Excel VBA中的For循环,vba,excel,Vba,Excel,我试图编写一个代码,要求用户输入3个目的地,每个目的地都应该有自己的工作表。这就是我目前所拥有的 Option Explicit 'Keep the following global variables Dim airfare As Currency Dim hotel As Currency Dim food As Currency Dim destSheet As Worksheet Dim nameDes As String Sub travel2() 'Keep local vari

我试图编写一个代码,要求用户输入3个目的地,每个目的地都应该有自己的工作表。这就是我目前所拥有的

Option Explicit
'Keep the following global variables
Dim airfare As Currency
Dim hotel As Currency
Dim food As Currency
Dim destSheet As Worksheet
Dim nameDes As String

Sub travel2()

'Keep local variabels
Dim numDays As Integer
Dim namePass As String
Call Macro2

End Sub

Sub Macro2()

Const noOfDestinations As Integer = 3
Dim ThisGo As Integer

For ThisGo = 1 To noOfDestinations
    nameDes = InputBox("Please enter the destination")
    If Len(nameDes) > 0 Then

        Set destSheet = Worksheets.Add
        destSheet.Name = nameDes

    Exit For
    End If
Next ThisGo
End Sub

以下是我对您的问题的建议解决方案:

-摆脱退出

-去掉一些“局部变量”,它们没有任何用处

-根据用户输入测试重复的工作表(使用
SheetExists()


注意。GoTo语句会使代码难以阅读和维护。只要有可能,请改用控制结构。

以上代码已修改,因此您可以使用Do…till而不是GoTo语句:

Option Explicit
'Keep the following global variables
Dim airfare As Currency, hotel As Currency, food As Currency
Dim destSheet As Worksheet
Dim nameDes As String

Sub travel2()

    'Keep local variables
    'Why???
    'Dim numDays As Integer
    'Dim namePass As String
    Call Macro2

End Sub

Sub myMacro2()

    Const noOfDestinations As Integer = 3
    Dim Counter As Integer

    Do
        nameDes = InputBox("Please enter the destination")
        If Len(nameDes) > 0 And SheetExists(nameDes) = False Then
            Set destSheet = Worksheets.Add
            destSheet.Name = nameDes
            Counter = Counter + 1
        Else
            MsgBox "Sheet already exists! Please try again."
        End If
    Loop Until Counter = noOfDestinations

End Sub

Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
    'Courtesy of Tim Williams
    Dim sht As Worksheet

    If wb Is Nothing Then Set wb = ThisWorkbook
    On Error Resume Next
    Set sht = wb.Sheets(shtName)
    On Error GoTo 0
    SheetExists = Not sht Is Nothing
End Function

有问题吗?您的缩进不一致,使此代码更难阅读。此外,您的
travel2()
子系统没有任何意义。它所做的只是声明2个它从未使用过的局部变量(因此有效地丢弃了它们),然后调用其他子变量。你想做什么?我们无法读懂您的想法。请摆脱退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出退出。查看一些预先编写的
工作表列表()。弹出窗口往往会惹恼用户,所以最好尽量少用。
Option Explicit
'Keep the following global variables
Dim airfare As Currency, hotel As Currency, food As Currency
Dim destSheet As Worksheet
Dim nameDes As String

Sub travel2()

    'Keep local variables
    'Why???
    'Dim numDays As Integer
    'Dim namePass As String
    Call Macro2

End Sub

Sub myMacro2()

    Const noOfDestinations As Integer = 3
    Dim Counter As Integer

    Do
        nameDes = InputBox("Please enter the destination")
        If Len(nameDes) > 0 And SheetExists(nameDes) = False Then
            Set destSheet = Worksheets.Add
            destSheet.Name = nameDes
            Counter = Counter + 1
        Else
            MsgBox "Sheet already exists! Please try again."
        End If
    Loop Until Counter = noOfDestinations

End Sub

Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
    'Courtesy of Tim Williams
    Dim sht As Worksheet

    If wb Is Nothing Then Set wb = ThisWorkbook
    On Error Resume Next
    Set sht = wb.Sheets(shtName)
    On Error GoTo 0
    SheetExists = Not sht Is Nothing
End Function