Macro IF > 0, adds 1 to cell

Craig Farish

New Member
Joined
Apr 8, 2011
Messages
5
Hi all
I am fairly new to writing macros and am completely baffled by what you guys know. :eeek:
I have been reading for hours on various websites etc for something that i could manipulate into what i need but whatever i attempt to do so it fails.
Example:
I have data in a column A,the number of rows in column A varies from from workbook to workbook. I want to loop through the data in this column and whenever the data is greater than 0 i want to add 1 to a specific cell (C4)
The logic seems very simple but i just have no idear how to write this in code.

Any suggestions will be greatly appreciated.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I need a macro to run this code in many columns and list the number of times the data is greater than 0 in varying cells.

The example has been simplified to make it clear.
 
Upvote 0
Hi and welcome to the Board

Try
Code:
sub adder()
Dim r As Long, lr As Long, c As Integer
lr = Cells(Rows.Count, "A").End(xlUp).Row
c = 0
    For r = lr To 1 Step -1
        If Range("A" & r).Value <> 0 Then
            Range("C4").Value = c + 1
            c = c + 1
        End If
    Next r
    End Sub
 
Upvote 0
Hi Steve.
I agree about the COUNTIF....but I I'm imagining that Craig has probably got thousands of lines that vary in number, so the formula would have to change constantly.
Using a snippet might just save him the search down the column, so to speak.
 
Upvote 0
Thanks for your replies!

I have lots of data to sort and hundreds of workbooks to get through, hence the need for the macro.

My intension is to open all the workbooks and run the macro to input the data into specific cells which can then be read by a master workbook via a vlookup. Never really done anything like this before so its kind of experimental. Fingers crossed.

Thanks again guys!
 
Upvote 0
Craig
Is this being done to lots of worksheets in each workbook ??
 
Upvote 0
Sorry

I have lots of .csv files where i need the macro to run and store the data it reads into a specific cell. e.g

Cell G2 will hold the qty of cells will 0 in column A
Cell G3 will hold the qty of cells will 0 in column B
Cell G4 will hold the qty of cells will 0 in column C
Cell G5 will hold the qty of cells will 0 in column D
Cell G6 will hold the qty of cells will 0 in column E

I will have a master .xslx that will look at each .csv file and read the specific cells (G2,G3,G4,G5,G6). This will enable me to report on all the .csv files and how much missing data i have across them all.

Well thats the idea :laugh:
 
Upvote 0
Ok i used the CountA & Count IF within a macro and it works perfectly.

Range("O3").Select
ActiveCell.FormulaR1C1 = "Entries"
Range("O4").Select
ActiveCell.FormulaR1C1 = "Mapprod"
Range("O5").Select
ActiveCell.FormulaR1C1 = "Weight"
Range("O6").Select
ActiveCell.FormulaR1C1 = "Cost"
Range("O7").Select
ActiveCell.FormulaR1C1 = "Erate"
Range("P3").Select
ActiveCell.FormulaR1C1 = "=COUNTA(C[-3])-1"
Range("P4").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-10],""#N/A"")"
Range("P5").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-7],0)"
Range("P6").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-5],0)"
Range("P7").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-3],0)"

On to the next part :)

Thanks for your replies.
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,794
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top