How to fix a macro so that it runs on the active sheet without giving it a name

Jay3

Board Regular
Joined
Jul 3, 2009
Messages
237
Hi All

In this macro when a task is complete I can select "Yes" from a drop down and it fires an email, but more importantly strikes through the task in the list.

I have defined the name of the worksheet which is causing issues as I am calling this module to replicate the same behaviours on a template which I am copying over and over.

Here's the code:

VBA Code:
Option Explicit

Sub Confirm_Task_Complete(ByVal aRow As Long)
    
    Dim OutApp As Object ' Outlook.Application
    Dim OutMail As Object ' MailItem
    Dim strbody As String
    Dim wsData As Worksheet
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    Set wsData = ThisWorkbook.Sheets("New Task Not Requiring Board")
  
 
    
    strbody = "<font face='Arial,Helvetica' color='Black'>Dear " & wsData.Range("b" & aRow) & "<br><br>" & _
              "Your request relating to " & "<font color = 'Black'>" & Format(wsData.Range("c" & aRow).Value, "dd mmmm yyyy") & _
              " as outlined below has been completed by " & "<font color = 'red'><b>" & Format(wsData.Range("I" & aRow).Value, ".") & _
              "<font color='black'></b><br><br>" & _
              "Requirment: " & "<font color ='black'><br><br>" & wsData.Range("D" & aRow).Value & "</b></font>" & "<br><br>"
 
    
    
    
    
    On Error Resume Next
    With OutMail
        .To = wsData.Range("K" & aRow)
        .CC = ""
        .BCC = ""
        .Subject = wsData.Range("B" & aRow)
        .HTMLBody = strbody
        .Display   'or use .Send
    End With
    On Error GoTo 0
    
    wsData.Range("B" & aRow & ":M" & aRow).Font.Strikethrough = True

    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub

This line is the problem as sometimes the sheet is called something else:

Set wsData = ThisWorkbook.Sheets("New Task Not Requiring Board")

How can I fix this?

Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
If it's the active sheet, use ThisWorkbook.Activesheet
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,656
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top