首页 文章

关于Dir VBA的问题

提问于
浏览
1

我有一组文件,我想用文件名循环:2015年预算,2016年预算,2017年预算到2022年 . 我使用Dir循环使用它们 .

Dim OpenWb as workbook
path = Dir("C:\pathtofile\Budget 20??.xlsx") 'Using the ? Dir Wildcard
filepath = "C:\pathtofile\" 'Since Dir returns file name, not the whole path
myHeadings = Split("Januari,Februari,Mars,April,Maj,Juni,Juli,Augusti,September,Oktober,November,December", ",")
j = 0
 Do While Len(path) > 0
    i = 0
    If Dir = "Budget 2014.xlsx" Then
        j=0
    Else
        For i = 0 To UBound(myHeadings)
            Set openWb = Workbooks.Open(filepath & path)
            MsgBox Len(path)
            Set openWs = openWb.Sheets(myHeadings(i))
            If openWs.Range("C34") = 0 Then
                currentWb.Sheets("Indata").Cells(70, i + 27 + 12 * (j + 1)).Value = ""
            Else
                currentWb.Sheets("Indata").Cells(70, i + 27 + 12 * (j + 1)).Value = openWs.Range("C34")
            End If
        Next i
   End if
   path = Dir
   j= j + 1
  Loop

问题是在文件路径中还有一个名为Budget 2014的文件,我不想循环,因为1)没有必要,已经计算了值和2)因为它在循环中搞砸了我的索引

更新了我的代码 . 但是在for i = 0 ...循环中使用msgBox(path)返回“Budget 2014.xlsx”,我不想循环,因此这与我的j下标“混乱” .

3 回答

  • 1

    您可以使用Year方法 . 就像是,

    Dim OpenWb as workbook, yearNo As Long, filepath As String
    
    filepath = "C:\pathtofile\"
    yearNo = Year(Date())
    
    path = Dir("C:\pathtofile\Budget " & yearNo & ".xlsx")
    
    Do While Len(path) > 0
        set OpenWb = Workbooks.open(filepath & path) ' Since Dir only returns file name
        'Doing some things
        yearNo = yearNo + 1
        path = Dir("C:\pathtofile\Budget " & yearNo & ".xlsx")
    Loop
    
  • 1

    你也可以试试这个:

    Dim OpenWb as workbook
    path = Dir("C:\pathtofile\Budget 20??.xlsx") 'Using the ? Dir Wildcard
    filepath = "C:\pathtofile\" 'Since Dir returns file name, not the whole path
    myHeadings = Split("Januari,Februari,Mars,April,Maj,Juni,Juli,Augusti,September,Oktober,November,December", ",")
    j = 0
     Do While Len(path) > 0
        i = 0
    'change here: only execute if it's NOT the file you're NOT after
        If Dir <> "Budget 2014.xlsx" Then
            For i = 0 To UBound(myHeadings)
                Set openWb = Workbooks.Open(filepath & path)
                MsgBox Len(path)
                Set openWs = openWb.Sheets(myHeadings(i))
                If openWs.Range("C34") = 0 Then
                    currentWb.Sheets("Indata").Cells(70, i + 27 + 12 * (j + 1)).Value = ""
                Else
                    currentWb.Sheets("Indata").Cells(70, i + 27 + 12 * (j + 1)).Value = openWs.Range("C34")
                End If
            Next i
    'Change here: only update path & j if you processed the file
           path = Dir
           j= j + 1
       End if
      Loop
    
  • 1
    Sub M_snb()
     c00 = "C:\pathtofile\"
     sn = Application.GetCustomListContents(4)
    
     c01 = Dir(c00 & "Budget 20*.xlsx")
     Do While c01 <> ""
      If c01 <> "Budget 2014.xlsx" Then
       With GetObject(c00 & c01)
        For j = 0 To UBound(sn)
         c02 = c02 & "|" & IIf(.Sheets(sn(j)).Range("C34") = 0, "", .Sheets(sn(j)).Range("C34"))
        Next
        .Close 0
       End With
      End If
      c01 = Dir
     Loop
    
     sp = Split(Mid(c02, 2), "|")
     ThisWorkbook.Sheets("Indata").Cells(70, 51).Resize(, UBound(sp)) = sp
    End Sub
    

相关问题