我有一个用户窗体,其中有许多文本框和一个组合框,组合框的值来自Excel工作表。我想要的是如果单元格A,F,J,K为空,我希望它们通过文本框手动输入(如果可用),则应该在文本框中看到它们。请查找代码。如果文本框为空,则使用文本框为单元格添加值
Private Sub cbox5_Change()
Dim r As Long
Dim wks As Worksheet
Set wks = Worksheets("Sheet2")
r = cbox5.ListIndex + 2
On Error Resume Next
If wks.Cells(r, "R") = "" Then
txt8.Value = wks.Cells(r, "N")
Else
txt8.Value = wks.Cells(r, "R")
End If
txt10.Value = wks.Cells(r, "F")
txt11.Value = wks.Cells(r, "A")
txt16.Value = wks.Cells(r, "J")
txt17.Value = wks.Cells(r, "K")
If cbox5.Value = "" Then
Sheets("Sheet2").Select
Range("A65536").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1).Select
Me.txt8.Value = ""
Me.txt9.Value = ""
Me.txt10.Value = ""
Me.txt11.Value = ""
Me.txt15.Value = ""
Me.txt16.Value = ""
End If
End Sub
此外,我无法看到组合框中的值,直到userform被关闭并再次打开。任何人都可以让我知道什么可能会导致这个问题?我需要在哪里更改代码?
我无法获得此代码工作:
If wks.Cells(r, "D") = "" Then
wks.Cells(r, "D").Value = Me.txt9.Value
Else
txt9.Value = wks.Cells(r, "D")
End If
根据您的ComboBox是'cbox5'的假设,我没有看到这个代码中的任何地方,你正在分配任何东西。因此,没有理由改变组合框的值。 – FreeMan 2015-04-03 12:53:49
@Freeman这是cbox5的代码, 'With Worksheets(“Sheet2”) cbox5.List = .Range(“I2:I150”&.Range(“I”&.Rows.Count).End(xlUp) .Row).Value End With' – user2842252 2015-04-03 12:58:17
在评论中阅读代码非常困难。编辑您的原始帖子,将它放在适当的位置,并详细说明您可能收到的错误消息以及它们出现在哪一行。 – FreeMan 2015-04-03 13:00:11