【问题标题】:Combine ActiveCell.Value and Characters to search file结合 ActiveCell.Value 和 Characters 来搜索文件
【发布时间】:2015-10-09 17:02:20
【问题描述】:

我正在尝试将工作簿中的引号名称超链接到文件的位置。并非所有文件都存在,我想在创建超链接之前对布尔值进行测试以查看它们是否存在。我一直在使用活动单元格中的文件名来保留字符串并进行搜索。命名约定是“XX-MMDDYY.XX”。保存的文件名为 Quote_XX-MMDDYY.XX。我已将“Quote_”与活动单元格结合起来搜索文件,但我的宏似乎只在列表中循环。

Sub LoopRange()

    Dim currRow As Integer, lastRow As Integer
    Dim ws As String, quoteID As String
    Dim path As String
    Dim FileName As String
    path = "C:\Some file path\"

    ws = "Quote LOG"
    currRow = 3
    lastRow = Sheets(ws).Cells(Sheets(ws).Rows.Count, "A").End(xlUp).Row

    While currRow <= lastRow
        Sheets(ws).Cells(currRow, 1).Select
        quoteID = "Quote_" & ActiveCell.value
        FileName = path & quoteID
        If Dir(FileName) <> "" And quoteID <> "" Then
            Sheets(ws).Hyperlinks.Add anchor:=Cells(currRow, 2), Address:=FileName, TextToDisplay:=quoteID
        End If
        currRow = currRow + 1
    Wend

End Sub

【问题讨论】:

  • 您需要在路径变量的末尾添加另一个反斜杠。 path = "C:\Some file path\"

标签: string excel vba search


【解决方案1】:

MacroMan 的提示有帮助吗?如果没有,请告诉我,我会试一试。

有一点,对于 VBA 来说非常值得学习,那就是如何(避免使用 .Activate 和 .Select)[How to avoid using Select in Excel VBA macros。考虑到这一点,我已经调整了您的代码,它应该运行得更流畅一些:

Sub LoopRange()

Dim currRow As Integer, lastRow As Integer
Dim ws As String, quoteID As String
Dim path          As String
Dim FileName      As String
Dim cellValue     As String

path = "C:\Some file path\"

ws = "Quote LOG"
currRow = 3
lastRow = Sheets(ws).Cells(Sheets(ws).Rows.Count, "A").End(xlUp).Row

While currRow <= lastRow
    'Sheets(ws).Cells(currRow, 1).Select
    cellValue = Sheets(ws).Cells(currRow, 1)
    'quoteID = "Quote_" & ActiveCell.Value
    quoteID = "Quote_" & Sheets(ws).Cells(currRow, 1)
    Debug.Print quoteID
    FileName = path & quoteID & ".pdf"
    If Dir(FileName) <> "" And quoteID <> "" Then
        Sheets(ws).Hyperlinks.Add anchor:=Cells(currRow, 2), Address:=FileName, TextToDisplay:=quoteID
    End If
    currRow = currRow + 1
Wend

End Sub

【讨论】:

  • 是的,它确实定义了路径。有一段时间我只是让它循环播放
  • 感谢您的远见,不要使用活动单元格!
  • 我已经运行了几次(数据库不完整),它比活动单元更强大。
  • 我也会试一试,今天早上的某个时候。我会及时通知你,让你知道结果如何。
  • @PeaButter - 怎么样?对代码改进有什么 cmets 或建议吗?
【解决方案2】:

我的文件名与代码不匹配。我需要将“Quote_”添加到 quoteID 和文件类型,在这种情况下,将“.pdf”添加到 FileName

** 子循环范围()

Dim currRow As Integer, lastRow As Integer
Dim ws As String, quoteID As String
Dim path As String
Dim FileName As String
path = "C:\Users\pxbotr\Documents\Quote DataBase\"

ws = "Quote LOG"
currRow = 3
lastRow = Sheets(ws).Cells(Sheets(ws).Rows.Count, "A").End(xlUp).Row

While currRow <= lastRow
    Sheets(ws).Cells(currRow, 1).Select
    quoteID = "Quote_" & ActiveCell.Value
    FileName = path & quoteID & ".pdf"
    If Dir(FileName) <> "" And quoteID <> "" Then
        Sheets(ws).Hyperlinks.Add anchor:=Cells(currRow, 1), Address:=FileName, TextToDisplay:=quoteID
    End If
    currRow = currRow + 1
Wend

结束子

**

【讨论】:

  • 一种缩短代码的方法,尽管有点小,是更改ws = "quoteLog" 并执行dim ws As Worksheet 然后Set ws = Sheets("Quote LOG") 然后在后面的代码中您可以执行lastRow = ws.cells(ws.rows.count, ...ws.Hyperlinks.add ...
猜你喜欢
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 2021-03-23
  • 1970-01-01
相关资源
最近更新 更多