How do I use For loop to pick different Range of cells to Select & Merge?

  • Thread starter Thread starter chiendh
  • Start date Start date
C

chiendh

Hi!
I'm trying to loop through Ranges of Cells to select & merge them
together.
I can do this by hand, and I can do this if I hardcode the cell
row&column values.

BUT, I can't seem to figure out how to do this programatically!

Thanks!
David

-----------------sample code--------------------

For datastartrow = 5 to 50
rem datastartrow is the row I'm on

ActiveSheet.Range("B13:H13").Select
ActiveSheet.Range("B13:H13").Merge
rem == Hardcoding works, but... I need to loop through the rows....

rem == HOW DO I CHANGE this to something I can loop through?
rem == eg. this doesn't work for some reason:
rem == ActiveSheet.Range(Cells(datastartrow, 2),
Cells(datastartrow, 9)).Select
rem == ActiveSheet.Range(Cells(datastartrow, 2),
Cells(datastartrow, 9)).Merge

With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With

Next i
 
dim myRow as long
for myRow = 5 to 50
activesheet.cells(myrow,"B").resize(1,7).merge
next myrow

Or without looping

activesheet.range("B5:H50").merge across:=true
 
activesheet.cells(myrow,"B").resize(1,7).merge

Thanks for the quick answer! That helped me after days of
searching for an answer!!
 

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