Results 1 to 4 of 4

Help with the macro modification

Threaded View

  1. #1
    Registered User
    Join Date
    09-20-2011
    Location
    Swiss
    MS-Off Ver
    Excel 2003
    Posts
    22

    Help with the macro modification

    HI,

    We have a macro which is being used for converting the xls files to csv files., I need to change the output filenames of the generated .csv.. right now it is generating the filenames as tabname_timestamp for all the tabs present in the xls.

    But I am looking for the below naming convention..if my xls name is SEP012011_DollarU_load.xls which has tabs like Monthly , weekly, the macro should generate the files as

    SEP012011_DollarU_load.xls_Monthly
    SEP012011_DollarU_load.xls_weekly
    below is the piece of code which I tried to modify for the above outcome.., but i was not able to even compile the code

     tempName = myPath & Trim(.Name) & "_" _
                                              & Format(Time, "hhmmss") & ".csv"

    And below is the macro which we are using .Please tell me how to add the xls name in the tempname variable so that the macro will generate the file formats as mentioned above.

    Option Explicit
    Sub testme01()
    
        Application.ScreenUpdating = False
    
        Dim myFiles() As String
        Dim fCtr As Long
        Dim myFile As String
        Dim myPath As String
        Dim tempWkbk As Workbook
        Dim logWks As Worksheet
        Dim tempName As String
        Dim wks As Worksheet
        Dim oRow As Long
    
        'change to point at the folder to check
        myPath = "C:\DollarU"
        If Right(myPath, 1) <> "\" Then
            myPath = myPath & "\"
        End If
    
      
        'get the list of files
        fCtr = 0
        Do While myFile <> ""
            fCtr = fCtr + 1
            ReDim Preserve myFiles(1 To fCtr)
            myFiles(fCtr) = myFile
            myFile = Dir()
        Loop
    
        If fCtr > 0 Then
            oRow = 1
            For fCtr = LBound(myFiles) To UBound(myFiles)
                Set tempWkbk = Nothing
       
                If tempWkbk Is Nothing Then
                    logWks.Cells(oRow, "A").Value = "Error Opening: " _
                                                          & myFiles(fCtr)
                    oRow = oRow + 1
                Else
                    For Each wks In tempWkbk.Worksheets
                        With wks
                            If Application.CountA(.UsedRange) = 0 Then
                                'do nothing
                            Else
                                .Copy 'to a new workbook
                                tempName = myPath & Trim(.Name) & ".csv"
                                Do
                                    If Dir(tempName) = "" Then
                                        Exit Do
                                    Else
                                        tempName = myPath & Trim(.Name) & "_" _
                                              & Format(Time, "hhmmss") & ".csv"
                                    End If
                                Loop
                               
                            End If
                        End With
                    Next wks
                    tempWkbk.Close savechanges:=False
                End If
            Next fCtr
        End If
    
        With logWks.UsedRange
            .AutoFilter
            .Columns.AutoFit
        End With
    
        Application.ScreenUpdating = True
    
    End Sub
    Best REgards
    Last edited by Nithya1987; 09-21-2011 at 08:45 PM.

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