我正在编写一个用户表单,将一些同事数据输入到工作表中。但是,当我运行代码时,我的对象的值都是空的。有人可以给我一些关于员工姓名代码的建议吗?
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
这是显示设计的照片
正如我在评论中所说,你应该使用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