1

I use this script to open csv file and save them in another directory as xls file.

I also want to add one more feature - I want to free the top row so that when I scroll down it remain constant. How can I do that?

Dim app, fso, file, fName, wb, dir 

dir = "D:\TA\"

dirsave = "D:\TA\XLS"

Set app = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

For Each file In fso.GetFolder(dir).Files
    If LCase(fso.GetExtensionName(file)) = "csv" Then  
    fName = fso.GetBaseName(file)

    Set wb = app.Workbooks.Open(file) 
    app.Application.Visible = True
    app.Application.DisplayAlerts = False
    app.ActiveWorkbook.SaveAs dirsave & "\" & fName & ".xls", 56
    app.ActiveWorkbook.Close 
    app.Application.DisplayAlerts = True 
    app.Application.Quit 

    End if
Next
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
nnnnmmm
  • 233
  • 2
  • 6
  • 17

2 Answers2

1

Try this answer

Rows("1:1").Select
ActiveWindow.FreezePanes = True
Community
  • 1
  • 1
Stephen Lloyd
  • 733
  • 8
  • 21
1

Your current code can't be working for more than 1 file.

App.application is same as app in this case; just use app.

Visible and displayalerts can be done once, before the for loop.

Instead of app.activeworkbook, use the variable wb.

Just before save as, use the two lines provided by Stephen; prefix the first line with wb.worksheets(1) and second with "app.".

Consider saving as xlsx instead of xls.

Resetting displayalerts and app.quit should be after for loop.

Community
  • 1
  • 1
MikeC
  • 960
  • 1
  • 7
  • 15