How to calculate number of used columns per row in excel 2007 vba - vba

I want to calculate number of used columns for each row in excel 2007 using VBA.
I am calculating number of used rows by using ActiveSheet.UsedRange.Rows.Count
and now I want that, i just pass cells(1,1).value to a function that give me total number of used column for that specific row.
Thanks

Below example for column A:
WorksheetFunction.Sum(Sheet1.Range("A1:A" & ActiveSheet.UsedRange.Rows.Count ))

Related

VBA: Is there a way to apply a single formula to a column without looping through each cell

I'm quite new in VBA and was looking for ways on how to apply the same formula to all cells in a column. I have 5000 rows and I would simply like to multiply row values in Column 1 with row values in Column 2 and show the results of the same row in Column 3.
I have tried looping through each of the cells:
For i = 1 to 5000
Next i
but the calculation was slow and was wondering if there is another way to calculate all 5000 rows quicker.
I actually have 60 columns with different formulas and conditional statements which slows down the calculation process. I am actually looking for a simpler way to 'BULK' apply a certain formula to the whole column.
The following code makes Column1*Column2=Column3 using Formula property of Range object without a looping
Sheet1.Range("C1:C5000").Formula = "=RC[-2]*RC[-1]"

Excel VBA - Looping to count cell values larger than / smaller than specific value

I'm currently working with a large amount of contract lists. Each list is in a separate worksheet for each year's quarter and it needs to stay that way. E.g.: 2007_Quarter1, 2007_Quarter2, etc. I have 10 years of data, so 40 quarter report worksheets.
Now I need to code a vba routine that would do the following, in the specified order:
In each quarter report worksheet, run through the column containing contract values; then
Count all values between 0 and 10,000; then
Count all values between 10,001 and 25,000; and so on
Then, when there are no more contract values in the range (which will vary from sheet to sheet), go to the next worksheet and repeat the procedure.
All results should be returned in a worksheet (let's call it Worksheets("Report")) in a table where rows are for value intervals (e.g. 0 to $10,000) and columns are for quarters (e.g. 2007 Q1, 2007 Q2, etc.).
One particular aspect of my problem is that, for practical reasons, I'd prefer to set the control intervals' min and max values in a table contained in another worksheet ("Variables"), where, for example, I'd have all minimum values in range C4 down and all maximum values in range D4 down.
For each contract value, an "If" condition should look like:
If ContractVal > Worksheets("Variables").Range("C4").Value And
If ContractVal < Worksheets("Variables").Range("D4").Value Then ...
So far I'm having no success at coding this efficiently. I suspect some loop would work but I can't find a way to make it happen. A loop would do:
In 2007_Quarter1 ws:
For each cell from A4 down to the end, count values included between Range("C4").Value and Range("D4").Value
Then for each cell from A4 down to the end, count values included between Range("C5").Value and Range("D5").Value
... and so on, then repeat for 2007_Quarter2, 2007_Quarter3, and so on.
I need rescue! Thanks in advance!
If it is ok to you to prepare the resulting matrix manually (intervals in rows and quarters in columns) you could write a macro that read the right file and count the contracts in the right range for every cell in the matrix.
If you want to build the matrix automatically you should tell the macro the year range. Then you can write a loop that read the ranges and for every range you write a loop to read every file in the right order.

Using Bloomberg BDH formula for several cells in a VBA Macro

Do you think it is possible to use the BDH formula but instead of using the name of one security we use the information within the cells for instance (assume that on the column A you have the ISIN of the securities and I want the price on column C at a certain date, date available in the column B):
for I = 1 to 10
cells(i+1,3) = BDH(cells(i+1,1),"PX_LAST", cells(i+1,3), cells(I+1,3), "Period, Dates", "M,H")
next I
Yes, you can. If the ISIN is stored in cell A1 you can use this formula:
BDH(A1 & " ISIN", "PX_LAST", "8/25/2016", "8/25/2016")
You can also replace the dates with a reference to a cell that contains the date.

Copying values across a dynamic column and row range

I have the following code that:
copies the column headings (in row 1) from column C across to the second last column
pastes these column headings in row 1 in the column 2 across from the last column with data
pastes the column headings alongside each row of data through to the bottom row
Sub GLDR()
'use End(xlUp) to determine Last Row with Data, in column A of the GLDRYYPP tab
Dim lastRowDR As Long
lastRowDR = Sheets("GLDRYYPP").Range("A" & Rows.Count).End(xlUp).Row
'copy the cost type categories and paste alongside the cost centres
CTNameCol = "S2:AF" & lastRowDR
Sheets("GLDRYYPP").Range("C1", Range("C1").End(xlToRight).Offset(0, -1)).Copy
Sheets("GLDRYYPP").Paste Destination:=Sheets("GLDRYYPP").Range("C1").End(xlToRight).Offset(0, 2)
Sheets("GLDRYYPP").Range(Range("C1").End(xlToRight).Offset(0, 2), Range("C1").End(xlToRight).Offset(0, 2).End(xlToRight)).Copy
Sheets("GLDRYYPP").Paste Destination:=Sheets("GLDRYYPP").Range(CTNameCol)
End Sub
The first two steps have been set to be dynamic for any additional columns added but I am having trouble writing some code that will paste the data through to the bottom row. As you can see the range "S2:AF(last row)" has been written to make use of the result from the lastRowDR dimension.
Is there a way to write the code which will make the copy dynamic across the columns and rows?
Yes there is a way to write the code which will make the copy dynamic across the columns and rows. What you need to do is to identify a distinct quality about the cost centres so that you locate it using something like ActiveSheet.Cells.Find(Txt).
Then you'll be able to determine what CTNameCol should be. There is some helpful info here: Range.Find Method.
If you add add a picture of your spreadsheet I will update this answer with more precise information.

how to count cells in excel with conditional statement?

I want to create a VBA script that can count the number of cells in two columns , for example column A and B, that have values "yes". Moreover, if one of the two cells or both in the same row have value "yes", I want the script to count only 1 time.
For example, if A2 and B2 have value "yes", it is counted 1 time. If A2 or B2 has value "yes", it is also counted 1 time.
Please suggest a solution.
Try this formula.
=SUMPRODUCT((--((A:A="yes")+(B:B="yes"))>0)*1)
I would not advise using whole column references with sumproduct, though, since with over a million rows in Excel 2007 and later, calculation might take a while. You may want to build range names that grow and shrink with the populated rows and use these instead of the column references.
Note that whole column references in SumProduct only work in Excel 2007 and later. For earlier versions of Excel you will need to ringfence a range.