2016-04-25 106 views
0

我有一段代碼來查找右下角的單元格,它運行在excel中,我希望能夠通過Access子例程運行它,它將返回單元座標(例如:J17)。但是我對Access並不熟悉,不確定如何翻譯代碼。在Access子程序的Excel文檔上運行VBA Excel腳本

Sub FindLast_Message() 

MsgBox FindLast(3) 

End Sub 

Function FindLast(lRowColCell As Long, _ 
       Optional sSheet As String, _ 
       Optional sRange As String) 
'Find the last row, column, or cell using the Range.Find method 
'lRowColCell: 1=Row, 2=Col, 3=Cell 

Dim lRow As Long 
Dim lCol As Long 
Dim wsFind As Worksheet 
Dim rFind As Range 

'Default to ActiveSheet if none specified 
On Error GoTo ErrExit 

If sSheet = "" Then 
    Set wsFind = ActiveSheet 
Else 
    Set wsFind = Worksheets(sSheet) 
End If 

'Default to all cells if range no specified 
If sRange = "" Then 
    Set rFind = wsFind.Cells 
Else 
    Set rFind = wsFind.Range(sRange) 
End If 

On Error GoTo 0 

Select Case lRowColCell 

    Case 1 'Find last row 
     On Error Resume Next 
     FindLast = rFind.Find(What:="*", _ 
         After:=rFind.Cells(1), _ 
         LookAt:=xlPart, _ 
         LookIn:=xlFormulas, _ 
         SearchOrder:=xlByRows, _ 
         SearchDirection:=xlPrevious, _ 
         MatchCase:=False).Row 
     On Error GoTo 0 

    Case 2 'Find last column 
     On Error Resume Next 
     FindLast = rFind.Find(What:="*", _ 
         After:=rFind.Cells(1), _ 
         LookAt:=xlPart, _ 
         LookIn:=xlFormulas, _ 
         SearchOrder:=xlByColumns, _ 
         SearchDirection:=xlPrevious, _ 
         MatchCase:=False).Column 
     On Error GoTo 0 

    Case 3 'Find last cell by finding last row & col 
     On Error Resume Next 
     lRow = rFind.Find(What:="*", _ 
         After:=rFind.Cells(1), _ 
         LookAt:=xlPart, _ 
         LookIn:=xlFormulas, _ 
         SearchOrder:=xlByRows, _ 
         SearchDirection:=xlPrevious, _ 
         MatchCase:=False).Row 
     On Error GoTo 0 

     On Error Resume Next 
     lCol = rFind.Find(What:="*", _ 
         After:=rFind.Cells(1), _ 
         LookAt:=xlPart, _ 
         LookIn:=xlFormulas, _ 
         SearchOrder:=xlByColumns, _ 
         SearchDirection:=xlPrevious, _ 
         MatchCase:=False).Column 
     On Error GoTo 0 

     On Error Resume Next 
     FindLast = wsFind.Cells(lRow, lCol).Address(False, False) 
     'If lRow or lCol = 0 then entire sheet is blank, return "A1" 
     If Err.Number > 0 Then 
      FindLast = rFind.Cells(1).Address(False, False) 
      Err.Clear 
     End If 
     On Error GoTo 0 

End Select 

Exit Function 

ErrExit: 

MsgBox "Error setting the worksheet or range." 

End Function 

下面是訪問代碼,我需要協調這一部分。 'J72'應該是前面的代碼返回的右下方的單元座標。

Sub Format_Excel_Workbook(workbook_path As String, worksheet_name As String, myRows As Integer, myColumns As Integer) 
'============================================================================== 
Dim objExcelApp As Object 
Dim xlWbk As Object 
'============================================================================== 

Dim x, y As String 

x = "B2" 
y = "J72" 
Z = x & ":" & y 

'============================================================================== 
Set objExcelApp = New Excel.Application 

objExcelApp.Workbooks.Open (workbook_path) 

objExcelApp.Worksheets("t_DATA").Columns.AutoFit 

objExcelApp.Worksheets("t_DATA").Range(x).Select 

objExcelApp.ActiveWindow.FreezePanes = True 

objExcelApp.Worksheets("t_DATA").Range(Z).HorizontalAlignment = xlCenter 

objExcelApp.Worksheets("t_DATA").Range(Z).VerticalAlignment = xlTop 

objExcelApp.ActiveWorkbook.Close (True) 

Set objExcelApp = Nothing 
'============================================================================== 

End Sub 

回答

0

最簡單的方法可能是您的FindLast()函數的參數更改爲對象,而不是字符串:

Function FindLast(lRowColCell As Long, _ 
        Optional sSheet As Excel.Worksheet, _ 
        Optional sRange As Excel.Range) 

從Excel中,你可以調用這個函數是這樣的:

FindLast(3, , FindLast(3, , ThisWorkbook.Sheets(1).Range("A3:E7"))) 

在函數中,您必須更改使用參數sSheet和sRange的那些部分:只需使用提供的對象,而不是從鋼帶中創建它們GS。

通過這種方式更改功能,您可以輕鬆地將其傳輸到其他主機應用程序,如Access,因爲函數的調用者定義函數應在其上運行的對象,而不是函數本身。

從訪問你可以調用這樣的功能:

FindLast(3, , objExcelApp.Worksheets("t_DATA").Range(Z))