【问题标题】:With Worksheets does not seem to change cell values on different sheets使用 Worksheets 似乎不会更改不同工作表上的单元格值
【发布时间】: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

【问题讨论】:

标签: vba excel worksheet with-statement


【解决方案1】:

在您的问题中,您说 在我声明的模块中的一个子项中: 然后为您的工作表声明 2 个变量:

Dim wsAssemblyBOM As Worksheet
Set wsAssemblyBOM = Worksheets("Assembly BOM")

Dim wsDocuments As Worksheet
Set wsDocuments = Worksheets("Documents"

这些变量仅在该子目录中可用。如果您想在多个 subs 上使用它们(例如 Private Sub OK_Button_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

根据您的需要调整它

【讨论】:

【解决方案2】:

你写的,

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 已被声明并设置为工作表,并且它们可用于 OK_Button_Click 私有子,那么您应该没有问题。

你的最后一个例子,

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.Borders(xlEdgeTop) 必须解析四次。

Option Explicit 放在每个代码表的顶部。

【讨论】:

    猜你喜欢
    • 2022-07-25
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多