Hello Stackoverflow users,
I am trying to convert files from a non-microsoft file into an excel
I have managed with the following code:
Sub FastaToExcel()
'
' Macro
'
'
fName = Application.GetOpenFilename()
Workbooks.OpenText Filename:=fName, Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
ActiveWorkbook.SaveAs Filename:="NewName.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close SaveChanges:=True
End Sub
To get files converted and renamed. This works and a nice shiny NexName.xlsx rolls out.
However, in order to make the naming dynamic I tried the following code based on a post here (VBA Excel file to CSV, keeps CSV filename same as original workbook):
Sub Macro1()
NewName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
ActiveWorkbook.SaveAs Filename:="C:\Users\Username\Desktop\" & NewName & ".csv", , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close SaveChanges:=True
End Sub
However, this does not work as the "ActiveWorkbook.Name" pulls the name of .xlsm file that has the macro in it to convert the files, and unfortunately does not use the name of non-windows format file of whom I want to keep the name and have it used for the new .xlsx file.
Can anyone help me to tackle this problem? Much appreciated.
[edit] I also tried with the code suggested by @Jeeped (as asked by @skkakkar) in the same topic creating this, but this hangs at ActiveWorkbook.SaveAs line:
Sub FastaToExcel()
'
' Macro
'
'
fName = Application.GetOpenFilename()
Dim myPath As String, myFileName As String
myPath = "C:\Users\" & Environ("USERNAME") & "\Desktop\"
If Not CBool(Len(Dir(myPath, vbDirectory))) Then MkDir Path:=myPath
myFileName = Left(ActiveWorkbook.Name, InStr(1, ActiveWorkbook.Name, ".xl", vbTextCompare) - 1) & ".fasta"
Debug.Print myPath & Chr(92) & myFileName
Workbooks.OpenText Filename:=fName, Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
ActiveWorkbook.SaveAs Filename:=myPath & Chr(92) & myFileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close SaveChanges:=True
End Sub