2017-03-08 69 views
0

我有一個程序,根據作業類型,將用戶輸入信息放在工作表中。爲了達到這個目的,我們只關注圖紙(「活動作業」)。在函數VBA中傳遞對象變量時出現故障

我有一個函數,它從UserForm獲取信息,並根據jobID將它放入單元格中。一個簡單的計數跟蹤的作業ID,並且工作放入紙張,根據其ID,因爲這樣的:

Public btn As Button 
Public t As Range 

Function GetActiveJob() 

Dim i1 As Integer 
i1 = Worksheets("Sheet1").Range("C4").Value '//Keeps count of jobs, gives the JobID 

    With Worksheets("Active Jobs") 
    .Range("A" & (5 * i1)) = UserForm1.TextBox1 
    ActiveSheet.Range("B" & (5 * i1) & ":" & "E" & (3 + (5 * i1))).Merge 
    .Range("B" & (5 * i1)) = UserForm1.TextBox2 '//Handles a decent bit of text 
    .Range("B" & (5 * i1) & ":" & "E" & (3 + (5 * i1))).VerticalAlignment = xlTop 
    Set t = ActiveSheet.Range("G" & (5 * i1)) 
    Set btn = Worksheets("Active Jobs").Buttons.Add(t.Left, t.Top, t.Width, t.Height) 

    With btn 
    .Name = "RemovetoArchive" & i1 
    .Caption = "Archive" 
    .OnAction = "RemovetoArchive" 
    End With 

    End With 

UserForm1.TextBox1.Value = "" 
UserForm1.TextBox2.Value = "" 

i1 = i1 + 1 
Worksheets("Sheet1").Range("C4").Value = i1 
MsgBox ("I1 = " & i1) 'For testing purposes 

End Function 

(作業ID不一定是唯一的)

正如你所看到的,用戶窗體創建一個名稱(函數的名稱)後跟jobID的按鈕。

當作業完成後,&歸檔按鈕被按下時,我想將它移動到一個新的工作表,稱爲存檔。

爲此,我走串並操縱它來獲得的ID號,然後將其轉換字符串到整數:

Function RemovetoArchive() 

    Dim ButtonID As Integer 
    Dim ButtonString As String 

    ButtonString = Mid(btn.Name, 16, 2) '<-- Error here, (see below) 
    ButtonID = CInt(ButtonString) 

    Worksheets("Active Jobs").Range("A" & (5 * ButtonID) & ":R" & (3 + (5 * ButtonID))).Select 


    MsgBox ("ButtonID is " & ButtonID) 'Testing 

的ID號被用作一個變量來確定需要選擇哪個範圍,並轉移到檔案。然而

運行,我得到

Object variable or With Block Variable not set 
我在與設置btn.Name對象問題

。 即

I enter a job with JobID '5' 
The information is created along with a button. 
I press Button 5, a.k.a "RemovetoArchive5" 

我的代碼應該從字符串「RemovetoArchive5」只取數字字符,並將其轉換爲整數,用作ButtonID。

因爲它是現在,btn.Name,不具備價值

btn.Name = "RemovetoArchive5" 

請問這個有在GetActiveJobs功能做?點擊它引用的按鈕後,如何傳遞btn.Name對象?

任何幫助將不勝感激。

+0

術語「傳遞對象變量的函數「通常意味着*參數*,而你的函數沒有。你讀過*參數*嗎? 'btn'似乎是一個模塊級變量,又名「公共領域」或「全局變量」,這是「傳遞參數」 –

+0

@SJR這爲我提供了相同的結果。我現在有,通過測試的對面,也許更合乎邏輯。我認爲我遇到的問題是,在調用「RemovetoArchive」函數時,btn.Name屬性未設置,即沒有值,因此無法操作字符串。 –

+0

如果錯誤是運行時錯誤91,那麼它不是'Name'屬性沒有值,它是'btn'引用本身; 'btn'是'Nothing'。在哪個模塊中聲明瞭'btn'?一個標準/程序模塊?工作表/類模塊?用戶表單的代碼隱藏?請參閱Documentation.SO的VBA主題中的[Variables and Scopes](http://stackoverflow.com/documentation/vba/877/declaring-variables/2957/variables#t=201703081638244410797)。 –

回答

1

使用您在創建按鈕就可以,充其量,返回你所創建的最後一個名稱創建一個臨時對象的名稱 - 甚至認爲將是不可能的,一旦該臨時對象是garbage-集。

所以,如果你想找到調用RemoveToArchive按鈕的Name,你應該做類似下面的,它使用Application.Caller,以確定哪個按鈕被按下:

Sub RemovetoArchive() 'Only use Function when you have a function, 
         'use Sub when you have a subroutine 
    Dim ButtonID As Integer 
    Dim ButtonString As String 

    ButtonString = Mid(Application.Caller, 16, 2) 
    ButtonID = CInt(ButtonString) 

    Worksheets("Active Jobs").Range("A" & (5 * ButtonID) & ":R" & (3 + (5 * ButtonID))).Select 

    MsgBox ("ButtonID is " & ButtonID) 'Testing 

End Sub 

一隨後對馬克菲茨傑拉德對這個問題的評論也讓我看到你的GetActiveJob代碼中存在潛在的問題。您的代碼部分內容如下:

With Worksheets("Active Jobs") 
    .Range("A" & (5 * i1)) = UserForm1.TextBox1 
    ActiveSheet.Range("B" & (5 * i1) & ":" & "E" & (3 + (5 * i1))).Merge 
    .Range("B" & (5 * i1)) = UserForm1.TextBox2 '//Handles a decent bit of text 
    .Range("B" & (5 * i1) & ":" & "E" & (3 + (5 * i1))).VerticalAlignment = xlTop 
    Set t = ActiveSheet.Range("G" & (5 * i1)) 
    Set btn = Worksheets("Active Jobs").Buttons.Add(t.Left, t.Top, t.Width, t.Height) 

    With btn 
    .Name = "RemovetoArchive" & i1 
    .Caption = "Archive" 
    .OnAction = "RemovetoArchive" 
    End With 

End With 

ActiveSheet有一些引用。如果活動工作表不是「活動工作」工作表,則會導致問題。(但它顯然會工作,如果你總是確保活動工作表「活動作業」。)爲了避免出現問題,我建議你更改代碼:

With Worksheets("Active Jobs") 
    .Range("A" & (5 * i1)) = UserForm1.TextBox1 
    .Range("B" & (5 * i1) & ":" & "E" & (3 + (5 * i1))).Merge 
    .Range("B" & (5 * i1)) = UserForm1.TextBox2 '//Handles a decent bit of text 
    .Range("B" & (5 * i1) & ":" & "E" & (3 + (5 * i1))).VerticalAlignment = xlTop 
    Set t = .Range("G" & (5 * i1)) 
    Set btn = .Buttons.Add(t.Left, t.Top, t.Width, t.Height) 

    With btn 
    .Name = "RemovetoArchive" & i1 
    .Caption = "Archive" 
    .OnAction = "RemovetoArchive" 
    End With 

End With 
+0

這解決了它,感謝您的額外信息。作業被放置在由三個選項按鈕之一的值確定的頁面上。所以我打算做這樣的事情: 例如,如果optionbutton2.value = true,那麼 Sheet2.activate 例如,但你的建議肯定是更好的imo。謝謝! –