• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Simple macro to find the next blank column in the range

Hi All,

I have data for same month in multiple columns in a continuous range.

Each month range is separated by a blank column.

What I want is a macro that find the next blank cell in column starting cell B10 and autosum the columns preceding the blank cell.

I want autosum in highlighted columns in yellow (must be a loop).

upload_2016-9-29_23-18-3.png

I started writing the code but stuck:

Code:
Sub sum()
ActiveSheet.Range("b10").Select
Selection.Columns.End(xlToRight).Offset(0, 1).Select
ActiveCell.Value = "=Sum(columns.end (xltoleft)"
End Sub

Many thanks
Sophia
 
Last edited by a moderator:
Hi Sophia ,

You cannot have a loop which merely autosums columns preceding a blank cell ; when you start the loop from B10 , execution will find that D10 is blank , and sum B10 and C10 correctly.

When it encounters the next blank cell , it is G10 , but now the summing will be done of B10 , C10 , D10 , E10 and F10 since these are all columns preceding a blank cell.

If you can confirm that every blank cell has to be filled in with the sum of the preceding 2 column cells , then there is no chance of any problems.

You can try the following code :
Code:
Public Sub InsertSums()
           With ActiveSheet
                .Range("A8").Offset(, .Columns.Count - 1).End(xlToLeft).Offset(, 1).Select
                Do While ActiveCell.Column > 2
                   With ActiveCell
                        .Offset(2).Value = .Offset(2, -1).Value + .Offset(2, -2).Value
                        .Offset(4).Value = .Offset(4, -1).Value + .Offset(4, -2).Value
                        .Offset(, -3).Select
                   End With
                Loop
           End With
End Sub
Narayan
 
Back
Top