Loop through column counting cells that are not blank - excel

I am trying to loop through a column and count the number of cells that are not blank. The process that it must follow are:
Start at first cell and count non blank cells until you get to two consecutive blank cells
STOP at this point and return the value of the count to a cell on the sheet
Start Count again at next non blank cell and repeat 1. and 2. until you have reached the end of the data
Extra: if I could also return the date in the row that relates to the first non blank and also the first blank (from the point at which you find the first two blank cells)
If you need more information please ask. I presume VBA would be much better at doing this?
Thanks.
Code so far is:
Sub Test1()
Range("I3").Select
Do Until IsEmpty(ActiveCell) And IsEmpty(ActiveCell.Offset(1, 0))
Dim iVal As Integer
iVal = Application.WorksheetFunction.CountIf(Range("I:I"), "TRUE")
ActiveCell.Offset(2, 0).Select
Loop
End Sub

As you mention I presume VBA would be much better at doing this? I am assuming you would contemplate a formula solution.
Assuming data is in ColumnA starting with a single blank cell, in B2:
=IF(AND(ISBLANK(A2),ISBLANK(A3)),COUNTA(A$1:A2)-SUM(B$1:B1),"")
dragged down until 0 appears.

Related

How can I prevent a merged cell, in a Do While loop, from moving my selected cell from one column to another in VBA?

I receive excel files from a bank that contains information that I need in a certain column. So I want to loop through the specific column and get the values of those cells. Lets say I select column B. I start at B1 and loop through the column cells. But once I get to a merged cell, which there are quite a lot of, the merged cell throws me off of column B when I try to move past it. I'm using Offset(1, 0) to go down the column.
'Here is a quick example of how the selected cell will move
'I'm using an index to move down 15 cells
'Merge cell A2 and B2 before running the macro
Sub test()
Dim index As Integer
index = 0
Range("B1").Select
Do While index < 15
Selection.Offset(1, 0).Select
index = index + 1
Loop
End Sub
The selection moves from B1 to B2, which is merged with A2, then continue to A3 instead of B3.
Using Select is not a best practice, but if you need it for some visual reasons, the code below would work. It gets a starting cell startingRange and each time it loops one row down from it - startingRange.Offset(rowoffset:=index).Select
Sub TestMe()
Dim index As Long
index = 0
Dim startingRange As Range
Set startingRange = Worksheets(1).Range("B1")
Do While index < 15
startingRange.Offset(rowoffset:=index).Select
Application.Wait (Now + #12:00:01 AM#)
index = index + 1
Loop
End Sub
The Application.Wait (Now + #12:00:01 AM#) is added in order to visualize the Seleced cell better.
You should do this without using Select, VBA can address any cell in your workbook without selecting it first and find out what value it contains. There are doubtless different ways of doing what you want to achieve, but this example explains how to move through a column without it deviating:
Sub test()
For Each Cell In Range("B1:B14").Cells
'Finds the MergeArea of the Cell and gets the value from the top left cell
MsgBox Cell.MergeArea.Cells(1, 1).Value
Next
End Sub
When you run the sub it'll fire off Message Boxes containing the values of the cells in column B, rows 1-14, including those that are merged.
This is probably an incomplete answer, your final answer may be a mixture of the code in this example plus the code in your original question.

How to find a cell with any value and then find the next value that is different in VBA

I have a list of data with various different columns.
What I am trying to achieve is for excel to look through one of the columns and find the first value that occurs, copy that cell and then paste it in another cell. The data starts in row three (Rows 1 and 2 are headers). Let's say I want to paste the first value in G3.
Then, I need excel to find the next value that is different from the first in the same column and perform the same action as before: copy the value, and paste it in the next row in column G.
I have tried coding this but I'm not getting anywhere with it, and I haven't found a way to find a cell and then the next cell if the value within the cell is not defined (as the .Find function requires a value to search for). I know how to code the copy/paste functions but I cannot figure out how to get it to find the cell in the first place.
Any help would be much appreciated. Many thanks in advance.
In this sample, we are looking for values in column A:
Sub FillG()
Dim i As Long
i = Rows.Count
Range("A3:A" & i).Copy Range("G3:G" & i)
ActiveSheet.Range("G3:G" & i).RemoveDuplicates Columns:=1, Header:=xlNo
If Range("G3").Value = "" Then Range("G3").Delete Shift:=xlUp
End Sub
The method is to copy all of column A to G, then remove duplicates, then remove an empty in G3 if it exists.:

VBA to Sum a Column from a fixed cell up until the first blank cell

I have a column of numeric data, where I need to sum the values from a specified cell, down to the last value before the first blank cell in the Column. I have used a Range function previously to complete this, but on this occasion the number of rows before the blank cell is unknown and cannot be defined in a range. the simple explanation is I need the result in cell A1 to be
=sum (A6:AX)
where X is one before the first blank cell.
I will then write this into my VBA loop to complete it for nth columns over nth sheets.
Use the following sub:
Sub SumTillBlankCell()
Dim BeforeFirstBlankCell
BeforeFirstBlankCell = Range("A6").End(xlDown).Row
Range("A1").Value = Application.Sum(Range("A6:A" & BeforeFirstBlankCell))
End Sub

Excel VBA Clear Contents

I have a column in excel sheet that contains the IF condition i.e.
=If(Cond 1 is TRUE, X, Y)
Now, after using this condition, i get certain values in the column. Following format can be considered (these are actual values):
4L
4L
4L
4L
Note: The two empty cells in the above col are an outcome of the TRUE condition(entry 4 and 5, i entered total 6 entries, two are empty cells ). Therefore, they are valid. (let me call the above col "A" for future reference)
Now, these empty cells actually contains formulas (the if condition). I want to CLEARCONTENT them using VBA but it is not working.
And I'm trying the below code:
If ActiveSheet.Cells(row_no, col_no) = "" Then
ActiveSheet.Cells(row_no, col_no).ClearContents
End If
But this does not work. I just want to CLEAR CONTENT those empty cells by running a loop over the whole column. The cells of the column where TEXT exist (i.e. 4L), that should be skipped but as soon the code encounters the EMPTY CELL (which actually have an IF condition), it should CLEAR CONTENT it and complete the loop. So the final result would be again the same as column "A", the only difference would be that the empty cells will be BLANK now i.e. they will not have any IF condition.
I do not have problem running the loops but i am not getting how to tell VBA about that empty cell and clear contenting it. Hopefully i was able to post a clear query. Thanking in advance.
Regards
Nayyar
Please try the below sample code it should work..
Sub test()
'Assuming your data in column A from A2
Set Rng = Range("A2", Cells(Rows.Count, 1).End(xlUp))
For Each cell In Rng
If cell.Value = "" Then
cell.ClearContents
End If
Next
End Sub
And in your formula =If(Cond 1 is TRUE, X, Y) your not giving any output like "" which will give you blank. Please update it and then try :)
Try this as well
If (Range("A35").Value = "") Then
Range("A35").Formula = ""
End If

Insert blank rows in excel if row above is not empty

If A1 is not empty (contains actual data), is there a way to if-then so that I can insert a blank row under A1. This should apply to any cell/row as I have a spreadsheet with about 65000 rows and they want to have blanks to separate the rows that contain different identifiers. I looked on here, right before I signed up, and I saw some things about empty cells or making cells empty depending on other cells, but that doesn't seem to be what I'm looking for. Google wasn't too much help either.
thanks.
Is this what you want?
Sub helping()
Dim count As Long
For count = ActiveSheet.UsedRange.Rows.count To 1 Step -1
If Information.IsEmpty(Cells(count, 1)) = False Then Rows(count + 1).Insert
Next count
End Sub

Resources