我有一個程序,根據作業類型,將用戶輸入信息放在工作表中。爲了達到這個目的,我們只關注圖紙(「活動作業」)。在函數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對象?
任何幫助將不勝感激。
術語「傳遞對象變量的函數「通常意味着*參數*,而你的函數沒有。你讀過*參數*嗎? 'btn'似乎是一個模塊級變量,又名「公共領域」或「全局變量」,這是「傳遞參數」 –
@SJR這爲我提供了相同的結果。我現在有,通過測試的對面,也許更合乎邏輯。我認爲我遇到的問題是,在調用「RemovetoArchive」函數時,btn.Name屬性未設置,即沒有值,因此無法操作字符串。 –
如果錯誤是運行時錯誤91,那麼它不是'Name'屬性沒有值,它是'btn'引用本身; 'btn'是'Nothing'。在哪個模塊中聲明瞭'btn'?一個標準/程序模塊?工作表/類模塊?用戶表單的代碼隱藏?請參閱Documentation.SO的VBA主題中的[Variables and Scopes](http://stackoverflow.com/documentation/vba/877/declaring-variables/2957/variables#t=201703081638244410797)。 –