Excel用户窗体错误

我正在尝试编写一个代码来在工作表上插入项目成本。 我不是一个Excel专家,所以我使用本教程来指导我: https : //www.youtube.com/watch?v = NO10WZ2prDQ (这是葡萄牙语,但你可以看到什么UserForm在第一分钟video)

我想要做的是在工作表中插入成本,然后过滤它们以生成所有项目成本及其类别的报告。

这是我的UserForm: https : //i.stack.imgur.com/pIzwT.png

当编写插入button('Inserir Custo')时,这里是我input的内容:

Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("Plan4") iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1 ws.Cells(iRow, 1).Value = Me.ListBox1.Value ws.Cells(iRow, 2).Value = Me.TextBox1.Value Me.ListBox1.Value = "" Me.TextBox1.Value = "" Me.TextBox1.SetFocus 

Excel在代码的iRow行显示错误,但我不知道什么是错的。 单击该button时,我想让代码将ListBox1的结果插入到单元格中,并将TextBox1插入到其旁边的另一个单元格中。

有人能帮我吗? 我写错了什么?

谢谢!

 Private Sub CommandButton1_Click() Dim iRow As Long Dim ws As Worksheet Dim FoundIt As Range Set ws = Worksheets("Plan4") Set FoundIt = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues) If FoundIt Is Nothing Then MsgBox ("nothing found") Else iRow = FoundIt.Row + 1 ws.Cells(iRow, 1).Value = Me.ListBox1.Value ws.Cells(iRow, 2).Value = Me.TextBox1.Value Me.ListBox1.Value = "" Me.TextBox1.Value = "" Me.TextBox1.SetFocus End If End Sub 

问题是,如果查找函数没有得到一个匹配,它将返回一个没有值,然后你试图从任何地方得到行号..我上面的代码简化了一下,只试图获得行号,如果是一个有效的范围。

你需要在A1开始search,因为你正在从那里searchUP方向,滚动到底部并继续向上,直到遇到数据

 iRow = ws.Cells.Find(What:="*", after:=ws.range("A1"), SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1 
 Dim rw As Range, f As Range Dim ws As Worksheet Set ws = Worksheets("Plan4") Set f = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues) If Not f Is Nothing Then Set rw = f.Offset(1,0).EntireRow Else Set rw = ws.Rows(1) End If With rw .Cells(1).Value = Me.ListBox1.Value .Cells(2).Value = Me.TextBox1.Value End With Me.ListBox1.Value = "" Me.TextBox1.Value = "" Me.TextBox1.SetFocus