VBA用户表单数据填写

问题描述 投票:-1回答:1

我正在编写一个用户表单,将一些同事数据输入到工作表中。但是,当我运行代码时,我的对象的值都是空的。有人可以给我一些关于员工姓名代码的建议吗?

Private Sub txtstaffname_Change()
    staff_name = txtstaffname.Value
End Sub
_________________________________________________________________________    

Private Sub Done_Click()

'Updat Maintenance Sheet'
Application.EnableEvents = False

r = Application.CountA(Range("A:A")) + 1

With Worksheets("For Maintenance Propose")
    .Cells(1, r).Value = staff_name
End With

Application.EnableEvents = True

End Sub

这是显示设计的照片

enter image description here

vba excel-vba userform
1个回答
0
投票

正如我在评论中所说,你应该使用Option Explicit,然后在每个Sub中都不会在本地定义staff_name。我假设您发布的代码位于userform的模块中。然后,以下更改可以修复您的代码

Option Explicit

Dim staff_name as string

Private Sub txtstaffname_Change()
    staff_name = txtstaffname.Value
End Sub
_________________________________________________________________________    

Private Sub Done_Click()
dim r as long
'Updat Maintenance Sheet'
Application.EnableEvents = False

r = Application.CountA(Range("A:A")) + 1

With Worksheets("For Maintenance Propose")
    .Cells(1, r).Value = staff_name
End With

Application.EnableEvents = True

End Sub

PS我也猜你真的想填充下面的单元格,所以它应该是

.Cells(r,1).Value = staff_name
© www.soinside.com 2019 - 2024. All rights reserved.