0

我确实是 VBA 的初学者。

我正在尝试创建一个用户表单,该表单将更新一个人在电子表格上列出的给定日期完成的任务数。我设想用户窗体有两个按钮(它们被隐藏并显示为子例程的条件)。顺便说一句,我正在使用 Mac,而且我知道这将对在 PC 上使用 VBA 编码产生影响,反之亦然。

示例表是这样的:

我想使用 Userforms 和 VBA 搜索和更新的示例简单电子表格

示例用户表单 (a) 是这样的:

用户表单的第一阶段,我希望用户表单为人员和日期搜索工作表并检索相应单元格中的任务数

为了争论,假设我想更新或输入 Greg 在 5 月 7 日 (2013/05/07) 完成的任务数。

我希望 Userform 进行如下操作:

输入人员和日期:

在此处输入图像描述

然后,在按钮单击后 7 日检索 Greg 的任务数:

在此处输入图像描述

现在,我想输入我知道 Greg 在 7 日完成了 6 个任务,然后单击第二个按钮(现在可见,第一个按钮隐藏):

在此处输入图像描述

电子表格中的结果:

在此处输入图像描述

我应该在这里输入一些代码,但是我的技能和代码的完整性是需要的。但我会投入我所拥有的:

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

提前感谢您的任何帮助!

4

1 回答 1

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
于 2013-05-04T11:19:01.233 回答