2017-12-03 86 views
-1

我付了一个人来帮助我编写一个宏来填充电子表格中的表。我觉得我通常可以理解一些编写的代码,但这超出了我的想法。我只是想学习如何为自己做这件事。此代码的哪一部分提供了用于选择项目的msg框

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 
+1

只有一个部分打开了一个消息框,在这里是“MsgBox”Empty sheet“,你可以在你需要帮助的地方添加更多信息吗? –

+0

你可能需要发布'start_win'的代码, – YowE3K

回答

-2

正如@ Yow32K表明,它似乎有一个自定义窗体“start_win”是谁帮助你的人创建的。

start_win.Show将是实际提出这种形式的线。

+0

非常感谢你提供宝贵意见,我很抱歉花了这么长时间才这么说。 – Gilmer

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 - 对不起,通常不链接这个网站之外,但它是一个很好的教程。

相关问题