3 way use button to run macro

Aleytha

New Member
Joined
Apr 16, 2024
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi!
I'm a complete begginer, so i don't know much about coding.
I'm trying to create a button to run a macro that does the following:
1.- Save sheet2 in a specific location as PDF and saves the hyperlink in sheet24 of the active workbook in column 11
2.- Save a copy of some of the sheets of the active workbook in excel (macro enabled) in another specific location and save the hyperlink in sheet24 of the active workbook in column 12
3.- Send e-mail with PDF from sheet2 and put the date of when the mail was sent in sheet24

I managed to make work points 1 and 3 without problems, but with point 2 i'm not able to make it work, can anyone give me a heads up? Thanks

what i have that is working is this:

VBA Code:
Sub saveaspdf()

Dim nºpresupuesto As Long
Dim nºpedido As String
Dim cliente As String
Dim importe As Currency
Dim fecha_presupuesto As Date
Dim path As String
Dim fname As String
Dim siguientepresupuesto As Range

nºpresupuesto = Range("J5")
nºpedido = Range("J4")
cliente = Range("B4")
importe = Range("N68")
fecha_presupuesto = Range("O4")
fname = nºpresupuesto & " - " & cliente
path = "C:\Users\.....\"



Sheet2.ExportAsFixedFormat Type:=xlTypePDF, IgnorePrintAreas:=False, Filename:=path & fname

Set siguientepresupuesto = Sheet24.Range("A1048576").End(xlUp).Offset(1, 0)

siguientepresupuesto = nºpresupuesto
siguientepresupuesto_Offset(0, 2) = cliente
siguientepresupuesto_Offset(0, 3) = importe
siguientepresupuesto_Offset(0, 4) = fecha_presupuesto
siguientepresupuesto_Offset(0, 13) = Now

Sheet2.Hyperlinks.Add anchor:=siguientepresupuesto_Offset(0, 12), Address:=path & fname & ".pdf"



                                       
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail

.To = Range("B6")
.Subject = "Presupuesto nº " & nºpresupuesto
.Body = "Buenos días," & vbCrLf & "Nos complace enviarle, adjunto a este correo, en  un archivo en PDF, el presupuesto solicitado." & vbCrLf & "Cualquier consulta estamos en contacto." & vbCrLf & "Atentamente,"
.Attachments.Add (path & fname & ".pdf")
.Display

End With


Set OutApp = Nothing
Set OutMail = Nothing


what i'm still trying to put inside and i have it aside because i can't figure out how to make it work is this:

VBA Code:
Sub SavePedidoAsExcel()

Dim nºpresupuesto As Long
Dim nºpedido As String
Dim cliente As String
Dim importe As Currency
Dim fecha_presupuesto As Date
Dim path As String
Dim fname As String

nºpresupuesto = Range("J5")
nºpedido = Range("J4")
cliente = Range("B4")
importe = Range("N68")
fecha_presupuesto = Range("O4")
fname = nºpresupuesto & " - " & cliente
path = "C:\Users\.......\"

ActiveWorkbook.SaveCopyAs

With ActiveWorkbook
    .SaveAs Filename:=path & fname, FileFormat:=51
    .Close
End With


With the following part i put it also on the sheet i want but creates another row, but i want to include it in the same row of the pdf, allthough different column and also i understand that i'm repeating some info that i already included before, but right now i have it in 2 separated macros.

VBA Code:
Set siguientepresupuesto = Sheet24.Range("A1048576").End(xlUp).Offset(1, 0)

siguientepresupuesto = nºpresupuesto
siguientepresupuesto_Offset(0, 2) = cliente
siguientepresupuesto_Offset(0, 3) = importe
siguientepresupuesto_Offset(0, 4) = fecha_presupuesto


Sheet2.Hyperlinks.Add anchor:=siguientepresupuesto_Offset(0, 11), Address:=path & fname & ".pdf"

I understand that if i want some concrete sheets i should do an array like:
sheets(array(sheet1,sheet2,sheet3,...)).copy -> but gives me error

If anyone can help i would appreciate it, thanks for your time and patience
 
Last edited by a moderator:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
OK, I seem to have solved the issue going around the web.

The problem I'm facing now is that I get 2 lines every time instead of only 1 in my control sheet, how can I solve that?

1300​
xxxx
10.044,79 €​
16/04/2024​
C:\Users\.....xlsxC:\Users\.....pdf
16/04/2024 15:14​
1300​
xxxx
10.044,79 €​
16/04/2024​

So i don't want the last line to appear as i don't need it

What I have until now is this (sorry, I don't know how to upload a mini-sheet from the code)


VBA Code:
Sub SaveActiveworkbookAsExcel()

Dim nºpresupuesto As Long
Dim nºpedido As String
Dim cliente As String
Dim importe As Currency
Dim fecha_presupuesto As Date
Dim path As String
Dim fname As String
Dim siguientepresupuesto As Range


nºpresupuesto = Range("J5")
nºpedido = Range("J4")
cliente = Range("B4")
importe = Range("N68")
fecha_presupuesto = Range("O4")
fname = nºpresupuesto & " - " & cliente
path = "C:\Users\...\"

'Save copy the active workbook

Sheets(Array("ENTRADA DADES", "PRESSUPOST", "FULL COMANDA", "TALL ALUMINI", "ACCESORIS", "MUNTATGE POTES-LAMES", "MUNTATGE CANALS", "LEDS LAMES", "LED LAMAS (CONF. ESPECIAL)", "LED PERIMETRAL TIRA LED", "FOCOS LED PERIMETRAL", "PECES PER LACAR", "PERFILS PER LACAR", "ETIQUETES PERFILS", "COMPONENTS TIVANTI", "CONSUMO")).Copy

'Save the new Workbook to a specified folder

With ActiveWorkbook
    .SaveAs Filename:=path & fname, FileFormat:=51
    .Close
   
End With

'need to put the details of the workbook in the record of control

Set siguientepresupuesto = Sheet24.Range("A1048576").End(xlUp).Offset(1, 0)

siguientepresupuesto = nºpresupuesto
siguientepresupuesto_Offset(0, 2) = cliente
siguientepresupuesto_Offset(0, 3) = importe
siguientepresupuesto_Offset(0, 4) = fecha_presupuesto

Sheet24.Hyperlinks.Add anchor:=siguientepresupuesto_Offset(-1, 11), Address:=path & fname & ".xlsx"



End Sub



Sub saveaspdf()

Dim nºpresupuesto As Long
Dim nºpedido As String
Dim cliente As String
Dim importe As Currency
Dim fecha_presupuesto As Date
Dim path As String
Dim fname As String
Dim siguientepresupuesto As Range

nºpresupuesto = Range("J5")
nºpedido = Range("J4")
cliente = Range("B4")
importe = Range("N68")
fecha_presupuesto = Range("O4")
fname = nºpresupuesto & " - " & cliente
path = "C:\Users\...\"



Sheet2.ExportAsFixedFormat Type:=xlTypePDF, IgnorePrintAreas:=False, Filename:=path & fname


Set siguientepresupuesto = Sheet24.Range("A1048576").End(xlUp).Offset(1, 0)

siguientepresupuesto = nºpresupuesto
siguientepresupuesto_Offset(0, 2) = cliente
siguientepresupuesto_Offset(0, 3) = importe
siguientepresupuesto_Offset(0, 4) = fecha_presupuesto
siguientepresupuesto_Offset(0, 13) = Now

Sheet2.Hyperlinks.Add anchor:=siguientepresupuesto_Offset(0, 12), Address:=path & fname & ".pdf"



                                       
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail

.To = Range("B6")
.Subject = "Presupuesto nº " & nºpresupuesto
.Body = "Buenos días," & vbCrLf & "Nos complace enviarle, adjunto a este correo, en  un archivo en PDF, el presupuesto solicitado." & vbCrLf & "Cualquier consulta estamos en contacto." & vbCrLf & "Atentamente,"
.Attachments.Add (path & fname & ".pdf")
.Display

End With


Set OutApp = Nothing
Set OutMail = Nothing

SaveActiveworkbookAsExcel

End Sub
 
Last edited by a moderator:
Upvote 0
I solved it! needed to only arrange this part:

VBA Code:
'need to put the details of the workbook in the record of control

Set siguientepresupuesto = Sheet24.Range("A1048576").End(xlUp).Offset(1, 0)

siguientepresupuesto = nºpresupuesto
siguientepresupuesto_Offset(0, 2) = cliente
siguientepresupuesto_Offset(0, 3) = importe
siguientepresupuesto_Offset(0, 4) = fecha_presupuesto

Sheet24.Hyperlinks.Add anchor:=siguientepresupuesto_Offset(-1, 11), Address:=path & fname & ".xlsx"

End Sub

to:

VBA Code:
'need to put the details of the workbook in the record of control

Set siguientepresupuesto = Sheet24.Range("A1048576").End(xlUp).Offset(1, 0)

Sheet24.Hyperlinks.Add anchor:=siguientepresupuesto_Offset(-1, 11), Address:=path & fname & ".xlsx"

End Sub

and it works perfectly!

Anyway thanks a lot if someone was looking into it
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

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