【发布时间】:2019-01-04 09:32:06
【问题描述】:
我有写在模块中的宏。我还有用户表单来收集用户输入并在同一个工作簿的两个不同工作表上填充某些单元格。
在我声明的模块中的一个子项中:
Dim wsAssemblyBOM As Worksheet
Set wsAssemblyBOM = Worksheets("Assembly BOM")
Dim wsDocuments As Worksheet
Set wsDocuments = Worksheets("Documents")
HeaderInfoUserForm.Show
这将打开我的用户表单,一切似乎都按计划进行,直到我们进入 OK 按钮功能:
Private Sub OK_Button_Click()
With wsAssemblyBOM
Cells(2, 3) = Author.Value
Cells(2, 5) = Title.Value
Cells(2, 7) = SubCode.Value
Cells(2, 6) = DateText.Value
Version = BOMVersion.Value
End With
With wsDocuments
Cells(2, 3) = Author.Value
Cells(2, 5) = Title.Value
Cells(2, 7) = SubCode.Value
Cells(2, 6) = DateText.Value
End With
End Sub
这只会填充活动表中的单元格。我在这里有点困惑,因为网络中的所有示例都表明这应该有效。我也尝试在“单元格”前面添加点,但它只会给出错误。
我在这里做错了什么?
我也很困惑何时以点开头。例如:
With wsDocuments
.Cells(2, 3) = Author.Value
.Cells(2, 5) = Title.Value
.Cells(2, 7) = SubCode.Value
.Cells(2, 6) = DateText.Value
End With
这将给出错误“需要对象”。 Cells 是工作表的一个对象,但它是错误的,因为我试图为 .cells 赋值,而这实际上不是单元格内容的位置?没有点它是指单元格的实际内容?只是在这里猜测..
很多时候点在类似的地方使用。逻辑对我来说不是很清楚。 工作代码示例:
With FormatRange.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
【问题讨论】:
-
关于
With,它执行一系列重复引用单个对象或结构的语句,以便语句可以使用简化的语法。检查With...End With Statement (Visual Basic)。
标签: vba excel worksheet with-statement