用户表单填充工作表

时间:2014-01-24 20:02:30

标签: excel vba populate

我仍然很擅长使用VBA,所以如果我不了解所有首选条款,我会道歉。

我正在尝试编写用户表单来填充电子表格,以便我跟踪所使用的员工PTO。此电子表格是作为日期的X轴(第1行)和作为员工姓名的Y轴(第A列)。

我创建的用户表单有5条标准:

1)  Employee Name (ComboBox1; this is a drop down of multiple rows)
2)  Date of PTO (ComboBox2; this is supposed to be a drop down of multiple columns)
3)  Duration of PTO (OptionButton1 is 1 Day, OptionButton2 is 0.5 Day)
4)  Enter Data (CommandButton1)
5)  Cancel (CommandButton2)

我已经通过选择并标记电子表格上的菜单来完成标准1.

剩下的就是踢我的东西。

我似乎无法获得日期(标准2),而在行格式中,显示在ComboBox2下拉列表中。如果我将这些项目转换为单个列,我就可以标记该组并创建下拉菜单。这是有问题的,因为我无法形成一个网格来填充。

我的OptionButton1和OptionButton2项目有意成为输入值(1和0.5)。在这个时候,我一直无法给他们价值,只是名字。此输入是主要问题的次要问题,我无法使网格格式起作用(上面的段落)。

第三个问题是标准4和5的组合。我的脚本无法执行CommandButton1,但我相信这是由于我非常基本的代码:

Private Sub CommandButton1_Click()
    cmdOK
End Sub

CommandButton2似乎使用此代码工作:

Private Sub CommandButton2_Click()
    cmdCancel
End Sub

我的结局都是问题,描述了我的问题,将是“我的最终目标是否可行?”我只打算使用Excel,因为许多人已经可以访问它。如果这是可能的,那么我会要求帮助识别我的错误点和纠正,或者链接到编码手册,我可以尝试更多地了解这些功能(这更像是一个长期的情况,这不是'对这个问题很有帮助,但我觉得没问题。)

提前感谢您的回复。

编辑:

@ Seth

我已经做到了ComboBox2(日期)选择。我无法获得下面建议的循环来访问值(1行,365列)。

我尝试修改Seth的代码,但我真的对循环一无所知;我觉得我很可能没有恰当地填写一个部分(我正在看.Value<>“”):

Private Sub ComboBox2_Change()     Dim i As Long

i = 1

Do While ThisWorkbook.ActiveSheet("Breakdown").Cells("A1:NC1", i).Value <> ""

Me.ComboBox1.AddItem (ThisWorkbook.ActiveSheet("Breakdown").Cells("A1:NC1", i).Value)
i = i + 1

Loop

End Sub

使用此代码,我收到错误“运行时错误'438':对象不支持此属性或方法”

我正在尝试找到理解编写循环的一般指南,虽然我没有找到上述某些语句的参考,所以我不肯定如何纠正我的问题。我相信它也可能与盒子的属性有关。我可以在RowSource下输入“日期”(我的通用术语,如突出显示和标记所定义的那样),尽管这不是循环行。

@ Alex D

我也在试着收到第二篇文章的路径;我尝试使用时收到运行时错误:

Private Sub UserForm_Initialize()
    For i = 2 To Sheets("Breakdown").UsedRange.Columns.Count
         comboDate.AddItem (Cells(1, i))
        comboDate.List(comboDate.ListCount - 1, 1) = i
    Next
End Sub

可能移动到定义数据的位置会使这个工作吗?一般来说,我在工作表中有一个按钮(也就是一个选择一个单元格并弹出用户窗体),所以可能不需要初始化?

Cyril 20140127(1700 UTC)

2 个答案:

答案 0 :(得分:1)

好的,我们按相反的顺序进行。对于你的上一个问题,“我的最终目标是否可能?”,答案是肯定的。我不太理解您的推理,但如果您更喜欢使用Excel,那就这样吧。但我确实认为,这将是记录PTO的一种痛苦方式。例如,如果我休息一周怎么办?我是否必须每天填写此表格一次?

要继续向后移动,您的按钮需要做一些事情。如果cmdCancelcmdOK是您在别处定义的潜艇,那么您已经发布的内容尚不清楚,那么让我们假装它们不是。在“取消”按钮中,您需要执行一项基本操作,关闭表单。这应该就这么简单,Unload Me

你的OK按钮是宏的关键部分。这是您将用户输入的值拼接在一起的位置。它也是您使用选项按钮执行操作的地方。您实际上没有为选项按钮分配值,您可以检查它们的值属性。因此,当用户单击“确定”时,运行的代码将包含以下内容:

If OptionButton1.Value = True Then
     'Put 1 in the cell.
Else
     'Put .5 in the cell.
End if

请注意,没有必要明确检查OptionButton2,因为您只有两个选择。

最后,日期。听起来日期已经在您的电子表格中,在某个特定的行中。你需要做的是遍历该行中的单元格并将值拉入组合框的列表中。这应该让你开始。把它放在UserForm_Activate表单事件:

Dim i As Long

i = 1

Do While ThisWorkbook.ActiveSheet.Cells(DateRow, i).Value <> ""

    Me.ComboBox1.AddItem (ThisWorkbook.ActiveSheet.Cells(DateRow, i).Value)
    i = i + 1

Loop

现在我们返回OK按钮。您需要获取在日期组合框中进行的选择。为此,您需要阅读组合框的ListIndex属性并添加一个。 (添加1个帐户,因为组合框中的列表从0开始,而不是1.)返回的数字将是您插入数据的列。

“确定”按钮的完整代码如下:

Dim Col As Long
Dim Row As Long
Dim PTOValue As Long

Col = DateComboBox.ListIndex + 1
Row = EmployeeNameComboBox.ListIndex + 1

If FullDayComboBox.Value = True Then
     PTOValue = 1
Else
     PTOValue = .5
End if

ThisWorkbook.ActiveSheet.Cells(DateRow, i).Value = PTOValue

希望这会让你开始。

答案 1 :(得分:0)

填写您的标准,应该是可以理解的。假设名称从单元格(2,1)开始,日期在单元格(1,2)中开始。您可能必须根据所拥有的内容更改元素的某些名称。但我完全建立了像你描述的东西,它完美无缺。

确实需要一些数据验证来防止错误,但您应该能够处理错误。

'Ok button which sets the values
Private Sub CommandButton1_Click()
    Columns("A:A").Select
    If OptionButton1.Value = True Then
        Cells(Get_Name_Row(comboName.Text), Get_Date_Column(comboDate.Text)).Value = 1
    Else
        Cells(Get_Name_Row(comboName.Text), Get_Date_Column(comboDate.Text)).Value = 0.5
    End If
End Sub

'Cancel Button
Private Sub CommandButton2_Click()
    Unload Me
End Sub

'This loads the dates into your comboBox, make sure to update its name
'I assume you use RowSource for your names, so those should be fine
Private Sub UserForm_Initialize()
    For i = 2 To Sheets("Sheet1").UsedRange.Columns.Count
        comboDate.AddItem (Cells(1, i))
        comboDate.List(comboDate.ListCount - 1, 1) = i
    Next
End Sub

'Gets what row the name is from
Public Function Get_Name_Row(SearchString As String) As Integer
    Dim Rng As Range
    If Trim(SearchString) <> "" Then
        With Sheets("Sheet1").Range("A:A")
            Set Rng = .Find(What:=SearchString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Get_Name_Row = CInt(Rng.Row)
            End If
        End With
    End If
End Function

'Gets what column the date is from
Public Function Get_Date_Column(SearchString As String) As Integer
    Dim Rng As Range
    If Trim(SearchString) <> "" Then
        With Sheets("Sheet1").Range("A1").EntireRow
            Set Rng = .Find(What:=SearchString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Get_Date_Column = (Rng.Column)
            End If
        End With
    End If
End Function
相关问题