声明变量的方法

时间:2015-10-07 08:35:42

标签: vba excel-vba excel

我在vba中遇到了问题。

以下是代码:

Sub GetData()
If IsNumeric(UserForm1.TextBox1.Value) Then
flag = False
i = 0
id = UserForm1.TextBox1.Value
Do While Cells(i + 1, 1).Value <> ""

    If Cells(i + 1, 1).Value = id Then
        flag = True
        For j = 2 To 3
            UserForm1.Controls("TextBox" & j).Value = Cells(i + 1, j).Value
        Next j
    End If

    i = i + 1

Loop

If flag = False Then
    For j = 2 To 3
        UserForm1.Controls("TextBox" & j).Value = ""
    Next j
End If

Else
ClearForm
End If
End Sub

我的问题是第一个If语句。它仅在我声明变量时起作用:

Dim id As Integer
Dim i As Integer

Dim i, id As Integer

但当我声明它们时,工作:

Dim id, i As Integer

当条件为假时,它就像。

有什么区别?

2 个答案:

答案 0 :(得分:0)

当您用逗号分隔的行中的多个变量

e.g。

Dim i, id as Integer

..只有第一个变量(i)才能正确标注尺寸。第二个变量(id)将是一个变体。

答案 1 :(得分:0)

请参阅http://www.cpearson.com/excel/declaringvariables.aspx(特别是“#34;注意使用一个Dim语句声明的变量&#34;”),以便详细讨论如何声明变量。在您的情况下,只有Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 20: @if (count > 0) { Line 21: int i = 0; Line 22: foreach (WidgetPart widget in widgets.Where(w => w.Zone == zone).OrderBy(w => w.Position, new Orchard.UI.FlatPositionComparer())) { Line 23: <li class="widgets-@(widget.LayerId == Model.CurrentLayer.Id ? "this" : "other")-layer widgets-layer-@widget.LayerId@(i == 0 ? " first" : (i == count ? " last" : ""))"> Line 24: @using (Html.BeginFormAntiForgeryPost()) { Source File: e:\inetpub\wwwroot\TestWebsite\Modules\Orchard.Widgets\Views\WidgetPlacement.Zones.cshtml Line: 22 具有显式类型(id)。变量Integer将是默认类型i

请注意,这与VB.NET不同,后者的两个变量都有Variant类型。

相关问题