Help to VBA email code

KasperSSI

New Member
Joined
Nov 28, 2019
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have made a VBA code that sends out an email with an attachment from Outlook.
I want the code to be applied to a button somewhere at the end of each row. The creating of the email and attachments are working perfectly.
However, my cell references are "fixed" rather than dynamic pr. row. How do I change this - I would rather not copy the code and make x amount of "fixed ranges" .
1656068459528.png


I hope my question is clear and help is much appreciated.

br,
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I would insert and name Form buttons, for example in column M, using a macro like this
VBA Code:
Private Sub Insert_Form_Command_Buttons()
    Dim myCell As Range, myRng As Range
    Dim CMD As Button
    
With ActiveSheet
    Set myRng = .Range("M2:M20")    '<~~~~~ enter the range to have buttons
End With

Application.ScreenUpdating = False
    For Each myCell In myRng.Cells
        With myCell
            Set CMD = .Parent.Buttons.Add _
                        (Top:=.Top, Left:=.Left, _
                         Width:=.Width, Height:=.Height)
            CMD.Name = "CMD_" & .Address(0, 0)
            CMD.Caption = "Send E-Mail"
            CMD.OnAction = "RunForAllButtons"    '<~~~~~ sub to call
        End With
    Next myCell
Application.ScreenUpdating = True

End Sub

then each button will call the same RunForAllButtons proceedure when clicked.
VBA Code:
Sub RunForAllButtons()
    Dim x As Variant, rw As Long, ID As String
    With ActiveSheet.Buttons(Application.Caller)
        'get the row
        x = Split(.TopLeftCell.Address, "$")
        rw = x(UBound(x))
    End With
    
    'your code would be something along the lines of
    ID = "Invoice_" & Range("G" & rw) & "_" & Range("F" & rw)
    MsgBox ID   'just to show resulting ID
    'etc
    
End Sub

Hope that helps
 
Upvote 0
I would insert and name Form buttons, for example in column M, using a macro like this
VBA Code:
Private Sub Insert_Form_Command_Buttons()
    Dim myCell As Range, myRng As Range
    Dim CMD As Button
   
With ActiveSheet
    Set myRng = .Range("M2:M20")    '<~~~~~ enter the range to have buttons
End With

Application.ScreenUpdating = False
    For Each myCell In myRng.Cells
        With myCell
            Set CMD = .Parent.Buttons.Add _
                        (Top:=.Top, Left:=.Left, _
                         Width:=.Width, Height:=.Height)
            CMD.Name = "CMD_" & .Address(0, 0)
            CMD.Caption = "Send E-Mail"
            CMD.OnAction = "RunForAllButtons"    '<~~~~~ sub to call
        End With
    Next myCell
Application.ScreenUpdating = True

End Sub

then each button will call the same RunForAllButtons proceedure when clicked.
VBA Code:
Sub RunForAllButtons()
    Dim x As Variant, rw As Long, ID As String
    With ActiveSheet.Buttons(Application.Caller)
        'get the row
        x = Split(.TopLeftCell.Address, "$")
        rw = x(UBound(x))
    End With
   
    'your code would be something along the lines of
    ID = "Invoice_" & Range("G" & rw) & "_" & Range("F" & rw)
    MsgBox ID   'just to show resulting ID
    'etc
   
End Sub

Hope that helps
Thanks for the inspiration. Got it to work with ActiveSheet.Buttons(Application.Caller).TopLeftCell.Row and some more, with my experience, simple code.
 
Upvote 0

Forum statistics

Threads
1,216,129
Messages
6,129,047
Members
449,482
Latest member
al mugheen

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