使用通配符ERROR从其他工作簿中获取数据

问题描述 投票:0回答:2

我知道这可能是一个愚蠢的错误,但我尝试了许多方法,但没有一个工作。

我有这个代码用于从文件夹中的其他Excel工作表中获取数据并将其粘贴到主文件夹中。当我尝试使用通配符查找名称的一部分是可变的文件时,会弹出问题。在下面的示例中,文件名是Stock_RTC_17.02.2019.xlsx

但是,excel会返回一个错误,即在下面的代码文件夹中找不到该文件,即使您可以看到它找到了正确的文件名。任何人都知道我做错了什么?

enter image description here

Sub copytest() 'Procedure for retrieving data from the sourcefiles

    Dim wbTarget, wbSource As Workbook
    Dim target As Object
    Dim pathSource As String
    Dim xlApp As Application

    'path where the data source folders are located (please keep all of them in the same directory)
    pathSource = "C:\Users\vferraz\Desktop\crm stock\RAPOARTE IMPORTANTE\18.02\Rapoarte pentru Handsets\"
    Set wbTarget = ThisWorkbook

    Set xlApp = CreateObject("Excel.Application")
    xlApp.DisplayAlerts = False
    Application.CutCopyMode = False

    'Stock RTC
    Dim FileName As String

    FileName = Dir(pathSource & "Stock_RTC_*.xlsx", vbNormal)

    Set wbSource = xlApp.Workbooks.Open(FileName)

    wbSource.Sheets(1).UsedRange.Copy
    wbSource.Close
    Set target = wbTarget.Sheets("Stock Aberon GW TKR")
    target.UsedRange.Clear
    Range("A1").Select
    target.Paste

End Sub
excel vba wildcard
2个回答
1
投票

我认为pathSource不是当前的工作目录,所以你应该写:

Set wbSource = xlApp.Workbooks.Open(pathSource & FileName)

1
投票

您尝试打开工作簿时缺少路径。

Set wbSource = xlApp.Workbooks.Open(pathSource & FileName)
© www.soinside.com 2019 - 2024. All rights reserved.