使用范围内的值更新userform

时间:2014-10-13 16:29:13

标签: excel vba userform

我目前有一张工作表,可以跟踪我的建筑物中的人员收到包裹到货的通知,然后是有关该包裹的信息。我有一个“发送提醒”按钮,显示用户表单,用于查看工作表并确定哪些包已等待两天以上,然后在表单上填写几个字段,并为用户填写适当的数据(我还没有任何声誉,所以无法发布图片。)

第一个文本框是Email,第二个是Subject,第三个是Message。有一个Next按钮只是导致表单再次初始化,所以我希望它只是查找已经等待的下一个包,如果找不到,则表单关闭。

问题在于,当我单击按钮显示表单时,“电子邮箱”框和“消息”框都显示为空白,但主题框很好。

Private Sub UserForm_Initialize()

Dim checkRange As Range
Dim rCell As Range
Dim k As Integer

On Error Resume Next
k = 0
Sheets(1).Activate
checkRange = ActiveSheet.Range("D3", "D100")

For Each rCell In checkRange.Cells
    If rCell.Value >= 2 Then
        Email.Value = Cells(rCell.Row, 1).Value
        Subject.Value = "Package Reminder"
        Message.Value = "This is a reminder that you have a package from " & _
        Cells(rCell.Row, 2).Value & _
        " waiting for you in the machine shop."
        k = 1
        Cells(rCell.Row, 3).Value = Date
        Exit For
    End If
Next rCell

If k = 0 Then
    Unload Me
    MsgBox ("All reminders sent!")
End If

End Sub

1 个答案:

答案 0 :(得分:1)

修改

我必须失明;我没有意识到这行代码在做什么!

checkRange = ActiveSheet.Range("D3", "D100")

这应该是:

checkRange = ActiveSheet.Range("D3:D100")

希望这会有所帮助;抱歉慢! :)

<强>原始

我尝试编写你描述的代码,看它是如何运作的。

这就是我想出来的;我希望这能以某种方式帮助你!

我不确定您的代码有什么问题,因为这几乎是一样的,它按预期工作:

enter image description here

Private Sub UserForm_Initialize()

Dim rCell
Dim foundPackage
foundPackage = False

For Each rCell In Sheets("Sheet1").Range("D2:D12")
    If rCell.Value >= 2 And Sheets("Sheet1").Cells(rCell.Row, 3).Value = "" Then 'Added AND so it goes to the next one after a date is set
        Email.Value = Sheets("Sheet1").Cells(rCell.Row, 1).Value
        Subject.Value = "Package Reminder"
        Message.Value = "This is a reminder that you have a package from " & _
            Sheets("Sheet1").Cells(rCell.Row, 2).Value & " waiting in the machine shop."
        Sheets("Sheet1").Cells(rCell.Row, 3).Value = Date 'This will set the date on the cell - I believe this is as intended
        foundPackage = True
        Exit For
    End If
Next rCell

If foundPackage = False Then
    Unload Me
    MsgBox ("All reminders sent!")
End If
End Sub

enter image description here