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