0

I am a beginner in coding and I was doing a course of VBA, when suddenly while trying to do an excersice this error prompted: error 1004 in the method of 'Range' of the object 'Global' so I was wondering if someone could give me a hand.

Thanks for reading, and sorry if your eyes hurt with the code.

Private Maxima As Integer

Private Sub lblCódigoClientes_Click()

End Sub

Private Sub UserForm_Initialize()
    Inicializo
End Sub

Public Sub Inicializo()

Dim Rango As Range
Dim Maxima As Range

UltimaFila = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

txtNombre = ""
txtApellido = ""
txtTelefono = ""
txtEmail = ""

If UltimaFila = 5 Then
    lblCodigo.Caption = 1
    Exit Sub
End If

Set Rango = Range("B6:B", Trim(Str(UltimaFila)))

Set Maxima = Rango.Find(Application, WorksheetFunction.Max(Rango), LookIn:=xlValues, LookAt:=xlWhole)

valor = Maxima.Value
lblCodigo.Caption = valor + 1

End Sub
  • 1
    `Range("B6:B", Trim(Str(UltimaFila)))` ---> `Range("B6:B" & UltimaFila)` – BigBen Oct 12 '21 at 18:25
  • 1
    Side note: `UsedRange` and `xlCellTypeLastCell` are unreliable. See [this](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-excel-with-vba) for a better way to find the last cell. – BigBen Oct 12 '21 at 18:25
  • Also, `valor = WorksheetFunction.Max(Rango)` – CDP1802 Oct 12 '21 at 19:42
  • On a side note: WHY? `Private Maxima As Integer` and `Dim Maxima As Range`? – Siddharth Rout Oct 12 '21 at 20:12
  • Thank you for all the comments, I can't really explain the why of the things, as I was following a course, which resembles more a "guided project" more than a course itself. Again, I'm very grateful for all of your help, my knowledge of programming is really basic. Thanks! – Geremías Lovera Oct 12 '21 at 20:48

0 Answers0