0

我的 Access 数据库以 xls 格式导出报告,需要进一步修改(对列进行一些手动调整等 + 从前一天的报告中查找一些评论)。

这是我到目前为止创建的部分代码:


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 时,那么我怎样才能禁用它并只保留一次访问引用 strFile 的权限?

问题:到目前为止,被引用的工作簿中的每个第一张表都称为 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 回答
0

您是否尝试过从这些行中取出 .FormularR1C1 ?

另外,我不确定您要对工作表名称做什么,但是您可能可以从中破解一些东西?

Debug.Print Worksheets(1).Name

或者

For Each ws In Worksheets
    Debug.Print ws.Name
Next

更新:

试试这个,然后报告?

With xl.Worksheets(1)
    .Range("I2").FormulaR1C1 = "=VLOOKUP(RC7,[" & strFile & "]Sheet1!C7:C12,3,0)"
    .Range("J2").FormulaR1C1 = "=VLOOKUP(RC7,[" & strFile & "]Sheet1!C7:C12,4,0)"
End With

因此,额外澄清了哪个范围,并且没有额外的撇号

于 2013-06-27T08:03:58.347 回答