2013-05-04 3 views
0

저는 VBA에 대한 완전한 초보자입니다.행 및 열 내용을 기반으로 셀을 찾고 해당 셀을 업데이트하는 데 사용되는 VBA 코딩은 무엇입니까?

스프레드 시트에 나열된 특정 날짜에 완료되는 작업 수를 업데이트하는 사용자 양식을 만들려고합니다. 나는 숨겨진 두 개의 botton (서브 루틴의 조건으로 나타나는)이있는 Userform을 구상합니다. 저는 Mac에서 일하고 있습니다. VBA가 PC에서 사용하기 위해 코딩에 영향을 줄 것이라는 것을 알고 있습니다.

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

인수의 위해, 내가 업데이트 할 또는 입력 가정 해 봅시다 :

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

예 정의 폼 (a)는 이것이다 :

예 시트는 이것이다 Greg이 5 월 7 일 (2013/05/07)에 완료 한 작업 수

정의 폼이 같은 진행을 위해 내가 좋아하는 것 :

입력을 사람과 날짜 :

enter image description here

그런 다음, 버튼 클릭 후 7 일 그렉위한 작업의 수를 검색 :

enter image description here

이제는 그렉이 완성한 것을 알고 싶습니다. 스프레드 시트에서

enter image description here

그리고 결과 : 테드 7 6 작업과 나는 두 번째 버튼 (지금 눈에 보이는 숨겨진 첫 번째 단추)를 클릭

enter image description here

I가 해야지 여기에 몇 가지 코드를 입력하십시오. 그러나 코드에 대한 내 기술과 완성도가 필요합니다. 하지만 내가 가지고있는 것을 넣을 것입니다 :

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

고맙습니다. 나는 아직도 문제에 부딪치고있다. VBA/매크로를 실행할 때 오류가 발생합니다. "런타임 오류 '91': 개체 변수 또는 With 블록 변수가 설정되지 않았습니다." '디버그'를 클릭하면 다음과 같이 나타납니다. " 'inut 날짜를 찾고 있습니다."1 With ThisWorkbook.Worksheets ("Sheet1") frmDate = .Rows (1) .Find (CDate (dt01)). End With' –

+0

나는이 테스트를 완료했다 ...하지만 그것은 3 라인이다. 또한 변수를 올바르게 설정해야합니다. 흥미롭게도 그것은 이전 라인을 지나서 ok로 돌아갔다. – glh