您好我正在尝试编写一个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
第一个Sub SelectFile选择文件,我在单元格B2中有文件路径。所以我想使用Sub ISIN中来自单元格B2的路径。
如果我写了它的工作地址,但我希望宏自动获取地址。
也可能是mada没有打开另一个工作表。
答案 0 :(得分:2)
你应该只使用ISIN
中的参数,试试这个! ;)
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