2017-01-18 8 views
1

こんにちは私はVBAマクロを作成しようとしています。このマクロは、Excelファイルをブラウズしてからファイルを操作します。次のように私が書いたコードは次のとおりです。VBAブラウズファイルと操作を作成

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 

最初のサブSelectFileは、ファイルを選択し、私は、セルB2にファイルパスを持っています。だから、Sub ISINのセルB2からのパスを使用したいと思います。

アドレスを書き込むと動作しますが、マクロが自動的にアドレスを取得する必要があります。

また、別のワークシートを開くことなく変更がmadaである可能性があります。

+0

_ "とは何を意味しますか?マクロには自動的にアドレスが必要です。 – user3598756

答えて

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 
+0

問題は最初のマクロにあります。私は他のマクロにも他のファイルを選択しています – tombata

+0

@AntonPetrov:これについてもう少し分かりますか?異なるサブ選択で複数のプロシージャを実行する場合、同じ「Open Dialog」を使用しますか?同じ選択の場合は、マークした場所に追加するだけです! ;) – R3uK

+0

Open Dialogを使用して、別のマクロで使用しているファイルを完全にExcelで開くこともできます。 – tombata