+ Reply to Thread
Results 1 to 10 of 10

Error with Macro and referencing another sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-22-2008
    Location
    Birmingham
    Posts
    46

    Error with Macro and referencing another sheet

    I have a module that runs an update. I would like for it to run some code on another sheet then jump back to the module.
    I keep getting a Compile error. Sub or Function not defined. I must be using the wrong Call on the TargetUpdate

    Sub Update()
    Application.OnTime Now + TimeValue("00:02:00"), "my_macro"
    
    End Sub
    Sub my_macro()
    
    For Each objconnection In ThisWorkbook.Connections
        ThisWorkbook.RefreshAll
    Next
    
    Call TargetUpdate
    Call Update
    
    End Sub
    Here is what I am trying to get ran from the TargetUpdate on sheet1
    Private Sub TargetUpdate(ByVal Target As Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
    Set xRg = Intersect(Range("F2"), Target)
    If xRg Is Nothing Then Exit Sub
    If IsNumeric(Target.Value) And Target.Value > Range("E2").Value Then
    Call Mail_small_Text_Outlook
    End If
    End Sub
    Sub Mail_small_Text_Outlook()
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "" & vbNewLine & vbNewLine & _
    "" & vbNewLine & _
    ""
    On Error Resume Next
    With xOutMail
    .To = "[email protected]"
    .CC = ""
    .BCC = ""
    .Subject = Range("F2").Value & " / " & Range("A2").Value
    .Body = xMailBody
    .Display 'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
    End Sub
    Last edited by anoble1; 07-08-2021 at 11:23 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,869

    Re: Error with Macro and referencing another sheet

    Make TargetUpdate Public (it is Private).

    Also when you call it you must provide the argument value for Target.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    07-22-2008
    Location
    Birmingham
    Posts
    46

    Re: Error with Macro and referencing another sheet

    I made the change to public. What do you mean by "provide the argument value for Target"?
    I am now getting this after I run. Expected Sub, Function, or Property

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,962

    Re: Error with Macro and referencing another sheet

    Put TargetUpdate in a standard module, or try Call Sheet1.TargetUpdate. I'd go with the first option. And you need to pass the Target range to the called subroutines.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Registered User
    Join Date
    07-22-2008
    Location
    Birmingham
    Posts
    46

    Re: Error with Macro and referencing another sheet

    Quote Originally Posted by TMS View Post
    Put TargetUpdate in a standard module, or try Call Sheet1.TargetUpdate. I'd go with the first option. And you need to pass the Target range to the called subroutines.
    I changed it to this: Still getting an error Argument not optional.
    Sub Update()
    Application.OnTime Now + TimeValue("00:02:00"), "my_macro"
    
    End Sub
    Sub my_macro()
    
    For Each objconnection In ThisWorkbook.Connections
        ThisWorkbook.RefreshAll
    Next
    
    Call Sheet1.TargetUpdate
    Call Update
    
    End Sub
    Public Sub TargetUpdate(ByVal Target As Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
    Set xRg = Intersect(Range("F2"), Target)
    If xRg Is Nothing Then Exit Sub
    If IsNumeric(Target.Value) And Target.Value > Range("E2").Value Then
    Call Mail_small_Text_Outlook
    End If
    End Sub
    Sub Mail_small_Text_Outlook()
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "" & vbNewLine & vbNewLine & _
    "" & vbNewLine & _
    ""
    On Error Resume Next
    With xOutMail
    .To = "[email protected]"
    .CC = ""
    .BCC = ""
    .Subject = Range("F2").Value & " / " & Range("A2").Value
    .Body = xMailBody
    .Display 'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
    End Sub

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,565

    Re: Error with Macro and referencing another sheet

    my_macro should also be without double quotation marks.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,869

    Re: Error with Macro and referencing another sheet

    Quote Originally Posted by jolivanes View Post
    my_macro should also be without double quotation marks.
    For a call to OnTime, the name of the macro to be called must be in double quotation marks.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,962

    Re: Error with Macro and referencing another sheet

    Still getting an error Argument not optional
    Yes, because you're not passing the Target variable when you call the subroutine. Looks like it can find it now because it wants the parameter you promised it.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,869

    Re: Error with Macro and referencing another sheet

    Call Sheet1.TargetUpdate
    TargetUpdate requires a single argument, which is a Range. You are not passing any argument. Your call should look something like

    Sheet1.TargetUpdate Range("F2")
    But CAUTION you have not explained what you want your code to do so I actually have no idea what should really be there.

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,565

    Re: Error with Macro and referencing another sheet

    Re Post #8. (6StringJazzer)
    I was not aware of that at all. Thanks for clearing that up for me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 05-09-2016, 12:41 PM
  2. Compile error referencing checkbox in another sheet
    By BacktoCode in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-12-2016, 11:28 AM
  3. [SOLVED] Referencing another sheet in a macro
    By Louie25 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-24-2015, 08:41 AM
  4. Error referencing cell in another sheet
    By Pete291 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 01-26-2015, 01:19 PM
  5. [SOLVED] Need Help Referencing Sheet Name in a Macro
    By Curtis Campbell in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2014, 01:05 PM
  6. [SOLVED] Help with renaming a sheet and referencing that sheet in a macro
    By cober123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-30-2012, 01:18 AM
  7. Replies: 4
    Last Post: 09-23-2011, 03:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1