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 !
Related
I'd like to shorten my code by putting multiple columns to one command.
I'm copying formulas from one row but specified columns and want to paste to another sheet in the same wksht. Just the row number is a variable. Maybe do this by an array?
As an example I'm pasting below not working code with only 3 columns.
Sub Copy()
Dim LastrowQ As Long
Dim LastrowR As Long
LastrowQ = Cells(Rows.Count, "B").End(xlUp).Row
LastrowR = LastrowQ + 1
Workbooks(Rep1).Worksheets("Formula").Range("A2, C2:F2, N2, V2:AD2").Copy
*Workbooks(Rep1).Worksheets("Report").Range("A" & LastrowR, "C" & LastrowR & ":F" & LastrowR, "N" & LastrowR, "V" & LastrowR & ":AD" & LastrowR).PasteSpecial Paste:=xlPasteFormulas*
End Sub
Could you please help to modify the code in paste part as it doesn't work right now? This would allow to make original code much shorter and hopefully faster.
What if I'd like to propagate function in second sheet from variable row till the specified row at the same time? ("A" & LastrowR & ":A" & LastrowS,...)
Thanks in advance
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 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 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
I'm trying to calculate the sum of my columns (column I). From 1 to the last record in I it has. When I record a macro I get this as output, but this is gibberish to me.
ActiveCell.FormulaR1C1 = "=SUM(R[-11]C:R[-4]C)"
I found another topic and there they said
LastRow = .Range("I" & .rows.Count).End(xlUp).row
Range("I"&LastRow) = "SUM(I1:I...)"
Except in my case, I can't figure how to enter the lastrow of I in it.
All help is welcome :)
There are two ways of referencing a cell - 'R1C1' and 'A1'. The former works like co-ordinates, with a relative number of rows (R) and cells (C).
The other reference style refers to the cell name on the sheet - B6, F67 etc.
Let's say you want to put your Sum() in cell B1 and LastRow has a value of 6:
ActiveSheet.Range("B1") = "=Sum(I1:I" & LastRow & ")"
Would insert the following function in cell B1:
=SUM(I1:I6)