I have a series of Excel files all in the same format. The amount of data in each excel file differs. I am making a code that will loop through a folder and perform calculations with all excel files.
I am using the following code in VBA to determine the number of rows in an excel file:
i = .Cells(.Rows.Count, 2).End(xlUp).Row
I am using the following formula to perform calculations on a range of cells:
With .Range("D2:D" & i)
.Formula = "=Log(B2)"
End With
How would I calculate the sum of all values in the "D" column in the next available cell in D? This is how the formula would look in theory
j = i + 1
With .Range("D" & j)
.Formula = "=Sum(D2:D & i)"
End With
How would I use the D cell with the sum for future calculations? Lets say I wanted E1 "=(D2-$D$ & j)^2"
Sorry for the vague title, I didn't know how to describe this problem.
As follow up from comments, this one works:
With .Range("D" & j)
.Formula = "=SUM(D2:D" & i & ")"
End With
Related
I am currently building a macro for Excel in order to read hundreds of txt files. For now everything work well and each txt file has its sheet.
I would like to take the value of a precise cell in each sheet (that would always be at the same place) and put it in a created sheet.
It is the value of the cell that I can't get to work, because I don't know how to say in VBA "this cell equal the value of this cell in this sheet, without any R[]C[]."
The naming of the sheets is precise and in function of a concatenated loop of 3 parameters.
Here is what i have done (the sheet result is created at the begining) :
'###loops k, j, i
ActiveSheet.Name = querie 'querie =k & j & i basically
Sheets("Results").Select
Range("A" & j - 1 & i).Select
ActiveCell.FormulaR1C1 = "= querie & "!" & "M1""
Next i
Next j
Next k
Hope it is clear,
Thanks a lot !
I am trying to copy all the data from multiple workbooks to a single workbook and distribute them to different worksheets.
How should I change the code below to automatically select the data up to the last row and column with value and copy them instead of declaring the range?
For i = 16 To ws.Range("I" & Rows.Count).End(xlUp).Row 'Sheet1 is MasterSheet
File = ws.Range("C" & i) & ws.Range("I" & i) 'File Location and Excel Name
Copy = ws.Range("U" & i) & ":" & ws.Range("V" & i) 'Range to be copied
Workbooks.Open File, 0, 1 'Open File as Read Only
Range(Copy).Copy
ThisWorkbook.Sheets(ws.Range("W" & i).Value).Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial 12 'Paste as Values only
ActiveWorkbook.Close False 'Close WorkBook without saving
Selection.Value = Selection.FormulaR1C1 'Force F2 and Enter selected range
Range("A1").Select
Next i
Your solution works fine, Paolo. I did quite a bit of research on the topic some time ago and I found out these ways are the fastest and simplest ones. I don't think Excel is very good with these things, so I have always tried to stick to the basics.
Just two comments:
I would not use .UsedRange.Copy, since it will return the range of cells that have ever been used.
I would also try to avoid using.select as much as possible. You can find examples on how to avoid it in
How to avoid using Select in Excel VBA
I'm afraid I can't wrap my head around the FormulaR1C1 property when trying to find multiple columns in another sheet, I have these formulas currently (very slowly) autofilling:
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("F2:F" & LastRow).FormulaR1C1 = "=IFERROR(VLOOKUP(R2,LU!C[1]:C[5]),"""")"
Range("F2:F" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,2,true),"""")"
Range("G2:G" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,3,true),"""")"
Range("H2:H" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,4,true),"""")"
Range("I2:I" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,5,true),"""")"
Range("J2:J" & LastRow).Formula = _
"=COUNTIF(A:A,'Pivot Counter'!A7)"
You can see in the top formula, I've attempted it to no success. I keep getting an application defined error, but don't know enough about the property to fix it.
The formula
Range("F2:F" & LastRow).Formula ="=IFERROR(VLOOKUP(A2,LU!A:E,2,true),"""")"
converted to R1C1 reference style is
Range("F2:F" & LastRow).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-5],LU!C[-5]:C[-1],2,True),"""")"
To understand it completely considering the following:
the cell location where you enter the formula is the starting point
starting column is F
column A is 5 columns to the left of F, so A2 converts to RC[-5] (same row, 5 columns to the left of starting point (or think of 6th column (F) and move -5 from there))
same principle for the range lookup. You want to look in columns A:E in sheet LU. So -5 columns from column F (remember starting point is column F, even though it's now looking at a different sheet) to -1 column
Knowing this, you can convert the other formulas, even the COUNTIF.
Also, if you get stuck again, following #BruceWayne's suggestion of turning R1C1 reference style under Excel > Options > Formulas to see what the formula would be when you type in manually.
I am trying to use the index-match formula to reorganize data such that all of the names in column J that have a matching value in column A will be placed in the same spot. I'm going to do this for 5 different columns so that the 5 names on a team will be in the same row as the name of the corresponding client.
My issue is that the index-match formula needs to be able to dynamically shorten or lengthen the size the arrays it uses based on how many clients there are when the VBA script is run.
I can dynamically determine what numbers I need in the formula using COUNTA, but the code will not compile when I try to put it in my formula. My formula is below
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & ",MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0)"
As you can see I need the strings in cells J3 and J1 to be used as the arrays for the index match. J3 = $J$2:$J$2369 and J1 = $A$2:$A$1113
When I run the code it gives me a "Application-Defined or Object-defined error."
You need to use the Range member of worksheet
so use 'test sheet two'!Range("J2:J2369") rather than 'test sheet two'!("J2:J2369").
The following runs
ActiveCell.Formula = _
"=INDEX('test sheet two'!Range(""" & Range("J3") & """) _
,MATCH(Sheet1!A5,'test sheet two'!Range(""" & Range("J1") & """),0))"
Your formula was not including the column criteria for the INDEX Function.
Try:
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & "," & _
"MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0), 1)"
Notice the additional , 1)" on the end of the formula.
Also, you do not have to first Select the cell which you want to enter the formula in, you could just use:
Range("B7").Formula =
I'm trying to incorporate a SUMIF formula into my macro. The code in excel looks like this: =SUMIF('WSO Interest' H2:H46, '20140618 Loans' D10, 'WSO Interest' S2:S46)
I set my dim as i and lastrow as integer
I already set that so that I can find the last row of the column and continue my loop until the last row.
I also used
Sheets("20140618 Loans").Select
Range("A10").Select
Selection.End(xlDown).Select
lastrow = ActiveCell.Row
to find the last row filled with text.
This is what
I have so far:
Range("W10").select
For i = 10 to lastrow
SUMIF formula would go here
Next i
So basically what I'm trying to do is use the SUMIF formula in my macro to start at W10 and keep calculating the SUMIF formula until it reaches the last row. Thanks for the help and feel free to ask any questions.
You can use the Formula property of a Range object to set the formula.
Range("W" & i).Formula = "=SUMIF('WSO Interest'!H2:H" & lastrow & _
", '20140618 Loans'!D10, 'WSO Interest'!S2:S" & lastrow & ")"
The & operator is used to concatenate strings together.
Dim r As Range
Set r = Range("W10", Range("W10").End(xlDown)) --Change it per requirement .....
For Each cell In r
Debug.Print cell.Address --Your Code goes here I guess
Next