Array Formula in vba code with coping the formula down - vba

I work on the code that will calculate Array Formula basing on how many records is in the column N:N that is 11 columns earlier (offset 11). I want to use the formula with array that will use the parallel row from the column N:N and copy down until the last record in column N:N exist. However, for now, formula copies down basing on the first record only instead of taking the row in parallel:
With ThisWorkbook.Sheets("Sheet1")
TargetRow = 4
.Range("N4", .Cells(Rows.Count, "N").End(xlUp)).Offset(0, 11).FormulaArray = "=IFERROR(Name&INDEX(Names_Area,MATCH(RC[-11],Name&Name_Origin,0),2),"""")"
End With
I heard about fill down function or something alike but I am not sure how to insert it here.
How can I fix it so when the formula copies down into rows it takes the row in parallel and not all the time N4 (that is the first row of records).
I will appreciate any help.
I also want to mention that any other formula without array works and copies formula down basing on the rows in column N:N that are in paralell.

Try with .Autofill. something like:
With ThisWorkbook.Sheets("Sheet1")
TargetRow = 4
.Range("N4").FormulaArray = "=IFERROR(Name&INDEX(Names_Area,MATCH(RC[-11],Name&Name_Origin,0),2),"""")"
.Range("N4").AutoFill .Range("N4:N12")
End With
I have used an example end point of N12 for the autofill which you can adjust.
Though note you are actually going to column Y with:
.Range("N4", .Cells(Rows.Count, "N").End(xlUp)).Offset(0, 11)
So you may want to ensure you autofill and populate formula in the actual column you want to fill.
Maybe something like:
.Range("N4").Offset(0, 11).FormulaArray =
Reference:
https://www.mrexcel.com/forum/excel-questions/500971-how-copy-array-formula-down-vba-macro.html

you could also use
With ThisWorkbook.Sheets("Sheet1")
With .Range("N4", .Cells(Rows.Count, "N").End(xlUp)).Offset(0, 11)
.Cells(1, 1).FormulaArray = "=IFERROR(Name&INDEX(Names_Area,MATCH(RC[-11],Name&Name_Origin,0),2),"""")"
.Formula = .Cells(1, 1).Formula
End With
End With

Related

Excel dynamic column reference for range

This seems like such an easy thing but I can't seem to work it out.
All I am looking to do is have a dynamic last column based on the last active column in the row above.
I have a similar code for finding the last active row
LastCell = Sheets("CALCULATIONS").Range("A20000").End(xlUp).Row
Sheets("CALCULATIONS").Range("A2:A" & LastCell).Copy
Now I am trying to work out a horizontal version.
All I want to do is find the last active column based on values in row 15 in CALCULATIONS
then drag the array formula that already in A16 across to [?]16 (? being the last column from row 15)
I feel like the solution is right in front of me but I've been drawing blanks!
Any help would be great!!
Try,
dim lc as long
with workSheets("CALCULATIONS")
lc = .cells(15, .columns.count).end(xltoleft).column
.range(.cells(16, "A"), .cells(16, lc)).formula = .cells(16, "A").formula
'alternate method
'.range(.cells(16, "A"), .cells(16, lc)).fillright
end with

How to get VLOOKUP to select down to the lowest row in VBA?

Looking to automate the insertion of a VLOOKUP formula in a cell.
When recording the macro I instruct it to populate the columns below with the same formula. Works great, however, there is an issue when the table that the VLOOKUP searches through changes (more or less rows).
As it's recorded, the VLOOKUP drops down to the final row in the table (273). However, I want to set it up so that it will go down to the very last row. Meaning that I can run the script on tables of varying numbers of rows.
Selected columns will remain the same.
Range("AJ2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-20], Previous!R2C2:R273C22,17,FALSE)"
try this:
With Worksheets("Previous")
Range("AJ2").FormulaR1C1 = _
"=VLOOKUP(RC[-20], Previous!R2C2:R" & .Cells(.Rows.Count, 2).End(xlUp).Row & "C22,17,FALSE)"
End With
where:
Range("AJ2")
will implicitly reference the ActiveSheet
.Cells(.Rows.Count, 2).End(xlUp).Row
will reference "Previous" worksheet, being inside a With Worksheets("Previous")- End With block
#nbayly said it, plenty of posts on this. Infact i have provided an answer to this before here:
How to Replace RC Formula Value with Variable
below is slightly modified for a dynamic range, which is what i believe you are looking for
For j = n To 10 Step -1
If Cells(j, 1).Value = "" Then
Cells(j, 1).Formula = "=VLookup(RC20,Previous!R2C2:R273C22,17,FALSE)"
End If
Next j
remember to define j as long and n=sheets("sheetname)".cells(rows.count,1).end(xlup).row
replace 10 in j = n to 10 with the starting row number

Faster Workflow

I have a table (Table 1) with a whole bunch of well data (versions, MD, HD, etc.) and I want to create another table (Table 2) that will only show the data for the well I am interested in.
I have it set up where you select the well using a drop down list. Then I want Table 2 to be populated with four values for each of the iterations that show up in Table 1....
I tried using vlookup but was having issues when a well had multiple versions. And I also tried using an advanced filter.
Screenshot of the spreadsheet
Let's solve this using a helper column. First, assume column A will be used to the left of your table, to show the row number which each one of these is found in.
A5 would have the following formula:
=MATCH($C$1,K:K,0)
This shows us the row number that Well1 is first matched at. Then A6 and copied down would have the formula:
=A5+MATCH(B6,OFFSET(K1,A5,0,COUNT(M:M),1),0)
This uses OFFSET to create a new range, starting at the cell immediately below the previous match for Well1, and then uses MATCH to find what row that occurs.
So now, column A will always show the row number to pull data from. The rest is simply using the INDEX function to pull from your desired columns. For example, the data in column C pulls the iteration from column L, and can be pulled through formula like so, in cell C5 and copied to the right / down:
=INDEX(L:L,$A5)
If your data is appropriately normalized, you might be better off with a Pivot Table. This would give you the option of filtering by Well ID.
To use a Advanced filter you will need to create a worksheet event. Place this in the code for the sheet on which you want the data.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2")) Is Nothing Then
Dim dataRng As Range
Dim critRng As Range
Dim CpyToRng As Range
Dim cpytoarr() As Variant
With Worksheets("Sheet1")
Set dataRng = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown).End(xlToRight))
End With
With Me
.Range("CC1") = .Cells(1, 1).Value
.Range("CC2") = "'=" & .Cells(2, 1).Value
Set critRng = .Range("CC1:CC2")
Set CpyToRng = .Range(.Cells(6, 1), .Cells(6, 1).End(xlToRight))
End With
Debug.Print dataRng.Address
Debug.Print critRng.Address
Debug.Print CpyToRng.Address
dataRng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=critRng, CopyToRange:=CpyToRng, _
Unique:=False
critRng.ClearContents
End If
End Sub
How this works. This assumes the data is on Sheet1 and starts in "A1" with no blanks in column A or the last row:
On Sheet2 set it up like this:
It is important that the header rows on sheet2 are name identical to the headers on sheet1.
Now every time that the value changes in A2 on sheet 2, your drop down, the requisite data will appear below row 6.

VBA - Find number of times a value appears on another sheet

I'm relatively new to VBA and I've ran into a problem. I have a list of values in Sheet 1 and all of those values are listed in Sheet 2 as well. Many of the values are listed multiple times in Sheet 2. How do I count the number of times the value appears in Sheet 2, then add that number to Cells(a,3) in Sheet 1 (where a corresponds to the row)? I want to keep the listed values in Sheet 1 unchanged and only manipulate Cell(a,3) in each row.
I've tried numerous things but I really have no idea where to start. Any help would be appreciated.
You would loop through your cells using something like:
For Each rngCell in Range(Cells(1, 3), Cells(Rows.Count, 3).End(xlUp))
rngCell.Value = rngCell.Value + WorksheetFunction.CountIf('Sheet2!'A:AZ, "value")
Next rngCell
Where the for is looping through your first to your last cell in column 3, then adding the countif from sheet2 (change the A:AZ to whatever your used columns are) for "Value"
You can do this without a loop by just assigning the COUNTIF() function to your range:
With Range("C2:C" & Range("A2").End(xlDown).Row)
.Formula = "=COUNTIF(Sheet2!A:A,A2)"
.Value = .Value
End With

Loop through columns and copy and paste cells A to B

I am working on a simple code but i have not able to crack it. i want the macro to run and pick up a value (2), and copy and paste the cells A to C , into sheet 2. The code below does copy the Cells A-B and pastes. I failing on the loop . I want it to loop from 1st row to last row. please.
Sub Tier_2()
With Sheets("Sheet1")
.Range(.Cells(1, 1), .Cells(3, 3)).Copy Sheets("Sheet2").Cells(1, 1)
End With
End Sub
This isn't for your specific situation, but the concept is what you are looking for. You should be able to do some research with what I'm showing you and figure it out.
lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row 'gets last row of Source sheet
For x = 1 to lastRow
Sheets("Sheet2").Cells(x, 1) = Sheets("Sheet1").Cells(x, 1) 'copy data from Sheet1 to Sheet2 row x
Next x 'loop and add one to x
You can use variables for either the row or column numbers in either the source or target sheets. You can insert another loop inside the first one. I'm not sure of what you are looking for, but your question didn't seem to have a loop in it at all.
If that doesn't get you going in the right direction try "Do While Loop" in a search.
EDIT: Updated the formula to better reflect idea.