使用可变文件名导入

时间:2019-10-10 09:41:37

标签: excel

每天,我都会将一些文件导入主电子表格。该文件生成的时间戳随机8位数字,所以我不能预期文件名。我以前有?????????代替正在工作的代码。经过一些IT更改后,它现在不起作用,我们只是更改了驱动器,并且如果我添加了序列而不是???的

,我更新了位置就可以了

如何做到这一点,以便模仿以前在??????中所产生的效果?有效-大多数文件名是唯一的,可以估计_(带有日期等),但只能使用最后8位数字。

Dim Wb1 As Workbook
  Dim Wb2 As Workbook
  Dim filepath As String

  filepath = Sheets("control").Cells(1, 5).Value

  Application.ScreenUpdating = False
  Set Wb1 = Workbooks.Open("C:\Users\damian_MCP\Mobius Capital Partners\Mobius _
          Capital Partners Team Site - Documents\Operations\FTP\" & filepath & _
           "\5446890_FMCM_" & filepath & "_(76765435).xls")
  'current acc FMCM

...此部分的解决方法

Set Wb1 = Workbooks.Open("C:\Users\damian_MCP\Mobius Capital Partners\Mobius_
         Capital Partners Team Site - Documents\Operations\FTP\" & filepath & _
         "\5446890_FMCM_" & filepath & "_(76765435).xls")

如何编辑此内容以仅搜索FMCM?

Sub ubstransi()

Application.DisplayAlerts = False
Sheets("ubs trans").Select

' This section could have an unexpected result because it is not clear which cells are you clearing

Cells.Select
Selection.ClearContents
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Dim filepath As String

filepath = Sheets("control").Cells(1, 5).Value

Set Wb1 = Workbooks.Open("C:\Users\damian_MCP\Mobius Capital Partners\Mobius Capital Partners Team Site - Documents\Operations\FTP\" & filepath & "\5446890_FMCM_" & filepath & "_(76882269).xls")
'FMCM *** NOTE that there is no wildcard in your code...



Wb1.Sheets("Cash Movement").Range("A1:x100").Copy
Windows("MEMF RECS2.xlsm").Activate
Sheets("ubs trans").Range("a1").PasteSpecial Paste:=xlPasteAll

Sheets("UBS trans").Select
    Range("AD2").Select
    ActiveCell.FormulaR1C1 = _
         "=IF(AND((IF(OR(RC[-22]=""FOREX TRADE SPOT"",RC[-22]=""Transfer"",LEFT(RC[-22],5)=""UBSFX"",LEFT(RC[-22],6)=""UBS FX""),""FX"",0)=""FX""),RC[-21]=control!R2C3),""FX"",0)"
   Range("AD2").Select
    Selection.Copy
    Range("AD100").Select
    Range(Selection, Selection.End(xlUp)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

Wb1.Close SaveChanges:=False


Set Wb2 = Workbooks.Open("C:\Users\damian_MCP\Mobius Capital Partners\Mobius Capital Partners Team Site - Documents\Operations\FTP\" & filepath & "\5446890_FMSH_" & filepath & "_(76885899).xls")
'FMSH *** NOTE that there is no wildcard in your code...
Wb2.Sheets("Securities Holdings").Range("A1:X100").Copy

Windows("MEMF RECS2.xlsm").Activate
Sheets("UBS AM POS").Range("a1").PasteSpecial Paste:=xlPasteAll

Wb2.Close SaveChanges:=False



Windows("MEMF RECS2.xlsm").Activate

Sheets("BBGCASH").Select
    Range("A1").Select

    Dim datename As String
    Dim datename2 As String
    datename = Sheets("control").Cells(1, 5).Value
    datename2 = Sheets("control").Cells(2, 5).Value

    ' This instruction can not be handle as it is not setting to any var.

    Workbooks.Open Filename:= _
        "C:\Users\damian_MCP\Mobius Capital Partners\Mobius Capital Partners Team Site - Documents\Operations\FTP\" & datename & "\f3576cshdump2.ext." & datename2 & ".1.txt"

    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True
    Cells.Select
    Selection.Copy
    Windows("MEMF RECS2.xlsm").Activate
    Sheets("BBGCASH").Select
    Range("A1").Select
    ActiveSheet.Paste


Sheets("control").Select


End Sub

1 个答案:

答案 0 :(得分:0)

尝试这个:

Sub Open_Workboook()

Dim WB As Workbook
Dim MyPath As String, MyFile As String
Dim filepath as String

filepath = Sheets("control").Cells(1, 5).Value

MyPath = "C:\Users\damian_MCP\Mobius Capital Partners\Mobius _
    Capital Partners Team Site - Documents\Operations\FTP\"
MyFile = Dir(MyPath & "*_FMCM_" & filepath & "_(76765435).xls")

Do While MyFile <> ""
    Application.ScreenUpdating = False
    Set WB = Workbooks.Open(MyPath & MyFile)
    MyName = WB.Name
    If MsgBox("Is this the file you want to open?" & vbTab & MyName, vbYesNo) = vbYes Then
        GoTo continue
    Else
        WB.Close
    End If
        MyFile = Dir()
Loop
Exit Sub

continue:
WB.Close
MsgBox "File Find... and close " & MyName

End Sub

它将起作用,以后再适应。

希望有帮助