2013-05-04 39 views
0

我是一個真正的VBA初學者,真實。可以使用UserForm中的VBA編碼來根據行和列內容查找單元格,然後更新該單元格?

我正在嘗試創建一個用戶表單,它將更新某個人在電子表格中列出的給定日期內完成的任務數量。我設想Userform有兩個botton(它們是隱藏的,並且作爲子例程的條件出現)。順便說一下,我正在Mac上工作,而且我知道在PC上使用VBA編碼會產生影響,反之亦然。

的示例片材是這樣的:

Sample, simple spreadsheet table I want to search and update with Userforms and VBA

示例用戶窗體(a)是這樣的:

First stage of user form where I would like for the userform to serch the sheet for the person and date and retrieve the number of tasks in the appropriate cell

爲了便於討論,我們說我希望更新或輸入的Greg在5月7日完成的任務數量(2013/05/07)。

我想爲用戶窗體進行這樣的事情:

進入者和日期:

enter image description here

然後,點擊按鈕後第7檢索格雷格任務數:

enter image description here

現在,我想輸入我知道格雷格補全泰德7日6個任務,我單擊第二個按鈕(現在可見的第一個按鈕隱藏):在電子表格中

enter image description here

而結果:

enter image description here

我應該在這裏輸入一些代碼,但我的技能和代碼的完整性是想要的。但我會把我有什麼:

Option Explicit 

'Subroutine when clicking the first ("find") button 
Private Sub btnfind_Click() 
    lbltasks.Vissible = True 
    txttasks.Visible = True 
    btnupdate.Visible = True 
    btnfind.Visible = False 

'Defining variables 
    Dim pr01 As String 
    Dim dt01 As Date 
    Dim tsk01 As Integer 

'Assigning variables to inputs 
    pr01 = txtperson.Text 
    dt01 = txtdate.Text 
    tsk01 = txttask.Text 

'Looking for Name in column "A" 
    ' ? ? ? 

'Looking for inut Date in row "1" 
    ' ? ? ? 

'Retrieving the existing number of tasks according to name and date 
'and showing number in the 'tasks' text input box in the user form 
    ' ? ? ? 
End Sub 

'Subroutine when clicking the Second ("update") button 
Private Sub btnupdate_Click() 

'Paste updated Number of tasks in appropriate cells according to name and date 
'The new number of tasks should over write what was there previously 
    ' ? ? ? 

End Sub 

在此先感謝您的任何和所有幫助!

回答

0

這應該工作。請學習它,並使用Excel中的宏記錄功能掌握更多:

Option Explicit 
Public frmName As Variant 'store row of name 
Public frmDate As Variant 'store column of date 

'Subroutine when clicking the first ("find") button 
Private Sub btnfind_Click() 
'Defining variables 
    Dim pr01 As String 
    Dim dt01 As Date 
    Dim tsk01 As Integer 

'Assigning variables to inputs 
    pr01 = UserForm1.TextBox1.Text 
    dt01 = UserForm1.TextBox2.Text 
    tsk01 = UserForm1.TextBox3.Text 

'Looking for Name in column "A" 
    With ThisWorkbook.Worksheets("Sheet4") 
     frmName = .Columns("A").Find(pr01).Row 
    End With 


'Looking for inut Date in row "1" 
    With ThisWorkbook.Worksheets("Sheet4") 
     frmDate = .Rows(1).Find(CDate(dt01)).Column 
    End With 

    If frmName Is Nothing Or frmDate Is Nothing Then 
     'not found 
     Exit Sub 
    End If 

'Retrieving the existing number of tasks according to name and date 
'and showing number in the 'tasks' text input box in the user form 
    With ThisWorkbook.Worksheets("Sheet4") 
     UserForm1.TextBox3.Text = .Cells(frmName, frmDate) 
    End With 

End Sub 

'Subroutine when clicking the Second ("update") button 
Private Sub btnupdate_Click() 

'Paste updated Number of tasks in appropriate cells according to name and date 
'The new number of tasks should over write what was there previously 
    With ThisWorkbook.Worksheets("Sheet4") 
     .Cells(frmName, frmDate) = UserForm1.TextBox3.Text 
    End With 
End Sub 
+0

Thanks @glh。但我仍然遇到問題。當我運行VBA /宏時,出現en錯誤:「運行時錯誤'91':對象變量或塊變量未設置。」當我點擊「調試」時,我來行:''尋找inut日期在行中「1」 With ThisWorkbook.Worksheets(「Sheet1」) frmDate = .Rows(1).Find(CDate(dt01))。Column End With# – 2013-05-06 13:55:20

+0

我測試了這個好...這是3行。還要確保你正確設置了變量。有趣的是,它在上一行之後運行良好。 – glh 2013-05-06 18:27:15

相關問題