【问题标题】:Questions regarding Dir VBA关于 Dir VBA 的问题
【发布时间】:2015-03-17 16:05:43
【问题描述】:

我有一组文件,我想用文件名循环遍历它们:Budget 2015、Budget 2016、Budget 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(路径)会返回我不想循环的“Budget 2014.xlsx”,因此这与我的 j 下标“混乱”。

【问题讨论】:

  • 我认为你将不得不在循环中添加一个 if 条件

标签: vba excel


【解决方案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

【讨论】:

  • 谢谢!尚未测试,但它确实是一个我认为应该有效的创造性解决方案。如果你有时间,我有一个后续问题。我首先将值减去 AA70(请参阅更新的代码),在其中我添加了一个常数 27 到“到达”列 AA 开始。现在使用 Year 和我的语法,意味着下一次计算 yearNo = yearNo+1 时,将用 2015 值覆盖我的 AA70 单元格。有没有解决的办法?明年也不需要更新代码,Year(Date()) 将返回 2016 年,并使 AA70 具有 2016 年的值?
  • 也许我明白了!现在必须下班,但会尝试用 Year(Date()) mod 2015 替换 j,看看明天会怎样
  • 更像yearNo Mod Year(Date()) ;)
  • 我认为 yearNo Mod Year(Date()) 明年将返回 0,因此表达式将再次 AA70 导致程序明年覆盖 2015 年的值。然而,这个解决方案完美地工作!这是我有史以来的第一个宏,所以我正在一点一点地构建它,这个解决方案实际上帮助我完成了一个我还没有开始尝试解决的问题(即明年会发生什么)。我仍然需要弄清楚如何处理 12 月,因为值会写入明年导入的文件中,所以我需要在 2016 年扣除它们,但随后我的程序也会从 2016 年开始
  • 很高兴能帮上忙 :) Good Lick !
【解决方案2】:

你也可以试试这个:

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

【讨论】:

  • 感谢您的回答!该解决方案将在以后的循环中帮助我,我没有在这里写过。我在使用 If Dir .... 循环时遇到了一些麻烦,但现在我发现它可以以比我预期的更简单的方式完成。
【解决方案3】:
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

【讨论】:

  • +1 因为我相信这将帮助我摆脱使用 c02 = c02 & "|" 的一堆 if 语句... 解决方案。为什么要包含“|”那里?什么是sn?试图谷歌GetCustomListContents,但还没有理解。你从来没有给 sn 赋值?它如何“理解”您所指的 CustomList?
  • Excel 有 4 个内置的“自定义”列表(MS 的幽默!)。 custonlist 是一个将分配给变量 sn 的一维数组。我包括“|”分隔字符串中的值,以便稍后可以将字符串拆分为一维数组(名为“sp”)。您可以在 www.snb-vba.eu 网站上找到讨论自定义列表的页面
  • 感谢您的回复!会后会查看(我按你的?)网页。很高兴看到一些更先进的技术来完成工作,再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 2012-05-12
  • 2016-04-24
相关资源
最近更新 更多