2017-12-03 85 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 - 對不起,通常不鏈接這個網站之外,但它是一個很好的教程。

相關問題