简体   繁体   English

如何在 VBA Excel 中调用另一个模块

[英]How to Call Another Module in VBA Excel

I Have an Issue to call Another Module (Run Time Error'424': Object Required).我在调用另一个模块时遇到问题(运行时错误“424”:需要 Object)。 I have 2 Modules, Module 1 and Module 2. And Below is the code in Module 1:我有 2 个模块,模块 1 和模块 2。下面是模块 1 中的代码:

Private Sub test()

 Dim Work As Worksheet: Set work= Sheets("S_BDN")

 For i = 1 To 2

 Set f = work.Range("A5", work.Range("A5").End(xlDown))
 Set a = f.Find(i, LookIn:=xlValues)
        

    If a.Offset(0, 10).Value = "January" Then
        Call Module3.Proceed_B
    End If

 Next i
End Sub

And Below is the code in Module 2:以下是模块 2 中的代码:

sub Module3.Proceed_B()

 If a.Offset(0, 6).Value = "A" Then
      Debug.Print a.Offset(0, 4).Value
 else
      Debug.Print a.Offset(0, 5).Value
 end if

end sub

All help is greatly appreciated.非常感谢所有帮助。 Thank you.谢谢你。

I can't see your "fixed" code or your sheet, so here are some generic suggestions for improvement:我看不到您的“固定”代码或工作表,因此这里有一些通用的改进建议:

Private Sub test()

   Dim Work As Worksheet, f As Range, a As Range
   Set work= Sheets("S_BDN")
   
   Set f = work.Range("A5", work.Range("A5").End(xlDown)) 'take out of loop

   For i = 1 To 2
 
     Set a = Nothing
     Set a = f.Find(i, LookIn:=xlValues, lookat:=xlWhole) 'provide *all* relevant parameters
     If Not a Is Nothing Then                      'make sure you got a match   
         If a.Offset(0, 10).Value = "January" Then
             Proceed_B a   'pass a to the other sub
         End If
     End If

 Next i
End Sub

sub Proceed_B(a As Range)
   If a.Offset(0, 6).Value = "A" Then
      Debug.Print a.Offset(0, 4).Value
   else
      Debug.Print a.Offset(0, 5).Value
   end if
end sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM