Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Find and replace?

0

Hi,

I have gotten a lot of help with this workbook already and it's near completion thanks to you! 

I have a macro that makes a .pdf file, saves it in a specific folder and creates an email so that I can send it to the warehouse for packing. This is a list for buldingsite-elevators so there is always different addresses and heights that changes every time. 

There is one small, but important thing I totally forgot, and it's when the elevators are returned and not sent over to the next project. So for that purpose I have made a button (the big red) in "Pakkeliste".

When you click this button, the procedure will go as per usual. Make the list, save in folder and make an email. But there is one thing I want different. That is that the address in cell D9 is to be changed to "Tomta" (thats the name of our warehouse).

So that the next time I want to see what elevators are available I can go to the tab "Oversiktsliste" I can see all the units at "Tomta". I'll attach the workbook!

This is by the way for Module 7

Answer
Discuss

Discussion

I don't see any attached workbook. Did you mean to attach one?
don (rep: 1989) Mar 3, '22 at 8:30 am
Vergad- please attach a file to your question (as Don suggests). The last file I have is Pakkeliste Geda ny v0_a.xlsm which did not contain Module 7.
John_Ru (rep: 6142) Mar 3, '22 at 10:33 am
Should be there now!
LordBrenden (rep: 10) Mar 4, '22 at 2:07 am
Add to Discussion

Answers

0
Selected Answer

Vergad

In your Module 7 sub Email_Sheet_Click, a single line

ActiveSheet.Range("D9").Value = "Tomta"

would change the code to suit but I note that the code isn't specific to the named sheet (it uses ActiveSheet. commands) which could lead to problems if run with another sheet active. I've added a new variable oWs and changed the code to replace ActiveSheet with oWs (see changes on bold below)

Note too that I've renamed the macro (and assigned that your red "Returner..." button).

Sub Email_Returner_Click()


Dim objOutlook As Object
Dim objMail As Object
Dim signature As String
Dim oWB As Workbook
Dim PDF_Filename As String
Dim pakkeListe As Range
Set oWB = ActiveWorkbook
Dim SerNum As Range
' make macro specific to workbook/ sheet
Dim oWs As Worksheet
Set oWs = oWB.Worksheets("PakkeListe")

'change Ny adresse to Tomta
oWs.Range("D9").Value = "Tomta"

Set SerNum = Worksheets("Oversiktsliste").Range("B4:B100")

For Each Cell In SerNum

    If Cell.Value = oWs.Range("B10").Value Then
        ' ### set adresse first
        Cell.Offset(0, 2) = oWs.Range("D9") ' > addresse
        Cell.Offset(0, 8) = "Tidligere @ " & Cell.Offset(0, 2) & "; " & Cell.Offset(0, 8) ' + adresse>  Kommentar

    End If

    Next Cell

Set pakkeListe = oWs.Range("A4:B49")

pakkeListe.ExportAsFixedFormat Type:=xlTypePDF, Filename:="https:\\brendstil.sharepoint.com\Server\Stillas\Heis\Pakkelister\" & Range("D9") & "," & Range("A10") & "," & Range("B10")

PDF_File = "https:\\brendstil.sharepoint.com\Server\Stillas\Heis\Pakkelister\" & Range("D9") & "," & Range("A10") & "," & Range("B10") & ".pdf"

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

With objMail
    .Display
End With
    signature = objMail.HTMLbody
With objMail
    .To = oWs.Range("C80")
    .Cc = oWs.Range("C81")
    .Subject = "Pakkeliste heis"
    .HTMLbody = "<font face=" & Chr(34) & "Calibri" & Chr(34) & " size=" & Chr(34) & 4 & Chr(34) & ">" & "Hei," & "<br> <br>" & "Denne leveres tilbake til tomta. Tell over delene og sammenlign med forrige pakkeliste" & "<br> <br>" & signature & "</font>"
    .Attachments.Add PDF_File
    .Save
    .Display
End With

Set objOutlook = Nothing
Set objMail = Nothing

End Sub
then the macro will proceed to seek the serial number in worksheets "Oversiktsliste", create/save a pdf and email (if that's what you want). I've removed Module 7 sub FindSerial since the lines after the code extract above now correct "Oversiktsliste".

Incidentally I notice that you have a macro called Email_Sheet_Click in modules 1, 3, 4, 5, 6 and 7. Presumably they are iterations but it's not good practice to have them all the same, especially if you want to call on fronm a sheet macro say (VBA wouldn't know which one you meant!). You said you use only 5 and 7 so I've removed the others.

Hope this helps. 

Discuss

Discussion

Hi, thanks. It makes the list and email correct, but instead of getting "Tomta" in 'Oversiktsliste' column D i get FALSE which then shows as FALSE in 'Pakkeliste' A2. 

Regarding all the other Email_Sheet_Click I'm really just using 5 and 7. The others were just tests and modules made for practice. Can i just delete them without everything falling apart? 
LordBrenden (rep: 10) Mar 4, '22 at 6:44 am
Vergad. Please see my revised answer/file.

Not sure what happened but I suspect that you ran the macro with a sheet other than Pakkeliste active (so ActiveSheet. commands would operate on that sheet).I've made the macro specific so it always operates on that sheet (even if other workbooks are opened- you decalred/set oWB but then didn't use it).

I checked and (with the current address Artilleriveien 6, Bygg I) pressing the red button now puts Tomta in "PakkeListe" D9, "Oversiktsliste" D9 (and so  "PakkeListe" A2 via VLOOKUP) and adds it to Kommentar in "Oversiktsliste" J9 .

You'll see that I've deleted all modules other than 5 and 7.
John_Ru (rep: 6142) Mar 4, '22 at 8:59 am
Maybe it got 'confused' since I have all the other modules, but it seems to be working now. Thanks!
LordBrenden (rep: 10) Mar 7, '22 at 3:33 am
That's good. Thanks for selecting my answer. 
John_Ru (rep: 6142) Mar 7, '22 at 4:01 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login