请求帮助!在下面的代码部分(工作得很好btw),我一直在实际的文件名和工作表名称中获得.txt。我一直在寻找解决这个问题需要解决的问题,但我无法解决这个问题。我对VBA很新,所以我尽我所能请帮忙?!
'Opens the folder to location to select txt file
fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.OpenText fileName:=fileToOpen, _
DataType:=xlDelimited, Tab:=True
End If
'Creates the file name based on txt file name
fileName = Mid(fileToOpen, InStrRev(fileToOpen, "\") + 1)
'Creates the sheet name based on the active txt file
sheetName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
'Rename the original text file
ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment
Limited\" & "DNU_" & fileName & ".txt")
答案 0 :(得分:1)
也许你也可以使用你的工作表名作为文件名,因为你已经从工作表名中删除了“.txt”。
'Opens the folder to location to select txt file
fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.OpenText fileName:=fileToOpen, _
DataType:=xlDelimited, Tab:=True
End If
'Creates the sheet name based on the active txt file
sheetName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
'Rename the original text file
ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment
Limited\" & "DNU_" & sheetName & ".txt")
答案 1 :(得分:0)
您可以使用正则表达式并提取不带扩展名的文件名并使用它
public String extractFileName(String fullPathFile){
try {
Pattern regex = Pattern.compile(EXTRACT_FILE_NAME_REGEX);
Matcher regexMatcher = regex.matcher(fullPathFile);
if (regexMatcher.find()){
return regexMatcher.group(1);
}
} catch (PatternSyntaxException ex) {
LOG.info("extractFileName::pattern problem <"+fullPathFile+">",ex);
}
return fullPathFile;
}