Help me please understand this Visual Basic command

S

Steve

I saw this in another thread and need someone to please explain with an
example how to use this code:

Print selection or range with one or more areas with a macro.

The macro will add a new sheet and copy all the selection areas on it.
Then it will print and delete the sheet.

Sub Test()
Dim Destrange As Range
Dim Smallrng As Range
Dim Newsh As Worksheet
Dim Ash As Worksheet
Dim Lr As Long

Application.ScreenUpdating = False

Set Ash = ActiveSheet
Set Newsh = Worksheets.Add
Ash.Select

Lr = 1

'You can also use a range with more areas like this
'For Each smallrng In Ash.Range("A1:C1,D10:G20,A30").Areas

For Each Smallrng In Selection.Areas
Smallrng.Copy
Set Destrange = Newsh.Cells(Lr, 1)
Destrange.PasteSpecial xlPasteValues
Destrange.PasteSpecial xlPasteFormats
Lr = Lr + Smallrng.Rows.Count
Next Smallrng

Newsh.Columns.AutoFit

Newsh.PrintOut

Application.DisplayAlerts = False
Newsh.Delete
Application.DisplayAlerts = True

Application.ScreenUpdating = True

End Sub



I understand how to name ranges but not sure from above what I am supposed
to name what. I have three ranges that I want to print.

Thanks!!!
 
D

Duke Carey

this code doesn't look for named ranges, it is using the selected areas,
though the comment points out you could use specific addresses representing 3
different ranges:

A1:C1,D10:G20,A30

So...to make this code work, simply select the areas you want copied and
THEN run the macro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top