Vba 此代码的哪一部分会显示用于选择项目的消息框

Vba 此代码的哪一部分会显示用于选择项目的消息框,vba,excel,Vba,Excel,我雇了一个人帮我写一个宏来填充电子表格中的表格。我觉得我通常可以理解一些编写的代码,但这是我无法理解的。我只是想学习如何为自己做到这一点 Option Explicit Option Base 1 Dim s_no() As String Sub createReport() start_win.Show End Sub Sub ook() Dim last As Integer ReDim s_no(1 To 1) If Not Sheet1.Range("A2").Value

我雇了一个人帮我写一个宏来填充电子表格中的表格。我觉得我通常可以理解一些编写的代码,但这是我无法理解的。我只是想学习如何为自己做到这一点

Option Explicit
Option Base 1
Dim s_no() As String
Sub createReport()
start_win.Show
End Sub



Sub ook()


Dim last As Integer

ReDim s_no(1 To 1)

If Not Sheet1.Range("A2").Value = "" Then
    s_no(1) = Sheet1.Range("A2").Value
Else
    MsgBox "Empty sheet"
End If

last = Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
Dim i As Integer
For i = 2 To last
    If already_exists(Sheet1.Range("A" & CStr(i)).Value) = False Then
        ReDim Preserve s_no(1 To UBound(s_no) + 1)
        s_no(UBound(s_no)) = Sheet1.Range("A" & CStr(i)).Value
    End If
Next

For i = 1 To UBound(s_no)
    Debug.Print s_no(i)
Next
End Sub

Function already_exists(trial)
already_exists = False
Dim i As Integer
For i = 1 To UBound(s_no)
    If s_no(i) = trial Then
        already_exists = True
        Exit Function
    End If
Next
End Function

你花钱请人写的?我会要回我的钱。
如果您在
start\u win
表单中提供代码,我可以根据您的需要将其拆分

我在解释的每一行都添加了注释

Option Explicit                 'All variables must be declared first.
Option Base 1                   'Arrays start at 1 rather than 0.
Dim s_no() As String            'Global array variable.
                                'Available to all procedures in the module.

Sub createReport()
start_win.Show                  'Open and display a form called `start_win`.
                                'Form will likely contain code as well.
End Sub



Sub ook()                       'Reference to disc-worlds librarian?
                                '(and the name of this procedure)


Dim last As Integer             'The 'last' variable will hold values between
                                '–32,768 to 32,767.
                                'Terrible for holding row numbers.

ReDim s_no(1 To 1)              'The global variable is reassigned as an
                                'array containing 1 element.

If Not Sheet1.Range("A2").Value = "" Then   'Sheet1 is the sheet codename
                                            '(name not in brackets in Project explorer).
                                            'If cell A2 is an empty string then go to
                                            'next line, otherwise message box.

    s_no(1) = Sheet1.Range("A2").Value      'Place value from A2 into "s_no(1)"
                                            'element of variable.
Else
    MsgBox "Empty sheet"                    'If cell A2 was an empty string then
                                            'jump to this line.
End If

last = Cells(Sheet1.Rows.Count, 1).End(xlUp).Row    'Get the last row number from the
                                                    'sheet with codename Sheet1 and
                                                    'store in 'last' variable.
                                                    'Didn't I say that was a terrible idea?
                                                    'This line will fail if the last
                                                    'row is >32,767 (Overflow error).

Dim i As Integer                                    'Again, terrible idea - integer for row
                                                    'numbers... no.  Use LONG instead.

For i = 2 To last                                   'Step from row 2 to last row
                                                    '(providing error hasn't happened).

    If already_exists(Sheet1.Range("A" & CStr(i)).Value) = False Then   'Pass the value from
                                                                        'row number 'i' in column A
                                                                        'to the 'already_exists' procedure
                                                                        'where it will be called 'trial'
                                                                        '"Sheet1.Cells(i,2)" would be better.

        ReDim Preserve s_no(1 To UBound(s_no) + 1)                      'Increase the size of the 's_no'
                                                                        'array while keeping an values it
                                                                        'already holds.

        s_no(UBound(s_no)) = Sheet1.Range("A" & CStr(i)).Value          'Place the value from column A
                                                                        'in the array.
    End If
Next

For i = 1 To UBound(s_no)   'Cycle through the array.
    Debug.Print s_no(i)     'Place the value from the array in the immediate window.
Next
End Sub

Function already_exists(trial)      'Function to return a variant value
                                    '(should specify the return type).

already_exists = False              'Start as False.. so it's a boolean.
                                    'Be better to declare that in the function name.

Dim i As Integer                    'There's that integer again.  Just stop...

For i = 1 To UBound(s_no)           'Cycle through each element in 's_no' array.

    If s_no(i) = trial Then         'Does that element equal the one passed
                                    'from the main procedure?

        already_exists = True       'If it does then return TRUE to the main procedure.

        Exit Function               'Exit the function and jump back to the main procedure.
                                    'Would be the better to exit the loop and have one
                                    'exit point for the function.
    End If
Next
End Function
编辑:
接下来,我将编写
ook
过程,并删除已经存在的
函数

Sub ook()

    Dim lLast As Long      'Holds last row number.
    Dim i As Long          'Holds current row number.
    Dim s_no As Object     'Define an object.
    Dim key As Variant     'Use this to step through the populated dictionary.

    Set s_no = CreateObject("Scripting.Dictionary") 'Define it as a dictionary.

    With Sheet1
        lLast = .Cells(.Rows.Count, 1).End(xlUp).Row

        'Previously assumed that A2 would hold a value,
        'so if last row in column A is 1 then A2 will be blank
        'and the sheet is empty.
        If lLast <> 1 Then

            For i = 2 To lLast
                'Does the value already exist in the dictionary object?
                If Not s_no.exists(.Cells(i, 1).Value) Then
                    s_no.Add .Cells(i, 1).Value, .Cells(i, 1).Value 'It doesn't, so add it.
                End If
            Next i

            For Each key In s_no
                Debug.Print s_no(key)
            Next key

        Else
            'Nothing else should happen if the sheet is empty.
            MsgBox "Empty sheet", vbCritical + vbOKOnly

        End If
    End With

End Sub  
Sub-ook()
“Dim lLast As Long”保存最后一行编号。
“Dim i As Long”保存当前行数。
“将s_编号作为对象”定义对象。
Dim key As Variant’使用此选项逐步浏览填充的字典。
设置s_no=CreateObject(“Scripting.Dictionary”)'将其定义为字典。
附页1
lLast=.Cells(.Rows.Count,1).End(xlUp).Row
'之前假设A2将持有一个值,
'因此,如果列A中的最后一行是1,则A2将为空
“床单是空的。
如果是骆驼1那么
对于i=2到1
'该值是否已存在于dictionary对象中?
如果不存在序号(.Cells(i,1).Value),则
s_no.Add.Cells(i,1).Value、.Cells(i,1).Value'它没有,所以添加它。
如果结束
接下来我
对于s_编号中的每个键
调试。打印序号(键)
下一键
其他的
“如果工作表是空的,就不应该发生其他事情。
MsgBox“空表”,vbCritical+vbOKOnly
如果结束
以
端接头

另一个编辑:-抱歉,通常不要链接到此网站之外,但这是一个很好的教程。

只有一个部分打开了一个消息框,在这里是“MsgBox”空白页,您可以添加更多关于您需要帮助的信息吗?您可能需要发布
start\u win
表单的代码。