VBA-基于单元格中部分文本打开文件的宏

问题描述 投票:0回答:1

我一直在尝试改编我用来打开Excel电子表格中引用的文件的宏。但是,这一次在电子表格中的引用周围以“ work_”开头,后接文件名,例如:work_1234我很确定我需要在代码的粗体部分中进行一些更改,但是我不知道如何使ActiveCell.Text适应仅使用数字作为搜索目录所需的信息。

Dim directories(10) As String, fileName As String, i As Integer

directories(0) = "Users/username/folder/subfolder/goal/"
directories(1) = "Users/username/folder/subfolder/goal/1"
directories(2) = "Users/username/folder/subfolder/goal/2"
directories(3) = "Users/username/folder/subfolder/goal/3"
directories(4) = "/Users/username/folder/subfolder/goal/4"

i = 0

Do While i < 5

    If ActiveCell.Text() = "" Then
        Exit Do
    End If

'look for the numbers in the active cell that will be used when searching the folders listed above'  
    **fileName = Dir(directories(i) & "*" & ActiveCell.Text() & "*", MacID("Macintosh HD"))**
'fileName = "Users/username/folder/subfolder/goal/1234.html"'
    If fileName <> "" Then
        CreateObject("Shell.Application").Open (fileName) 'directories(i) & fileName)
        fileName = Dir()
        Exit Do
    End If
    i = i + 1
Loop

End Sub

我希望能够传达我正在尝试做的事情,并非常感谢您可以提供任何帮助我的见解。我对VBA并不十分熟悉,并且在搜寻了永久的网际网路之后,我想我会在这里找到一些帮助!

excel vba
1个回答
0
投票

欢迎来到SO。您可以通过这种方式获取单元格的值:

MyValue = Worksheets("Sheet1").Range("C2").Value
MyValue = Worksheets("Sheet1").Cells(2,3).Value

下一步,您可以这样做:

AnotherValue = Right(MyValue,Len(MyValue)-Len("work_"))

要快速开始使用VBA,请尝试例如这些免费课程:https://homeandlearn.org/https://www.excel-pratique.com/en/祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.