+ Reply to Thread
Results 1 to 4 of 4

Number from macro to cell

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Bergen, Norway
    MS-Off Ver
    MS 365 Subscription
    Posts
    369

    Number from macro to cell

    Hi,

    I have this code that saves and open a single sheet from exel workbook to a PDF file (report). When it get saved it automatic gives a name to the file such as TTS-1, and next time saved TTS-2.
    Is there anything I can do to the code that saves this filename in a cell in the worksheet as my reportnumber.

    This is the code that I use.
    Private Sub CommandButton2_Click()
    Static FileCount As Long
    Ark5.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\Oystein\Desktop\BTF\Hovedprosjekt\Exel prosjekt\Rapport\TTS-" & FileCount + 1 & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    FileCount = FileCount + 1
    End Sub
    Thanks in advance

    Oeysbrei

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Number from macro to cell

    This code will save the version number in cell A1 on sheet1. If you want it somewhere else, then just update the address
    Private Sub CommandButton2_Click()
        Static FileCount As Long
        sheets(1).range("A1").value="Ver " & FileCount
        Ark5.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\Oystein\Desktop\BTF\Hovedprosjekt\Exel prosjekt\Rapport\TTS-" & FileCount + 1 & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    FileCount = FileCount + 1
    End Sub

  3. #3
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Bergen, Norway
    MS-Off Ver
    MS 365 Subscription
    Posts
    369

    Re: Number from macro to cell

    Thanks Mallycat,

    I had to do some changes so that if some reports were deleted from the folder it would still be the the same reportnumber as filename.
    This is the code after modified.

    Private Sub CommandButton2_Click()
    Static FileCount As Long
    Ark5.Range("H1").Value = "TTS- " & FileCount + 1
    Ark5.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\Oystein\Desktop\BTF\Hovedprosjekt\Exel prosjekt\Rapport\TTS-" & FileCount + 1 & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    FileCount = FileCount + 1
    End Sub
    Is there a way I can place the value right side in the header?

    Regards
    Oeysbrei

  4. #4
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Number from macro to cell

    I taught myself to program in VBA. The trick is to turn on the Macro Recorder, do what you want to do, then analyse the code. When I just did this (added 'test') to the right header, I came up with this code

    ActiveSheet.PageSetup.RightHeader = "test" (simplified from the recorder).

    Based on this learning, try this

    Private Sub CommandButton2_Click()
    Static FileCount As Long
    ActiveSheet.PageSetup.RightHeader = "TTS- " & FileCount + 1
    Ark5.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\Oystein\Desktop\BTF\Hovedprosjekt\Exel prosjekt\Rapport\TTS-" & FileCount + 1 & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    FileCount = FileCount + 1
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1