在 sub 中使用函数(VBA 使用 Word 和 Excel)
using a function inside a sub (VBA using Word and Excel)
我对在私有子(命令按钮)中使用私有函数有疑问。
它不会 return 任何错误...也不会执行任何操作。当我在 Word 文档中按下命令按钮时,它应该形成行并从 Excel 工作表导入数据,直到 Excel 中的最后一行,这就是我试图获得的功能do - 找到工作表中的最后一行。
如果您可以查看我的代码并告诉我为什么它不起作用,我将不胜感激。我是否需要在 Private Sub commandbutton_2_Click() 中使用函数?提前谢谢你。
Private Sub CommandButton2_Click()
Dim tbl As Table
Dim row As row
Set tbl = ActiveDocument.Tables(2)
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
On Error Resume Next
Set exWb = objExcel.Workbooks.Open("S:\Electro-Protocol\Mot_Protocols\" & TextBox1 & ".xls")
Dim lastRow As Integer
lastRow = GetLastRow(objExcel, exWb)
ActiveDocument.Tables(2).Columns.DistributeWidth
For counter = 1 To lastRow
tbl.Rows.Add
tbl.cell(counter, 1).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 1)
tbl.cell(counter, 2).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 2)
tbl.cell(counter, 3).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 3)
tbl.cell(counter, 4).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 4)
tbl.cell(counter, 5).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 5)
tbl.cell(counter, 6).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 6)
Next counter
End Sub
Private Function GetLastRow(ByVal objExcel As Excel.Application, ByVal exWb As Excel.Workbook) As Integer
Dim lastRow As Integer
lastRow = 0
With exWb.Sheets("Tabelle1")
If objExcel.WorksheetFunction.CountA(.Cells) <> 0 Then
lastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).row
Else
lastRow = 1
End If
End With
End Function
在 End Function
之前,您需要这样做:
GetLastRow = lastRow
否则该函数没有 return 值。
我对在私有子(命令按钮)中使用私有函数有疑问。
它不会 return 任何错误...也不会执行任何操作。当我在 Word 文档中按下命令按钮时,它应该形成行并从 Excel 工作表导入数据,直到 Excel 中的最后一行,这就是我试图获得的功能do - 找到工作表中的最后一行。
如果您可以查看我的代码并告诉我为什么它不起作用,我将不胜感激。我是否需要在 Private Sub commandbutton_2_Click() 中使用函数?提前谢谢你。
Private Sub CommandButton2_Click()
Dim tbl As Table
Dim row As row
Set tbl = ActiveDocument.Tables(2)
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
On Error Resume Next
Set exWb = objExcel.Workbooks.Open("S:\Electro-Protocol\Mot_Protocols\" & TextBox1 & ".xls")
Dim lastRow As Integer
lastRow = GetLastRow(objExcel, exWb)
ActiveDocument.Tables(2).Columns.DistributeWidth
For counter = 1 To lastRow
tbl.Rows.Add
tbl.cell(counter, 1).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 1)
tbl.cell(counter, 2).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 2)
tbl.cell(counter, 3).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 3)
tbl.cell(counter, 4).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 4)
tbl.cell(counter, 5).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 5)
tbl.cell(counter, 6).Range.Text = exWb.Sheets("Tabelle1").Cells(counter, 6)
Next counter
End Sub
Private Function GetLastRow(ByVal objExcel As Excel.Application, ByVal exWb As Excel.Workbook) As Integer
Dim lastRow As Integer
lastRow = 0
With exWb.Sheets("Tabelle1")
If objExcel.WorksheetFunction.CountA(.Cells) <> 0 Then
lastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).row
Else
lastRow = 1
End If
End With
End Function
在 End Function
之前,您需要这样做:
GetLastRow = lastRow
否则该函数没有 return 值。