Øyvind
Øyvind

Reputation: 979

Merge cells with the following blank ones

I am writing a code for automatically reading in data from a closed workbook.

I am having some problems with the formating. The problem is when I am reading in some text in a row (each text is followed by six empty cells), and I am trying to merge the following blank cells.

For Row = 1 To NumRows
    For Column = 3 To LastColumn
        Range(Cells(Row, Column).Address, Cells(Row + 1, Column + 6).Address).Merge
    ...

This merges everything into one cell. If I have 10 cells containing text, I want ten merged cells.

Any ideas?

Upvotes: 1

Views: 1126

Answers (1)

JMax
JMax

Reputation: 26591

It does merge your whole cells because you parse 1 by 1. Meaningly, what your program do is:

  • Parse cell A1
  • Merge with the next 6 cells --> cells A1:A6 are merged
  • Parse cell A2
  • But... hey, this will try to merge cell A2 to A6 !

What you can do:

  1. Use the step option on your for loops - see here for more information
  2. Check if the cell is already merged when you parse it. If it is, then use the continue statement in your for loop

Regards,

Upvotes: 1

Related Questions