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

Copy text to new cell when executing code VBA

0

Hi,

I already got help creating this code, but I need to make change. I don't fully understand how the code works, but I will try to explain. 

I have a list of lifts with unique serial numbers, these lifts are linked to their current location or address. 

When the lifts are moved I will type in the new address and I want to move it to, push my button which executing the code that will change the address in my list. 

The current code changes the address and it makes a 'previous address' in the Kommentar field. When I move the lift the 'previous' field would only get longer and longer. I would like it only to change the current address to the new one and not to make the previous 

The code I used goes like this: (Filename: Pakkeliste Gedatest4 modified v0)

Sub Email_Sheet_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

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

    For Each Cell In SerNum

        If Cell.Value = ActiveSheet.Range("B10").Value Then

            Cell.Offset(0, 8) = "Tidligere @ " & Cell.Offset(0, 2) & "; " & Cell.Offset(0, 8) ' + adresse>  Kommentar

            Cell.Offset(0, 2) = ActiveSheet.Range("D9") ' > addresse

        End If

        Next Cell

    Set pakkeListe = Range("A4:B49")

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

    PDF_File = "https:\\brendstil.sharepoint.com\Server\Stillas\Maler\Pakkelister\Gedapakkelister\" & 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 = ActiveSheet.Range("C80")

        .Cc = ActiveSheet.Range("C80")

        .Subject = "Pakkeliste heis"

        .HTMLbody = "<font face=" & Chr(34) & "Calibri" & Chr(34) & " size=" & Chr(34) & 4 & Chr(34) & ">" & "Hei," & "<br> <br>" & "Fancy shcmanzy liste" & "<br> <br>" & signature & "</font>"

        .Attachments.Add PDF_File

        .Save

        .Display

    End With

    Set objOutlook = Nothing

    Set objMail = Nothing

End Sub

Sub FindSerial()

Dim SerNum As Range

End Sub

______________________________________

I have changed the code a bit because i'm now saving the PDF file to 2 separate folders. (filename: Pakkeliste Geda ny, Module5)So this is the current code. I would like to implement the code above in the bold text, but without the comment section-autofill. The kommentar fields will be manually filled. 

Answer
Discuss

Answers

0
Selected Answer

Brenden

I think you want the bold code above to be modified to work with the new sheet Oversiktsliste and not to add the comment.

The attached file does that (via Module 5). The key bits of the changed code are in bold below:

Dim SerNum As Range

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

    For Each Cell In SerNum

        If Cell.Value = ActiveSheet.Range("B10").Value Then
            Cell.Offset(0, 2) = "Tidligere @ " & Cell.Offset(0, 1) ' Tilføj kun sidste adresse til kommentar
            Cell.Offset(0, 1) = ActiveSheet.Range("D9") ' Pakkeliste D9 > Nåværende adresse
        End If

    Next Cell
The Set SerNum line now refers to the new sheet (rather than Alleheiserriktig) and the Cell.Offset lines refer to the correct offsets (from the cell where the serial number is found).

I've kept the Kommnetar line in but modified it so it overwrites that cell, i.e. only puts in ONLY one "previous" address. If you don't want that and prefer to do that comment manually, just delete the line starting Cell.Offset(0, 2).

The code works by finding the cell in Oversiktsliste which holds the serial number from Pakkeliste then writes D9 to Cell.Offset(0,2) - that means the cell 0 rows down and 1 column to the right (i.e. your column Nåværende adresse).

Hope this is what you want and that you now follow that bit of the code.

Discuss

Discussion

Hi,
Thanks! That was exactly what i meant. The @ previous address would be nice to have, but I can also customize it. 
Again, thank you very much for the help and a quick response. I'm showing this to the manager and the project leaders tomorrow! :)
LordBrenden (rep: 10) Feb 2, '21 at 4:14 am
Brenden

Happy to help, especially since I have fond memories of visiting Copenhagen and Aarhus many years ago (the last time I was unemployed!).

Thanks for selecting my Answer- good luck with your meeting tomorrow.
John_Ru (rep: 6142) Feb 2, '21 at 4:26 am
Add to Discussion


Answer the Question

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