如何规范范围中列出的文件名

我在电子表格中以“Smith,J. 010112.pdf”的forms列出了文件名。 但是,它们采用了“010112.pdf”,“01.01.12.pdf”和“1.01.2012.pdf”的不同格式。 我怎样才能把这些改成“010112.pdf”格式?

就个人而言,我讨厌使用VBA工作表function将工作,所以我已经find了一种方法来做到这一点与工作表函数。 虽然你可以把这一切都塞进一个单元格中,但我已经把它分成了许多独立的步骤,所以你可以看到它是如何工作的,一步一步的。

为了简单起见,我假设你的文件名是A1

B1 = LEN(A1)
确定文件名的长度

C1 = SUBSTITUTE(A1,“”,“”)
用空格replace空格

D1 = LEN(C1)
看看这个string是多less,如果你用空格replace空格

E1 = B1-D1
确定有多less空间

F1 = SUBSTITUTE(A1,“”,CHAR(8),E1)
用文件名中不能出现的特殊字符replace最后一个空格

G1 = SEARCH(CHAR(8),F1)
find特殊的字符。 现在我们知道最后一个空间在哪里了

H1 =左(A1,G1-1)
剥去最后一个空间之前的一切

I1 = MID(A1,G1 + 1,255)
剥去最后一个空间后的一切

J1 = FIND(“。”,I1)
find第一个点

K1 = FIND(“。”,I1,J1 + 1)
find第二个点

L1 = FIND(“。”,I1,K1 + 1)
find第三个点

M1 = MID(I1,1,J1-1)
find第一个号码

N1 = MID(I1,J1 + 1,K1-J1-1)
find第二个号码

O1 = MID(I1,K1 + 1,L1-K1-1)
find第三个号码

P1 =文本(M1,“00”)
垫第一个号码

Q1 = TEXT(N1,“00”)
垫第二个号码

R1 = TEXT(O1,“00”)
垫第三个号码

S1 = IF(ISERR(K1),M1,P1&Q1&R1)
把数字放在一起

T1 = H1&“”&S1&“。pdf”
把它放在一起

这有点乱,因为Excel在20多年中没有添加一个新的string操作函数,所以应该很容易的事情(比如“查找最后的空间”)需要严重的欺骗。

下面是一个简单的基于Excel命令和公式的四步法的截图,在回答的post的评论(有一些变化)中提出了…

在这里输入图像描述

下面的这个function工作。 我假定date是ddmmyy格式,但是如果是mmddyy适当调整 – 我不能从你的例子mmddyy出来。

 Function FormatThis(str As String) As String Dim strDate As String Dim iDateStart As Long Dim iDateEnd As Long Dim temp As Variant ' Pick out the date part iDateStart = GetFirstNumPosition(str, False) iDateEnd = GetFirstNumPosition(str, True) strDate = Mid(str, iDateStart, iDateEnd - iDateStart + 1) If InStr(strDate, ".") <> 0 Then ' Deal with the dot delimiters in the date temp = Split(strDate, ".") strDate = Format(DateSerial( _ CInt(temp(2)), CInt(temp(1)), CInt(temp(0))), "ddmmyy") Else ' No dot delimiters... assume date is already formatted as ddmmyy ' Do nothing End If ' Piece it together FormatThis = Left(str, iDateStart - 1) _ & strDate & Right(str, Len(str) - iDateEnd) End Function 

这使用以下辅助函数:

 Function GetFirstNumPosition(str As String, startFromRight As Boolean) As Long Dim i As Long Dim startIndex As Long Dim endIndex As Long Dim indexStep As Integer If startFromRight Then startIndex = Len(str) endIndex = 1 indexStep = -1 Else startIndex = 1 endIndex = Len(str) indexStep = 1 End If For i = startIndex To endIndex Step indexStep If Mid(str, i, 1) Like "[0-9]" Then GetFirstNumPosition = i Exit For End If Next i End Function 

去testing:

 Sub tester() MsgBox FormatThis("Smith, J. 01.03.12.pdf") MsgBox FormatThis("Smith, J. 010312.pdf") MsgBox FormatThis("Smith, J. 1.03.12.pdf") MsgBox FormatThis("Smith, J. 1.3.12.pdf") End Sub 

他们都返回"Smith, J. 010312.pdf"

你不需要VBA。 首先用“。”replace掉。

  =SUBSTITUTE(A1,".","") 

这将把“.PDF”改为“PDF”,所以让我们回过头来:

  =SUBSTITUTE(SUBSTITUTE(A1,".",""),"pdf",".pdf") 

有awk吗? 将数据获取到文本文件中

 awk -F'.' '{ if(/[0-9]+\.[0-9]+\.[0-9]+/) printf("%s., %02d%02d%02d.pdf\n", $1, $2, $3, length($4) > 2 ? substr($4,3,2) : $4); else print $0; }' your_text_file 

假设数据与您所描述的完全相同,例如,

Smith,J. 010112.pdf
Mit,H. 01.02.12.pdf
Excel,M 8.1.1989.pdf
Lec,X. 06.28.2012.pdf

免责声明:

正如@ Jean-FrançoisCorbett所提到的,这对"Smith, J. 1.01.12.pdf" 而不是完全重做,我build议他的解决scheme!

 Option Explicit Function ExtractNumerals(Original As String) As String 'Pass everything up to and including ".pdf", then concatenate the result of this function with ".pdf". 'This will not return the ".pdf" if passed, which is generally not my ideal solution, but it's a simpler form that still should get the job done. 'If you have varying extensions, then look at the code of the test sub as a guide for how to compensate for the truncation this function creates. Dim i As Integer Dim bFoundFirstNum As Boolean For i = 1 To Len(Original) If IsNumeric(Mid(Original, i, 1)) Then bFoundFirstNum = True ExtractNumerals = ExtractNumerals & Mid(Original, i, 1) ElseIf Not bFoundFirstNum Then ExtractNumerals = ExtractNumerals & Mid(Original, i, 1) End If Next i End Function 

我用这个作为一个testing用例,它不能正确覆盖你所有的例子:

 Sub test() MsgBox ExtractNumerals("Smith, J. 010112.pdf") & ".pdf" End Sub