Macro to save File Name using Text and Cell Reference

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,568
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below to email two sheets and to save the file containing two sheets and using a combination of text and cell reference containing the month and year


However when running the macro, I get a compile error "Subscript out of range and the code below is highlighted




Code:
  file_save_name = "Sales comms for Uploading pertaining to " & Sheets("Journals").Range("F1").Value & ".xlsx"


See my full code below

Code:
 Sub EmailSalesComms()

Dim Ztext As String
Dim Zsubject As String

Ztext = [bodytext1]                              'read in text from named cell
  Sheets(Array(""Summary Comms Payable", "Late Comms")).Copy

     With Range("A1:N150")
  .Value = .Value
End With
   
    Application.DisplayAlerts = False
   
    With ActiveWorkbook
        file_save_name = "Sales comms for Uploading pertaining to " & Sheets("Journals").Range("F1").Value & ".xlsx"
        .SaveAs Environ("TMP") & "\" & file_save_name
        .Close True
    End With
 

    Application.DisplayAlerts = True

   
    With CreateObject("Outlook.Application").CreateItem(0)
        .To = Join(Application.Transpose(Sheets("index").Range("F1:F4").Value), ";")
        .Subject = "Manager Comms for Uploading"
        .Body = Ztext
       
      .Attachments.Add Environ("TMP") & "\" & file_save_name
        .Display
        '.send
    End With
           
End Sub


it would be appreciated if someone could kindly amend my the portion of code above that is causing this problem
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
What exactly is in cell F1 on the Journals sheet?
You don't have an error in that cell, do you?

Can you run this line of code without an error?
VBA Code:
Sheets("Journals").Select
 
Upvote 0
Hi Joe


Thanks for the reply. I can run code below

Code:
 Sheets("Journals").Select

I have a month and year in cell F1 for e.g. Mar-2022
 
Upvote 0
OK, there is nothing wrong then with the code or the cell value.

Note that you will get that error if your workbook with the "Journals" sheet is NOT the active workbook at the time when that code is run.
Do you have multiple workbooks open?
 
Upvote 0
Solution
Thanks for the reply and your input . I closed all other workbooks and ran the code and it works now
 
Upvote 0

Forum statistics

Threads
1,215,701
Messages
6,126,290
Members
449,308
Latest member
VerifiedBleachersAttendee

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