Excel form - incompatible types

0

I know this should be a simple question to answer, but I'm starting to program in VBA now. The code below should transfer the data from one worksheet to another, it should be something very simple, but it gives compilation error.

Sorry if I used any commands that do not exist in VBA, but I'm really here to learn.

( Code is associated with a button )

Sub Submit()

Dim rLast As Long

With Sheets("Plan2")
 .Cells("B", 2).Select.Copy

End With  


With Sheets("database")

  'Obtém a última linha da Planilha:
   rLast = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
  .Cells(rLast, 1).Select.Paste


End With
End Sub
    
asked by anonymous 11.09.2017 / 23:40

2 answers

1

When using .Cells you should only use rows and columns numerically and using with you should always leave the commands in separate rows as below.

Another correction in your formula is that you are using copy for a Sheets() worksheet.

Your function will work with the following fixes:

Sub Submit()

Dim rLast As Long

    Sheets("Plan2").Range("B2").Copy

    With Sheets("database")
        'Obtém a última linha da Planilha:
        rLast = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
        .Cells(rLast, 1).Select
        .Paste    
    End With
End Sub
    
12.09.2017 / 11:42
1

Its syntax of .Cells is incorrect, where the correct one is .Cells(Linha, Coluna) . There is no need to use only numbers, because lastrow = db.Cells(db.Rows.Count, "A").End(xlUp).Row also returns the last line of the "database" worksheet

But there is a simple and practical way to copy and paste

'Declaração de variáveis, para duas ou mais cada Data Type deve ser escrito novamente
'Caso não seja escrito, a mesma é declarada como Variant
Dim db As Worksheet, ws2 As Worksheet

Set db = ThisWorkbook.Sheets("database")
Set ws2 = ThisWorkbook.Sheets(2)
'Pode usar .Sheets ou .Worksheets, com o nome entre "" ou com o número de index
rLast = db.Cells(db.Rows.Count, 1).End(xlUp).Row + 1

ws2.Range("B2").Copy Destination:=db.Cells(rLast, 1)

Where the Range.Copy method can be seen in the link and example This page has been used.

    
13.09.2017 / 20:01