在 vba 中创建的最后一行之上的新行

New row on top of the last row created in vba

我有以下问题。我在所有 "E" 列单元格上都有一个 hh:mm:ss 格式的日期和时间列。问题是我尝试了以下宏来首先对最新记录进行排序,最后留下旧记录,以记录当天的所有新条目。

问题是无法正常工作。在将数据添加到新行的每个单元格之前,我也尝试过 Cells("2:2").Insert 但也没有用。我会在这里留下我的全部代码:

If Me.TextBox1.Value <> "" Then

'Encontrar el legajo en la lista
row = Me.TextBox1.Value
Set FindRow = Hoja2.Range("B:B").Find(What:=row, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
Set AddMe = Hoja3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Set AddMe2 = Hoja3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1)
Set AddMe3 = Hoja3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 2)

'Encontrar el WT en la lista
wtrow = Me.TextBox2.Value
Set FindRow2 = Hoja4.Range("D:D").Find(What:=wtrow, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
Set AddWT = Hoja3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 3)

'Agregar la fecha y hora a la celda
Status = Now
Set Estado = Hoja3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 4)

'Agregar la palabra entregado a la celda
Status2 = "Entregado"
Set Estado2 = Hoja3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 5)

With Registros
'Si lo encuentra, agregarlo a la planilla
'Range("2:2").Insert
AddMe.Value = FindRow.Offset(0, 0)
AddMe2.Value = FindRow.Offset(0, 1)
AddMe3.Value = FindRow.Offset(0, 2)
AddWT.Value = FindRow2.Offset(0, 0).Value
Estado.Value = Status
Estado2.Value = Status2
End With

'Sort by Date
Hoja3.Select
With Registros
Hoja3.Range("E2:E1000").Sort Key1:=Range("E2"), Order1:=xlDescending, Header:=xlGuess
End With

ElseIf Me.TextBox1.Value = "" Then

MsgBox "El legajo no puede estar vacio"

Exit Sub
End If

MsgBox "Los datos fueron corroborados, puede entregar el WT"

'Deja las casillas vacias cuando termina
TextBox1.Value = ""
TextBox2.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""

'error block
    On Error GoTo 0
    Exit Sub
errHandler:
   ' Verify the data entered, because they are not correct
    MsgBox "Error! Verificar los datos ingresados, porque no son correctos!" & vbCrLf

End Sub

抱歉,有些文字是西班牙语,但我只需要一个简单的代码来添加一些数据,这样当我搜索特定值时,它会搜索最后输入的值,而不是最旧的值。谢谢!

我不确定我是否理解正确。
但是,如果要根据 E 列对 整个工作表 进行排序,则需要使用整个工作表的范围,而不仅仅是 E 列。

所以它应该看起来像:

Hoja3.UsedRange.Sort Key1:=Range("E2"), Order1:=xlDescending, Header:=xlGuess

您现在所拥有的只会对 E

列进行排序

抱歉,如果这不是您的意思。