Vba 使用工作表似乎不会更改不同工作表上的单元格值
我在模块中编写了宏。我还使用userform收集用户输入,并在同一工作簿的两个不同工作表上填充某些单元格 在模块中的一个子模块中,我声明:Vba 使用工作表似乎不会更改不同工作表上的单元格值,vba,excel,worksheet,with-statement,Vba,Excel,Worksheet,With Statement,我在模块中编写了宏。我还使用userform收集用户输入,并在同一工作簿的两个不同工作表上填充某些单元格 在模块中的一个子模块中,我声明: Dim wsAssemblyBOM As Worksheet Set wsAssemblyBOM = Worksheets("Assembly BOM") Dim wsDocuments As Worksheet Set wsDocuments = Worksheets("Documents") HeaderInfoUserForm.Show 这将打开
Dim wsAssemblyBOM As Worksheet
Set wsAssemblyBOM = Worksheets("Assembly BOM")
Dim wsDocuments As Worksheet
Set wsDocuments = Worksheets("Documents")
HeaderInfoUserForm.Show
这将打开我的用户窗体,在我们使用“确定”按钮功能之前,一切似乎都按计划进行:
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
这将仅填充activesheet中的单元格。我在这里有点困惑,因为网络中的所有例子都表明这应该是可行的。我还试图在“单元格”前面加上点,但它只会给出错误
我做错了什么
我也不知道什么时候该用点开始。例如:
With wsDocuments
.Cells(2, 3) = Author.Value
.Cells(2, 5) = Title.Value
.Cells(2, 7) = SubCode.Value
.Cells(2, 6) = DateText.Value
End With
这将给出一个错误“需要对象”。单元格是工作表的一个对象,但这是一个错误,因为我试图为单元格赋值。哪个单元格实际上不是单元格内容的位置?如果没有dot,它指的是单元格的实际内容?我只是在猜
很多时候,dot在类似的地方使用。我不清楚这个逻辑。
工作代码示例:
With FormatRange.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
你写道
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 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
请注意将wsAssemblyBOM父引用传递到每个.Cells的前缀
。假设wsAssemblyBOM和wsDocuments已声明并设置为工作表,并且它们可用于“确定”按钮\u单击“专用”子项,则应该没有问题
你的最后一个例子
With FormatRange.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
。。。就是说,
FormatRange.Borders(xlEdgeTop).LineStyle = xlContinuous
FormatRange.Borders(xlEdgeTop).ColorIndex = xlAutomatic
FormatRange.Borders(xlEdgeTop).TintAndShade = 0
FormatRange.Borders(xlEdgeTop).Weight = xlMedium
详细方法也比FormatRange慢。边框(xlEdgeTop)必须解析四次
放在每个代码表的顶部。你写的
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 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
请注意将wsAssemblyBOM父引用传递到每个.Cells的前缀
。假设wsAssemblyBOM和wsDocuments已声明并设置为工作表,并且它们可用于“确定”按钮\u单击“专用”子项,则应该没有问题
你的最后一个例子
With FormatRange.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
。。。就是说,
FormatRange.Borders(xlEdgeTop).LineStyle = xlContinuous
FormatRange.Borders(xlEdgeTop).ColorIndex = xlAutomatic
FormatRange.Borders(xlEdgeTop).TintAndShade = 0
FormatRange.Borders(xlEdgeTop).Weight = xlMedium
详细方法也比FormatRange慢。边框(xlEdgeTop)必须解析四次
在每个代码表的顶部。在您的问题中,您在我声明的模块中的一个子模块中说:然后为您的工作表声明两个变量:
Dim wsAssemblyBOM As Worksheet
Set wsAssemblyBOM = Worksheets("Assembly BOM")
Dim wsDocuments As Worksheet
Set wsDocuments = Worksheets("Documents"
这些变量仅在该子模块中可用。如果要在多个子模块上使用它们(如Private sub OK\u Button\u Click()
),则必须在模块中将变量定义为Public
因此,在您的模块顶部,您必须具备:
Option Explicit
Public wsAssemblyBOM As Worksheet
Public wsDocuments As Worksheet
大概是这样的:
现在这两个变量都可以在项目中的任何子项中使用,因此代码应该可以工作。确保将它们分配到正确的工作表。一旦设置了它们,就不需要再次执行,除非使用set MyVariable=Nothing
清空它们
因此,您的代码应该类似于:
Private Sub OK_Button_Click()
Set wsAssemblyBOM = Worksheets("Assembly BOM")
Set wsDocuments = Worksheets("Documents")
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
Set wsAssemblyBOM = Nothing
Set wsDocuments = Nothing
End Sub
根据您的需要调整它在您的问题中,您在我声明的模块中的一个子模块中说:然后为您的工作表声明两个变量:
Dim wsAssemblyBOM As Worksheet
Set wsAssemblyBOM = Worksheets("Assembly BOM")
Dim wsDocuments As Worksheet
Set wsDocuments = Worksheets("Documents"
这些变量仅在该子模块中可用。如果要在多个子模块上使用它们(如Private sub OK\u Button\u Click()
),则必须在模块中将变量定义为Public
因此,在您的模块顶部,您必须具备:
Option Explicit
Public wsAssemblyBOM As Worksheet
Public wsDocuments As Worksheet
大概是这样的:
现在这两个变量都可以在项目中的任何子项中使用,因此代码应该可以工作。确保将它们分配到正确的工作表。一旦设置了它们,就不需要再次执行,除非使用set MyVariable=Nothing
清空它们
因此,您的代码应该类似于:
Private Sub OK_Button_Click()
Set wsAssemblyBOM = Worksheets("Assembly BOM")
Set wsDocuments = Worksheets("Documents")
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
Set wsAssemblyBOM = Nothing
Set wsDocuments = Nothing
End Sub
根据您的需要调整它关于
使用
,它执行一系列重复引用单个对象或结构的语句,以便这些语句可以使用简化的语法。检查。关于使用的,它执行一系列重复引用单个对象或结构的语句,以便这些语句可以使用简化的语法。检查。也许你也应该检查一下:也许你也应该检查一下: