vba - 如何在 Excel VBA 中将值从一个子传递到另一个子

标签 vba excel

在我的宏中,我有一个子例程(使用 for 循环)遍历表的行并根据 S 列中的内容在 V 列中写入注释。在此循环期间,它还计算它说“新"在 S 列中。现在我想将此值向上传递到主宏并向下传递到另一个子例程。我该怎么做,或者我的方法是错误的?

如果我理解正确,通常不可能从 excel VBA 中的 subs 中返回值,但您可以从函数中返回值。但是我认为函数在这里不适合(我可能误解了函数在 VBA 中的工作方式和/或没有充分发挥它们的潜力!)。

那么如何将变量/值从 sub 中偷运到另一个中呢?是唯一的选择GlobalPublic声明?

这是我的代码的一个非常粗略的示例:

Sub MainMacro ()
  Call CommentSub
  Call NumberOfRowsToCopy
End sub

Sub CommentSub
  Dim Counter As Integer
  For Counter = 1 to 500
    If Cells(Counter, "S") = "New" Then
      NewOrderLineCounter = NewOrderLineCounter + 1
      Cells(Counter, "V").Select
      ActiveCell.FormulaR1C1 = "New Line"
    End If
  Next Counter
End sub

Sub NumberOfRowsToCopy
  ActiveSheet.Range("$A$12:$T$1001").AutoFilter Field:=16, Criteria1:= _
    "New"
  ActiveSheet.Range("B15:N" & NewOrderLineCounter).SpecialCells(xlCellTypeVisible).Select
End sub

(顺便说一句,我知道可能有“更好”的方法来获取需要在此处复制的行数(从而消除了在 subs 之间传递值的需要)但我想我已经尝试了所有这些但没有一个工作.我认为它是excel表的格式,但这是另一个问题,你必须使用你所得到的,对吧?)

最佳答案

我不确定你为什么觉得一个函数不适合这里。

它们和 sub 一样,只是有一个返回值。

Private Sub MainMacro()
    Dim lReturn As Long

    'Get the return from the CommentSub
    lReturn = CommentSub

    'Pass that to the nextsub
    NumberOfRowsToCopy (lReturn)
End Sub

Function CommentSub() As Long 'Declare the return type after the function
  Dim NewOrderLineCounter As Long
  Dim Counter As Integer
  For Counter = 1 To 500
    If Cells(Counter, "S") = "New" Then
      NewOrderLineCounter = NewOrderLineCounter + 1
      Cells(Counter, "V").Select
      ActiveCell.FormulaR1C1 = "New Line"
    End If
  Next Counter

  'Here you set the return value of the funtion
  CommentSub = NewOrderLineCounter
End Function

Sub NumberOfRowsToCopy(lCount As Long) 'Declare the variable being passed to the sub.
Dim NewOrderLineCounter As Long
NewOrderLineCounter = lCount
  ActiveSheet.Range("$A$12:$T$1001").AutoFilter Field:=16, Criteria1:= _
    "New"
  ActiveSheet.Range("B15:N" & NewOrderLineCounter).SpecialCells(xlCellTypeVisible).Select
End Sub

或者,如果您想使用公共(public)变量路由,请在表单或模块代码的顶部声明它们。最重要的是功能和潜艇。
'Declared like this it can be accessed by any sub or function in this module or form.
Private NewOrderLineCounter as Long

'Declared like this it can be accessed by any sub or function in this module or form and from others. Although I think if it is in a form it will not be accessible from modules.  For that you can create a module called globals and declare it there as public.
Public NewOrderLineCounter as Long

Sub MainMacro ()
  Call CommentSub
  Call NumberOfRowsToCopy
End sub

Sub CommentSub
  Dim Counter As Integer
  For Counter = 1 to 500
    If Cells(Counter, "S") = "New" Then
      NewOrderLineCounter = NewOrderLineCounter + 1
      Cells(Counter, "V").Select
      ActiveCell.FormulaR1C1 = "New Line"
    End If
  Next Counter
End sub

Sub NumberOfRowsToCopy
  ActiveSheet.Range("$A$12:$T$1001").AutoFilter Field:=16, Criteria1:= _
    "New"
  ActiveSheet.Range("B15:N" & NewOrderLineCounter).SpecialCells(xlCellTypeVisible).Select
End sub

关于vba - 如何在 Excel VBA 中将值从一个子传递到另一个子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296386/

相关文章:

vba - 根据填充颜色索引删除行

excel - OLE 自动化并与另一个 VBE/VBA IDE 交互

vba - 创建范围排除中间列

vba - 在 VBA Excel 中使用数学方程输入值进行数学计算

excel - 如何修改此代码以通过 FileDialog 导入文本文件?

python - 如何计算xlsxwriter中Excel文档中的行数

ms-access - querydefs 运行时错误 3265 : item not found in this collection

如果目标单元格中​​的公式超过特定值,则显示消息框弹出窗口的 VBA 代码

VBA:刷新与 Nielsen 链接的 Excel 页面

excel - 如何向数组添加不同的范围?