VBA用户表单数据填充
我正在编写一个用户表单,将一些同事的数据输入到工作表中。但是,当我运行代码时,对象的值都是空的。有人能给我一些建议,比如我的员工姓名代码吗VBA用户表单数据填充,vba,excel,userform,Vba,Excel,Userform,我正在编写一个用户表单,将一些同事的数据输入到工作表中。但是,当我运行代码时,对象的值都是空的。有人能给我一些建议,比如我的员工姓名代码吗 Private Sub txtstaffname_Change() staff_name = txtstaffname.Value End Sub _________________________________________________________________________ Private Sub Done_Click(
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
这是展示设计的照片
正如我在评论中所说的,您应该使用
选项Explicit
,这样员工的名字就不会在每个子公司中本地定义。
我假设您发布的代码位于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
代码是什么?在userform的模块中?你在哪里定义员工的名字?我的建议是始终使用
选项Explicit
,感谢您的及时回复。我可以知道,如果两行中的某个单元格合并,是否有计算行数的解决方案?例如,A1和A2合并。但是,无论是“r=Application.CountA(Range(“A:A”))+1”还是“r=Range(“A:A”).End(xlDown).Count+1”都不能为我提供正确的行号,如3。我的建议始终是:不要使用合并单元格。我不想花任何时间在合并的单元格上,但如果你想查看