Jump to content

Como usar while em VBA


Leonardo_Augusto
 Share

Recommended Posts

Olá ! Preciso muito da ajuda de vocês,

meu código abaixo lê os dados de uma planilha ,seleciona este e manda imprimir. Mas no momento eu precisaria repetir o código para funcionar pra cada cadastro(pessoa cadastrada).

exemplo : Range("A5").Select ....

Range("A6").Select ...... até o Range("A50").Select

Lembrando que os dados podem aumentar ..

Como posso otimizar para não precisar repetir o código abaixo para cada range?

Sub Macro1()

' Macro1 Macro

' Atalho do teclado: Ctrl+i

Sheets("Dados Pessoais").Select

Range("A5").Select

Selection.Copy

Sheets("Folha de Pagamento").Select

Range("I3").Select

ActiveSheet.Paste

Application.CutCopyMode = False

Activewindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _

IgnorePrintAreas:=False

Sheets("Dados Pessoais").Select

Range("A6").Select

Selection.Copy

Sheets("Folha de Pagamento").Select

Range("I3").Select

ActiveSheet.Paste

Application.CutCopyMode = False

Activewindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _

IgnorePrintAreas:=False

End Sub

Link to comment
Share on other sites

Leonardo_Augusto,

Boa Tarde!

Você pode tentar assim:

Sub Macro1()
Dim i As Long
Dim UltimaLinha As Long

UltimaLinha = Sheets("Dados Pessoais").Cells(Cells.Rows.Count, 1).End(xlUp).Row

' Macro1 Macro
' Atalho do teclado: Ctrl+i

For i = 5 To UltimaLinha
 Sheets("Dados Pessoais").Range("A" & i).Copy Destination:=Sheets("Folha de Pagamento").Range("I3")
 Activewindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
Next

Application.CutCopyMode = False
End Sub
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.