Specify last line for form filling-VBA

I am making a form and I want the person to register only 6x (only 6 lines) the content. How to limit this amount ?

insert the description of the image here

As above example, I put the rows and columns and when I fill the form, new numbers go down

    Private Sub CmdSalvar_Click()
Dim Linha As Integer

'Valor inicial da Variável Linha
Linha = 85

**Do Until ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 8).Value = Empty
    Linha = Linha + 1**

Loop
'SE o usuário não entrar com os dados
If TxtMes.Value = Empty Or TxtAno.Value = Empty Then
    ElseIf TxtLeitura.Value = Empty Or TxtConsumo.Value = Empty Then

        MsgBox ("Preencha todos os dados!")
Else
    ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 8).Value = TxtMes.Value
    ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 9).Value = TxtAno.Value
    ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 11).Value = TxtLeitura.Value
    ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 14).Value = TxtConsumo.Value
End If


End Sub

I believe it is because of the following lines:

**Do Until ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 8).Value = Empty
        Linha = Linha + 1**

But how to change to stop filling in line 90 ? (Starts at line 85 and I want to stop at 90)

Author: Danilo Alves, 2018-11-23

1 answers

You can add a conditional if to exit the function if you cross line 90.

Code

With the following code:

'Verifica se a Linha é maior do que 90
If Linha > 90 Then
    MsgBox "Você não pode mais inserir cadastros. Mensagem de aviso!"
    Exit Sub
End If

Full Code

Dim Linha As Long

'Valor inicial da Variável Linha
Linha = 85

Do Until ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 8).Value = Empty
    Linha = Linha + 1
Loop
'Verifica se a Linha é maior do que 90
If Linha > 90 Then
    MsgBox "Você não pode mais inserir cadastros. Mensagem de aviso!"
    Exit Sub
End If

'SE o usuário não entrar com os dados
If TxtMes.Value = Empty Or TxtAno.Value = Empty Then
ElseIf TxtLeitura.Value = Empty Or TxtConsumo.Value = Empty Then

    MsgBox ("Preencha todos os dados!")
Else
    ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 8).Value = TxtMes.Value
    ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 9).Value = TxtAno.Value
    ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 11).Value = TxtLeitura.Value
    ThisWorkbook.Sheets("CADASTRO").Cells(Linha, 14).Value = TxtConsumo.Value
End If
 0
Author: danieltakeshi, 2018-11-27 14:17:10