【问题标题】:How to define file directory in VBA如何在 VBA 中定义文件目录
【发布时间】:2015-03-28 17:10:41
【问题描述】:

我现在正在自己学习 Excel 中的 VBA。正如我们在 excel 中所知道的,我们可以将文件目录放在函数中,以将文件中的一个值链接到另一个值。例如,当我们在一个单元格中时,我们可以这样做

B2 ='C:\directory\[filename.xlsx]sheetname'!A1

如何将其放入 VBA 脚本中? 最终我该如何预先定义“目录”、“文件名”、“工作表名”甚至单元格位置,比如

directory = "myfolder\myfolder2\.."
cell = "A1"

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    如果你正在学习,你会这样做

    请注意,代码测试文件路径是否有效

    您的后续问题

    [b2] = "='" & strPath & "[" & strFile & "]" & strSht & "'!" & strCell
    [b2].Copy [b3:b4]
    

    原创

    Sub Test()
    
    Dim strPath As String
    Dim strFile As String
    Dim strSht As String
    Dim strCell As String
    
    strSht = "Sheet2"
    strCell = "A1"
    
    strPath = "C:\temp\"
    strFile = "test.xlsx"
    
    If Len(Dir(strPath & strFile)) > 0 Then
        [b2] = "='" & strPath & "[" & strFile & "]" & strSht & "'!" & strCell
    Else
        MsgBox "invalid file", vbCritical
    End If
    
    End Sub
    

    【讨论】:

    • 感谢您的帮助您知道如何分配一个单元格范围吗?我可以写 [B2: B4] = "='" & strPath & "[" & strFile & "]" & strSht & "'!" & strCell 其中 strCell 是“A1 : A3”?
    【解决方案2】:

    我“认为”您是在问如何将该字符串拆分为组件,是吗?这是相当长篇大论的,但对如何弹奏弦乐有很好的了解,我希望它对你的学习有所帮助。我已经为你注释了每一行。

    一开始这似乎有点令人生畏,但如果您现在对 split 命令和数组感到满意,它将大大帮助您前进:

    Sub SplitCellContents()
    Dim MyString As String, MySheetName As String, MyCell As String, MyDrive As String, MyDir As String, MyFileName As String
    MyString = "'C:\directory\[filename.xlsx]sheetname'!A1" 'Set the string value
    MyCell = Split(MyString, "!")(UBound(Split(MyString, "!"))) 'Split the string into an array on the ! and take the last value in the array
    MySheetName = Split(Split(MyString, "]")(UBound(Split(MyString, "]"))), "'")(0) 'Split the string into an array on "]" then split the resulting last value and split again on "'" and take the first value
    ' Look at what the above line does, split on ] gives a last value of sheetname'!A1 then split that on ' gives us a first value of sheetname
    MyDrive = Replace(Split(MyString, "\")(0), "'", "") 'Split the string on \ and take first value
    MyString = Replace(MyString, "'" & MyDrive, "") 'Chop out the drive reference from the string to allow further manipulation
    MyString = Replace(MyString, "'!" & MyCell, "") 'Chop out the cell reference from the string to allow further manupulation
    MyFileName = Replace(Replace(Split(MyString, "[")(UBound(Split(MyString, "["))), "]", ""), MySheetName, "") 'Similar to what we do for mycell, see if you can work out how
    MyDir = Replace(Replace(MyString, "[" & MyFileName & "]", ""), MySheetName, "") ' Replace the fileName and sheetname in the string with nothing, should leave the DIR
    MsgBox "MyCell = " & MyCell & vbLf & _
        "MySheetName = " & MySheetName & vbLf & _
        "MyDrive = " & MyDrive & vbLf & _
        "MyDir = " & MyDir & vbLf & _
        "MyFileName = " & MyFileName 'Output to a messagebox
    End Sub
    

    看起来很吓人,但把它粘贴到 VBE 中,看看你是否可以关注它。

    有很多方法可以使用字符串,我确实更喜欢拆分和数组操作,但很多人会使用 Mid、Left、Right 和 Find / Instr 的组合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      相关资源
      最近更新 更多