以下代码在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是弹出的用户表单,应该包含活动行中的数据。
答案 0 :(得分:0)
当你写:
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