0

私のAccessデータベースはレポートをxlsでエクスポートしますが、これはさらに作り直す必要があります(列などの手動調整+前日のレポートからのいくつかのコメントのvlookuping)。

これまでに作成したコードの一部を次に示します。


Option Compare Database

Function Adjustment()

' First I want to prompt user to select the report from previous day*

Dim f As Object
Dim strFile As String
Dim strFolder As String
Dim varItem As Variant

Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
If f.Show Then
    For Each varItem In f.SelectedItems
        strFile = Dir(varItem)
        MsgBox (strFile)
    Next
End If
Set f = Nothing

' here my Access database opens current report that has been exported   

  Dim xl As Object
  Set xl = CreateObject("Excel.Application")
  xl.Workbooks.Open ("I:\Temp\reports.xlsx")
  xl.Visible = True

' in currently open report, I want fill cell I2 and J2 with VLOOKUP function referencing to previously selected file

  Range("I2").FormulaR1C1 = "=VLOOKUP(RC7,'[" & strFile & "]SheetXY'!C7:C12,3,0)"
  Range("J2").FormulaR1C1 = "=VLOOKUP(RC7,'[" & strFile & "]SheetXY!C7:C12,4,0)"

End function

問題: I2 と J2 に式が入力されているときに、毎回ファイルを選択するように求められます。これを無効にして、Access で strFile を 1 回だけ参照するようにするにはどうすればよいですか?

質問: これまでのところ、参照されたワークブックの最初のシートはすべて SheeyXY と呼ばれていますが、別のシートも参照したい場合はどうすればよいでしょうか (その名前が何であれ、常にワークブックの最初のシートとしましょう)。

4

2 に答える 2

0

多分あなたはこれを試すことができます..

Option Compare Database

Function Adjustment(SheetName as String) '---> add parameter such as "SheetXY"

' First I want to prompt user to select the report from previous day*

Dim f As Object    
Dim strFolder As String
Dim varItem As Variant
Static strFile As String

If strFile = "" Then
  Set f = Application.FileDialog(3)
  f.AllowMultiSelect = True
  If f.Show Then
      For Each varItem In f.SelectedItems
          strFile = Dir(varItem)
          MsgBox (strFile)
      Next
  End If
  Set f = Nothing
Endif 

' here my Access database opens current report that has been exported   

  Dim xl As Object
  Set xl = CreateObject("Excel.Application")
  xl.Workbooks.Open ("I:\Temp\reports.xlsx")
  xl.Visible = True

' in currently open report, I want fill cell I2 and J2 with VLOOKUP function referencing to previously selected file

  Range("I2").FormulaR1C1 = "=VLOOKUP(RC7,'[" & strFile & "]" & SheetName & "'!C7:C12,3,0)"
  Range("J2").FormulaR1C1 = "=VLOOKUP(RC7,'[" & strFile & "]" & SheetName & "'!C7:C12,4,0)"

End function
于 2013-06-27T09:08:41.180 に答える