英文:
VBA userform entry saves on wrong worksheet
问题
我遇到了关于数据条目保存位置的问题。
我有下面的代码用于在工作表 "PL2_steel" 上添加一些条目的按钮。问题是,当表单加载时,如果此刻激活的不是工作表 "PL2_steel" 而是其他工作表,它会将条目保存在活动工作表上(而不是 "PL2_steel")。
我想我已经正确声明了工作表。那么问题可能是什么呢?
英文:
I am having a problem regarding the location where my data entries get saved.
I have the code below for a button to add some entry on the worksheet "PL2_steel". The problem is that when the form loads, if instead of sheet "PL2_steel" another sheet is activated at that moment, it just saves the entry on the active sheet (and not on "PL2_steel").
Private Sub but_spl2addelement_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("PL2_steel")
Dim lr As Long
lr = sh.Cells(Rows.Count, 1).End(xlUp).End(xlUp).Row
Dim rng As Range
Set rng = Sheets("PL2_steel").ListObjects("Tbl_spl2elements").DataBodyRange
'''''''''''''''Validation'''''''''
If txt_SPL2code.Text = "" Then
MsgBox "Please assign a unique identifier code!", vbCritical
Exit Sub
End If
With sh
Cells(lr + 1, "A").value = Me.txt_SPL2code.value
Cells(lr + 1, "B").value = Me.Combo_spl2elements.value
Cells(lr + 1, "C").value = Me.txt_spl2gwp.value
Cells(lr + 1, "D").value = Me.Txt_spl2count.value
Cells(lr + 1, "E").value = Val(Me.Txt_spl2count.value) * Val(Me.txt_spl2gwp.value)
Cells(lr + 1, "R").value = Val(Me.Txt_spl2count.value) * Val(Me.txt_spl2gwp.value)
End With
Me.Combo_spl2elements = ""
Me.txt_spl2gwp.value = ""
Me.Txt_spl2count.value = ""
End Sub
I suppose I have declared the sheet properly. What could be the problem then?
答案1
得分: 2
Cells()
语句被处理为ActiveWorkbook.Cells()
- 要引用特定工作表的单元格,您使用ws.Cells()
(一般来说,访问对象的属性或方法的方式是对象.属性
或对象.方法
这在With
块中也是正确的。With语句的作用是:节省您反复输入引用的对象的时间。例如,如果您有以下代码:
obj.property
obj.property
obj.property
您可以使用With语句来避免每次都显式命名obj。所以您只需将语句包装在With块中,然后从每行中删除obj
。然后您会得到:
With obj
.property
.property
.property
End With
.
表示这是对象的属性/方法。如果不使用点,它将只是一个普通变量。
例如:
Dim name As String
name = "Julian"
With obj
.name = name
name = .surname
End With
Debug.Print obj.name '输出:"Julian"
Debug.Print name '输出:" " // 前提是obj.surname之前未设置为某个值
有关With语句的文档,请参见此处:With语句文档。
英文:
The statement Cells()
is handled as ActiveWorkbook.Cells()
- To reference the Cells of a certain Worksheet you use ws.Cells()
(In general the way you access properties or methods of an object is object.property
or object.method
This also is true in With
blocks. All the with does is: save you from having to type out the object you are referring to over and over again. So if you for example have the following code:
obj.property
obj.property
obj.property
you can use a with statement to avoid having to explicitly name obj every single time. - So you just wrap the statements into a with block and then remove obj
from each line. You are then left with:
With obj
.property
.property
.property
End With
The .
signifies that this is the property/method of an object. If you didn't use the dot it would just be a normal variable.
For example see:
Dim name As String
name = "Julian"
With obj
.name = name
name = .surname
End With
Debug.Print obj.name 'Output: "Julian"
Debug.Print name 'Output: "" // provided obj.surname was not previously set to a value
For documentation on the With statement see here: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/with-statement
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论