Share via

VBA to loop through multiple columns

Anonymous
2011-11-02T19:16:06+00:00

I am using a routine that loops through cols A and E with the following loop

Sub updater()

While ActiveCell.Value <> "" And ActiveCell.Offset(0, 4).Value <> ""

 Application.run "sub1"

Application.run "sub2"

Application.run "sub3" etc

Wend

End sub

I have another routine that, at the end of this loop, deletes column E and closes up the gaps in the 'new' column E.

The problem is that the above loop will, of course, only run until the last line of data in columns  A  and E is reached, but I need to modify this to loop through cols E:CC (77 columns in all)

As I have the subroutine for closing up gaps in each column ( 'closeupgaps), I would like to remove the subroutine I have for deleting column E and instead loop through these 77 columns and close all gaps in the data.

How should I write the loop to cycle through E:CC?  (so that all gaps in rows are removed - assuming I'm starting as above from Column A)

As a guess, is it something like this?

Sub closegapsEtoCC()

While ActiveCell.Value <> "" And ActiveCell.Offset(0, 76).Value <> ""

Application.run "closeupgaps"

Wend

End sub

Many thanks.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-11-02T19:28:18+00:00

What you suggest might work. WIthout seeing the Closeupgaps macro, it is hard to give any usefull advice.

You say that you want to remove gaps. If gaps are empty cells in the columns, then I would suggest this:

Sub RemoveGaps()

Columns("E:CC").SpecialCells(xlCellTypeBlanks).Delete

End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2011-11-02T20:26:42+00:00

    Thanks - that's really great - very helpful!

    What you suggest is a much more elegant solution than I had in mind as it will do what I want in one go without the need for a loop.

    Was this answer helpful?

    0 comments No comments