[英]Run VBA Excel script on an Excel document from an Access subroutine
I have a section of code to find the bottom right cell, that runs in excel, and I want to be able to run it through an Access subroutine, which will return the cell coordinates (Ex.: J17). 我有一段代码可以找到右下角的单元格,该单元格在excel中运行,并且我希望能够通过Access子例程运行它,该子例程将返回单元格坐标(例如:J17)。 However I'm not that familiar with Access and am unsure of how to translate the code.
但是,我对Access不太熟悉,不确定如何翻译代码。
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
Below is the section of Access code where I need to coordinate this with. 下面是我需要与之协调的访问代码部分。 The 'J72' should be the bottom right cell coordinate as returned by the previous code.
“ 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
The easiest way is probably to change the parameters of your FindLast()
function to objects instead of strings: 最简单的方法可能是将
FindLast()
函数的参数更改为对象而不是字符串:
Function FindLast(lRowColCell As Long, _
Optional sSheet As Excel.Worksheet, _
Optional sRange As Excel.Range)
From Excel, you could call this function like this: 在Excel中,您可以这样调用此函数:
FindLast(3, , FindLast(3, , ThisWorkbook.Sheets(1).Range("A3:E7")))
In the function you have to change those parts where the parameters sSheet and sRange are used: Simply use the provided objects instead of creating them from the strings. 在函数中,您必须更改使用参数sSheet和sRange的那些部分:只需使用提供的对象,而不是从字符串创建它们。
With the function changed this way you can easily transport it to other host applications like Access, because the caller of the function defines the objects on which the function should operate, not the function itself. 以这种方式更改功能后,您可以轻松地将其传输到其他宿主应用程序(如Access),因为函数的调用者定义了应在其上操作的对象,而不是函数本身。
From Access you could invoke the function like this: 在Access中,您可以调用以下函数:
FindLast(3, , objExcelApp.Worksheets("t_DATA").Range(Z))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.