VBA浏览文件并进行操作

VBA浏览文件并进行操作,vba,excel,Vba,Excel,您好,我正在尝试编写一个VBA宏,它浏览excel文件,然后在文件中进行操作。我写的代码如下: Option Explicit Sub SelctFile() Dim intChoice As Integer Dim strPath As String Dim i As Integer 'allow the user to select multiple files Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = Tr

您好,我正在尝试编写一个VBA宏,它浏览excel文件,然后在文件中进行操作。我写的代码如下:

Option Explicit

Sub SelctFile()
Dim intChoice As Integer
Dim strPath As String
Dim i As Integer

'allow the user to select multiple files
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
    'get the file path selected by the user
    For i = 1 To Application.FileDialog(msoFileDialogOpen _
        ).SelectedItems.Count
        strPath = Application.FileDialog(msoFileDialogOpen _
        ).SelectedItems(i)
        'print the file path to sheet 1
        Cells(i + 1, 2) = strPath
    Next i
End If    
End Sub


Sub ISIN()

Dim MSReport As Variant

MSReport = Range("B2").Value

Set MSReport = Workbooks.Open(Filename:="MSReport")
Range("W3:W2500").Formula = "=IF(G3="""","""",BDP(G3&"" Equity"",""ID_ISIN""))"

End Sub
选项显式
子选择文件()
选择整数
将strPath设置为字符串
作为整数的Dim i
'允许用户选择多个文件
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect=True
'使文件对话框对用户可见
intChoice=Application.FileDialog(msoFileDialogOpen.Show)
'确定用户所做的选择
如果选择0,那么
'获取用户选择的文件路径
对于i=1到Application.FileDialog(msoFileDialogOpen_
).SelectedItems.Count
strPath=Application.FileDialog(msoFileDialogOpen_
).SelectedItems(i)
'将文件路径打印到工作表1
单元格(i+1,2)=strPath
接下来我
如果结束
端接头
亚ISIN()
Dim MSReport作为变体
MSReport=范围(“B2”).值
设置MSReport=workbook.Open(文件名:=“MSReport”)
范围(“W3:W2500”)。公式=“=IF(G3=“”,”,BDP(G3和“股权”、“ID”)
端接头
第一个子选项file选择文件,我在单元格B2中有文件路径。所以我想使用Sub-ISIN中单元格B2的路径

如果我写地址,它会工作,但我希望宏自动获取地址


也可能在不打开另一个工作表的情况下进行更改。

您只需在
ISIN
中使用一个参数,试试这个!)

选项显式
子选择文件()
选择整数
将strPath设置为字符串
作为整数的Dim i
'允许用户选择多个文件
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect=True
'使文件对话框对用户可见
intChoice=Application.FileDialog(msoFileDialogOpen.Show)
'确定用户所做的选择
如果选择0,那么
'获取用户选择的文件路径
对于i=1的Application.FileDialog(msoFileDialogOpen),选择editems.Count
strPath=Application.FileDialog(msoFileDialogOpen)。选择editems(i)
ISIN strPath
''在那里添加其他程序!
'NewProcedure strPath
接下来我
如果结束
端接头
子ISIN(ByVal文件路径为字符串)
以Excel.工作簿的形式创建报表
设置MSReport=Workbooks.Open(文件名:=FilePath)
MSReport.Sheets(“SheetName”)。范围(“W3:W2500”)。公式=_
=如果(G3=“”,“BDP”(G3&“权益”,“ID”为“”)
MSReport.Save
MSReport.Close错误
端接头

问题在于第一个宏“我还为其他文件选择了其他文件”macros@AntonPetrov:你能更清楚一点吗?对不同子选择上的多个过程使用相同的“打开对话框”吗?如果是相同的选择,您只需将它们添加到我标记的位置即可!;)我使用“打开”对话框也可以完全打开我在另一个宏中使用的excel文件。@AntonPetrov:您只需在
ISIN strPath
后面添加其他行,就像我注释的
NewProcedure strPath
!那么它解决了您最初的问题吗?您说的“但我希望宏自动获取地址”是什么意思?
Option Explicit

Sub SelctFile()
    Dim intChoice As Integer
    Dim strPath As String
    Dim i As Integer

    'allow the user to select multiple files
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
    'determine what choice the user made
    If intChoice <> 0 Then
        'get the file path selected by the user
        For i = 1 To Application.FileDialog(msoFileDialogOpen).SelectedItems.Count
            strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(i)
            ISIN strPath
            ''Add other procedures there!
            'NewProcedure strPath
        Next i
    End If
End Sub


Sub ISIN(ByVal FilePath As String)
    Dim MSReport As Excel.Workbook
    Set MSReport = Workbooks.Open(Filename:=FilePath)

    MSReport.Sheets("SheetName").Range("W3:W2500").Formula = _
            "=IF(G3="""","""",BDP(G3&"" Equity"",""ID_ISIN""))"

    MSReport.Save
    MSReport.Close False
End Sub