Help creating a proper loop

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like a user to select rows in a worksheet called "Inventory" (they
will be non-continuous) and then create a For..Next loop (or whatever works
best) based on the rows (or just row number), so I can copy and format select
cells from each row in another worksheet called "Labels". I'm not sure how
to do this, either with an array or by looping thru the rows selected. Also,
I need a check so if nothing has been selected they get an error message.

For the selections, will the user need to select the entire row (1:1, 5:5,
7:7, etc) or if they just ctrl select A1, A5, A7, A11, etc will this work too?

Thanks in advance!
 
No need for a loop:

Selection.EntireRow.Copy worksheets("Labels").Range("A2")
Application.CutCopyMode = False

The entire rows need not be selected.
_______________________________________________________________________
 
Thanks Vasant, but I don't want to copy all the contents from each row, just
certain cells and then reformat them on a new worksheet.
 
I guess I would do the following (assuming the first cell in each relevant
row is selected):

Dim c As Range, rng As Range, newrng As Range
For Each c In Selection.Cells
Set rng = c.Resize(, 5) 'this will encompass the first 5 cells in the
row, for example
If newrng Is Nothing Then
Set newrng = rng
Else
Set newrng = Union(rng, newrng)
End If
Next
newrng.Copy worksheets("Labels").Range("A2")
Application.CutCopyMode = False

Not tested so not 100% sure it will work as you want it to.
_________________________________________________________________________
 

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

Back
Top