来自用户表单的复选框数据



我在将复选框信息从我创建的 excel 用户表单返回到 excel 中的数据库时遇到问题。 它一直将活动工作表而不是工作表 2 放在我的 excel 工作簿中。

Sub CheckBox2_Click()
Dim iRow As Long
iRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
If CheckBox2 Then
Range("G" & iRow) = "Received"
Else
Range("G" & iRow).ClearContents
End If
End Sub

我想我需要设置以下内容才能使其工作,但它没有帮助。

iRow = Application.Workbooks("PIDParcelUtilities.xlsm").Worksheets("PIDParcelUtilitiesData").Range("A" & Rows.Count).End(xlUp).Offset(1).Row

它不断放置活动工作表

这是设计使然。

iRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row

Range指的是哪个工作表?

Range("G" & iRow) = "Received"

类模块(UserForm是类)中的非限定Range调用隐式引用ActiveSheet

拉取您要处理的工作表对象引用:

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("PIDParcelUtilitiesData")

然后你可以使用 is 来限定这些Range调用:

iRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1).Row
If CheckBox2 Then
ws.Range("G" & iRow) = "Received"
Else
ws.Range("G" & iRow).ClearContents
End If

最新更新