是否有更好的方法在初始化活动行时用来自活动行的数据填充用户表单组合框?Excel 2013



下面的代码在Excel 2010中工作得很好,但由于某种原因,在Excel 2013中几乎无法正常工作。

关键是,当双击单元格时(这里没有代码,在2010年和2013年都可以正常工作),将弹出一个用户表单,其中包含"活动行"的数据。当我在excel 2013中这样做时,用户表单会弹出,但它要么有来自不同行的数据,要么所有的组合框都是空白的。没有调试问题或通知。

以下是2010年有效而2013年无效的方法:

Private Sub UserForm_Initialize()
Call UnProtect
Dim r As Integer
r = ActiveCell.Row
StatusBox.Clear
With StatusBox                    
    .AddItem "New"
    .AddItem "In Process"
    .AddItem "Waiting on Material/Parts"
    .AddItem "Re-Assigned"
    .AddItem "Complete"
End With

'When the userform pops up, it automatically has the
'data from the row that has been double clicked
LocationValue.Value = Open_Orders.Cells(r, 2).Value
AssetValue.Value = Open_Orders.Cells(r, 3).Value
DescriptionValue.Value = Open_Orders.Cells(r, 10).Value
CommentBox.Value = Open_Orders.Cells(r, 11).Value
StatusBox.Value = Open_Orders.Cells(r, 8).Value
'LocationValue, AssetValue, DescriptionValue, CommentBox, and StatusBox are all
'either a combobox or textbox 

Call Protect
End Sub

我只使用vba很短的时间,所以我确信这是完全错误的方式去做。有没有更好的办法?

编辑:这是在名为Open_Orders的表中放置的代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Sheets("Open_Orders").Protect Password:="password", userinterfaceonly:=True
Dim TheRow As Integer
TheRow = LastRow(ActiveSheet.Range("A1:K1"))
        '^Function found in "LastRowFunction" module
        'Determines the last row that contains data in it
Set rng = Range("B2:K" & TheRow)
'If you click a cell with data in it then show the userform
If Not Intersect(Target, rng) Is Nothing Then Load UserComment
UserComment.Show
Sheets("Open_Orders").Activate
End Sub

UserComment是弹出的用户表单,应该包含来自活动行的数据。

当你写:

Call UnProtect
Dim r As Integer
r = ActiveCell.Row
StatusBox.Clear

你的UnProtect宏可能会返回一行不是你想要的,当你写:

If Not Intersect(Target, rng) Is Nothing Then Load UserComment
UserComment.Show
Sheets("Open_Orders").Activate

我也发现这些行不太清楚,因为UserComment.Show无论如何都会工作,因为你没有指定和End If在那里。我会写:

If Not Intersect(Target, rng) Is Nothing Then 
    UserComment.Show
    Sheets("Open_Orders").Activate
End If

最新更新