如何在最后一行插入公式

how to insert formula in the last row

我创建了一个用户窗体来登记患者并且它工作正常。

我尝试向其中添加一个公式以连接 F 列中的第一个中间姓氏和第二个姓氏,但它给了我一个错误。我无法通过它。有什么建议吗?

Private Sub CommandButton1_Click()Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Lista Pacientes")


'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1


'check for a Valid patient name
If Trim(Me.TextBox1.Value) = "" Then
  Me.TextBox1.SetFocus
  MsgBox "Favor Introducir Nombre"
  Exit Sub
End If

    With ws

  .Cells(iRow, 2).Value = Me.TextBox1.Value
  .Cells(iRow, 3).Value = Me.TextBox2.Value
  .Cells(iRow, 4).Value = Me.TextBox3.Value
  .Cells(iRow, 5).Value = Me.TextBox4.Value
  .Cells(iRow, 7).Value = Me.TextBox5.Value
  .Cells(iRow, 8).Value = Me.TextBox6.Value
  .Cells(iRow, 10).Value = Me.TextBox7.Value
  .Cells(iRow, 11).Value = Me.TextBox8.Value
  .Cells(iRow, 12).Value = Me.TextBox9.Value
  .Cells(iRow, 13).Value = Me.TextBox10.Value
  .Cells(iRow, 14).Value = Me.TextBox11.Value
  .Cells(iRow, 15).Value = Me.TextBox12.Value
  .Cells(iRow, 16).Value = Me.TextBox13.Value
  .Cells(iRow, 17).Value = Me.ComboBox1.Value
  .Cells(iRow, 6).Formula = "=CONCATENAR(LIMPIAR(ESPACIOS(B" & iRow & "));SI(LIMPIAR(ESPACIOS(C" & iRow & "))="""";"""";"" "");LIMPIAR(ESPACIOS(C" & iRow & "));SI(LIMPIAR(ESPACIOS(D" & iRow & "))="""";"""";"" "");LIMPIAR(ESPACIOS(D" & iRow & "));SI(LIMPIAR(ESPACIOS(E" & iRow & "))="""";"""";"" "");LIMPIAR(ESPACIOS(E" & iRow & ")))"

End With


'clear the data
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Me.TextBox7.Value = ""
Me.TextBox8.Value = ""
Me.TextBox9.Value = ""
Me.TextBox10.Value = ""
Me.TextBox11.Value = ""
Me.TextBox12.Value = ""
Me.TextBox13.Value = ""
Me.ComboBox1.Value = ""
Me.TextBox1.SetFocus


Unload Me


End Sub

我在这条明显的行中收到运行时错误 1004

.Cells(iRow, 6).Formula = "=CONCATENAR(LIMPIAR(ESPACIOS(B" & iRow & "));SI(LIMPIAR(ESPACIOS(C" & iRow & "))="""";"""";"" "");LIMPIAR(ESPACIOS(C" & iRow & "));SI(LIMPIAR(ESPACIOS(D" & iRow & "))="""";"""";"" "");LIMPIAR(ESPACIOS(D" & iRow & "));SI(LIMPIAR(ESPACIOS(E" & iRow & "))="""";"""";"" "");LIMPIAR(ESPACIOS(E" & iRow & ")))"

公式为西班牙语,对应的英语为

CONCATENAR = CONCATENATE
LIMPIAR = CLEAN
ESPACIOS = TRIM
SI = IF

由于语言问题,它无法正常工作,已更改

.Cells(iRow, 6).Formula

.Cells(iRow, 6).FormulaLocal

很有魅力