VBA单元格值作为文件夹

时间:2017-04-03 14:37:33

标签: excel vba excel-vba date

我希望你能帮助我目前有一段代码(见下文),允许用户选择一个文件夹。然后代码打开该文件夹中的所有工作簿,选择一个特定的工作表,名为" SearchCaseResults"从每个工作簿中复制此工作表上的数据,然后将其粘贴到另一个工作表中#34; Disputes"在另一个文件夹中的另一个工作簿中。

这一切都很完美,但我现在想要发生的是,而不是打开文件夹中的每个工作簿。我只希望它根据B6和B7的单元格值在文件夹中打开工作簿,我将其作为日期选择器,参见图1以便更好地理解。

因此,当文件夹不为空时,而不是状态所做的代码

Do While myFile <> ""

我想说出像

这样的话
Do While myFile >= "B6" And myFile <= "B7"

上面的代码编译但遗憾的是不起作用

我的代码是否可以修改为仅在单元格B6和B7中列出的日期范围内打开工作簿

我已经用完了在线资源,并且已经搜索了几天的答案,所以我正在寻求帮助

一如既往,我们非常感谢所有人的帮助。

图1 enter image description here

我的代码

Sub LoopAllExcelFilesInFolder()

'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim lRow As Long
Dim ws2 As Worksheet
Dim y As Workbook




'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
    .Title = "C:\Users\CONNELLP\Desktop\Claire Macro\Copy and Paste Disputes\Looper\"
    .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

Set y = ThisWorkbook
Set ws2 = y.Sheets("Disputes")

'Loop through each Excel file in folder
Do While myFile <> ""

    'Set variable equal to opened workbook
    Set wb = Workbooks.Open(Filename:=myPath & myFile)

    'Copy data on "SearchCaseResults" sheet to "Disputes" Sheet in other workbook
    With wb.Sheets("SearchCasesResults")
        lRow = .Range("A" & Rows.Count).End(xlUp).Row
        .Range("A2:M" & lRow).Copy ws2.Range("A" & Rows.Count).End(xlUp)(2)
    End With

    wb.Close SaveChanges:=True
    'Get next file name
    myFile = Dir
Loop

'Message Box when tasks are completed
MsgBox "Task Complete!"

ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

文件夹的图片

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您正在寻找B6和B7中日期之间最后修改的文件,请将其交换到当前循环中:

Do While myFile <> ""

    If Int(FileDateTime(myPath & myFile)) >= Range("B6").Value And _
        Int(FileDateTime(myPath & myFile)) <= Range("B7").Value Then

        'Set variable equal to opened workbook
        Set wb = Workbooks.Open(Filename:=myPath & myFile)

        'Copy data on "SearchCaseResults" sheet to "Disputes" Sheet in other workbook
        With wb.Sheets("SearchCasesResults")
            lRow = .Range("A" & Rows.Count).End(xlUp).Row
            .Range("A2:M" & lRow).Copy ws2.Range("A" & Rows.Count).End(xlUp)(2)
        End With

        wb.Close SaveChanges:=True

    End If

    'Get next file name
    myFile = Dir
Loop

但是,如果您想将文件名本身与单元格中的日期进行比较,则需要向我们显示文件名的格式以供我们帮助。

相关问题