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

时间:2017-12-03 00:30:30

标签: 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 = "" 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

2 个答案:

答案 0 :(得分:0)

你付钱给某人写了吗?我要求退还我的钱 如果您提供start_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过程并取消already_exists函数的。

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  

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

答案 1 :(得分:-2)

正如@ Yow32K所示,似乎有一个自定义表单“start_win”是帮助您创建的人。

start_win.Show将是实际显示该表单的行。