将文件名解析为excel vba

时间:2018-05-10 20:30:46

标签: excel vba

我正在尝试隔离我从包含一系列名称包含地址更改的文件的文件夹中获取的地址更改。请参阅我作为我正在绘制的文件夹示例中包含的第一张图片。我遍历文件夹,并将原始地址和新地址输出到excel表格以获得优秀。我遇到的问题是并非所有文件名都相同,所以我目前无法从文件名中提取正确的地址更改信息。包含的第二张照片是输出的照片,黄色的文件是我的脚本无法迭代的文件名。如果有人对如何扩大我可以处理的案件数量有任何建议,那么请参阅下面的当前代码。

enter image description here

enter image description here

Dim AddChng As Worksheet

If sheetExists("AddressChange") Then    'create a new sheet if one doesn't exist
    Set AddChng = ThisWorkbook.Sheets("AddressChange")
Else
    Set AddChng = ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count))
    AddChng.Name = "AddressChange"
End If
AddChng.UsedRange.Delete shift:=xlUp    'clear the sheet
AddChng.Range("A1").Value = "Old Name"  'set up
AddChng.Range("B1").Value = "New Name"
AddChng.Activate
AddChng.Range("A2").Select
Dim StrFile As String
'Change this to the directory containing all Address Change Circulation emails
'This will Pull in a list and, to the best of its ability make two columns that hold the data for
'the old and the new address
StrFile = Dir(Range("AddressChangeFolderPath").Value)
Dim Names() As String
Dim StrName
Do While Len(StrFile) > 0
    CheckVal = InStr(1, StrFile, "Address Change Circulation -", vbTextCompare) + _
        InStr(1, StrFile, "Address Change Circulation from ", vbTextCompare)
    If CheckVal <> 1 Then   'if the email does not fit the standard, just place it in the cell and
                            'move on to the next entry
        Selection.Value = StrFile
        Selection.Interior.Color = RGB(255, 255, 0) 'highlight the cell
        Selection.Offset(1, 0).Select
    Else
        StrName = Right(StrFile, Len(StrFile) - 29) 'trim to the correct size - probably not the
                                                    'best way to do this but it works
        If Left(StrName, 4) = "from" Then
            StrName = Right(StrName, Len(StrName) - 5)
        ElseIf Left(StrName, 2) = "om" Then
            StrName = Right(StrName, Len(StrName) - 3)
        End If
        StrName = Left(StrName, Len(StrName) - 4)
        Changes = Split(StrName, " and ")
        For Each Change In Changes
            Names = Split(Change, " to ")

            If Len(Names(0)) < 5 Then
                Selection.Value = Names(0) & Right(Names(1), Len(Names(1)) - Len(Names(0)))
            Else
                Selection.Value = Names(0)
            End If
            If UBound(Names) >= 1 Then 'this is a zero indexed array, checking greater than or
                                       'equal to 1 will check if there are two or more entries
                Selection.Offset(0, 1).Value = Names(1) ' in the event that there is no " to " in
                                                'the file name and it hasn't been handeled already
            End If
            Selection.Offset(1, 0).Select 'select the next cell to accept the next entry
        Next
    End If

    StrFile = Dir
Loop

MsgBox "Make sure to QAQC the new table and update any fields that haven't been properly " & _
    "filled in by the automation."

End Sub

1 个答案:

答案 0 :(得分:0)

“..并非所有文件名都是相同的......”我希望如此。

您显然必须将“Address Change from”添加到“CheckVal = ..”指令中。并添加所有其他可能的变化!

我建议您单独检查每个案例并单独处理每个案例。要处理一个“其他”的所有情况必须是错误的。 IMHO。