+ Reply to Thread
Results 1 to 6 of 6

Macro to convert a specific

Hybrid View

  1. #1
    Registered User
    Join Date
    01-29-2014
    Location
    Geneva, switzerland
    MS-Off Ver
    Excel 2007
    Posts
    8

    Macro to convert a specific

    Hello everyone,

    I am looking at converting a specific sheet from multiple files in a pdf format. I have found a macro wich I have slightly modified to be able to select the input folder containing the original files and the output folder where the pdf files should go. Currently it is working fine but the whole worksheet in being printed to a pdf file. I would need to only print the spreadsheet named "Profil". The code is described here below.

    Is there any one who could help me in modifying the existing macro to only convert the spreadsheet "Profil" to pdf?

    In advance thank you very much

    
    Option Explicit
    Sub ExcelToPDF2()
    Dim FilesInPath As String
    Dim OutputPath2 As String
    Dim MyFiles() As String, Fnum As Long
    Dim Buk As Workbook, BukName As String
    Dim CalcMode As Long
    Dim sh As Worksheet
    Dim StartTime As Date, EndTime As Date
    Dim LPosition As Integer
    Dim SourcePATH As String
    Dim OuputPATH As String
    
    'Specify the path of a folder where all the excel files are stored
    
    StartTime = Timer
    
    ' select source path
    
    MsgBox "Select the source Folder"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            SourcePATH = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
    ' end select source path
    
    ' select output path
    
    MsgBox "Select the output Folder"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            OuputPATH = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
    
    
    FilesInPath = Dir(SourcePATH & "*.xl*")
    If FilesInPath = "" Then
    MsgBox "No files found"
    Exit Sub
    End If
    
    Fnum = 0
    Do While FilesInPath <> ""
    Fnum = Fnum + 1
    ReDim Preserve MyFiles(1 To Fnum)
    MyFiles(Fnum) = FilesInPath
    FilesInPath = Dir()
    Loop
    
    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
    End With
    
    If Fnum > 0 Then
    For Fnum = LBound(MyFiles) To UBound(MyFiles)
    Set Buk = Nothing
    On Error Resume Next
    Set Buk = Workbooks.Open(SourcePATH & MyFiles(Fnum))
    On Error GoTo 0
    
    If Not Buk Is Nothing Then
    
    
    LPosition = InStr(1, Buk.Name, ".") - 1
    BukName = Left(Buk.Name, LPosition)
    Buk.Activate
    
    OutputPath2 = OuputPATH & BukName & ".pdf"
    
    On Error Resume Next
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=OutputPath2, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False
    
        On Error GoTo 0
        
    End If
    
    Buk.Close SaveChanges:=False
    
    Next Fnum
    End If
    
    
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = CalcMode
    End With
    
    EndTime = Timer
    MsgBox "Task succesfully completed in " & Format(EndTime - StartTime, "0.00") & " seconds"
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to convert a specific

    if BukName ="Profil" then

    ....export code

    end if
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    01-29-2014
    Location
    Geneva, switzerland
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to convert a specific

    Hi Nathansav

    Sorry I have just tried but it does not seem to export the pdf files. Is it possible to define a range withing the "profil" sheet? I guess that your code identifies the file name and not the sheet within the worksheet. Or more probably I am totally out.

  4. #4
    Registered User
    Join Date
    01-29-2014
    Location
    Geneva, switzerland
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to convert a specific

    Hi Nathansav

    Sorry I am relatively new to VBA, could you show me on the macro where I should insert the code you specified?

    Thank you

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to convert a specific

    before your export code after setting BukName = Left(Buk.Name, LPosition)

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to convert a specific

    You said spreadsheet in your post, so thought that you meant a workbook called Profil. Which is what my code does.
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            "\\wlfp02\home\CS4\nsavidge\Book1.pdf", Quality:=xlQualityStandard, _
            IncludeDocProperties:=False, IgnorePrintAreas:=False, From:=1, To:=1, _        OpenAfterPublish:=false
    you will need to change these both to Sheets("Profil").index i think.
    Last edited by nathansav; 08-04-2014 at 10:48 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to copy specific data from one WB to specific cells in another WB based on specific
    By d_rose in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-26-2014, 11:05 AM
  2. Convert specific number into specific sentence
    By sarangapani in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-26-2013, 03:57 PM
  3. [SOLVED] Use Macro to convert specific row to CSV
    By homer09001 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-13-2013, 03:53 PM
  4. [SOLVED] Macro to convert specific text and format specific text within a string
    By Mencae in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-06-2012, 12:13 AM
  5. Convert numbers using specific key with VBA
    By missy22 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-23-2012, 01:12 PM

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