Insert formulas in another row of the named range - vba

I have an excel sheet with multiple ranges one below the other. The range is only one row with multiple formulas. The formulas would need to be replicated below that row. I would need to resize each one of them with a loop. However as there are multiple ones the End function doesn't work.
For i = 1 To 2
resizeSh.Range("tablename").EntireRow.Copy
Destination:=resizeSh.Range("tablename").End(xlUp).Offset(1, 0)
Next
Does anyone have a solution how to find the last row of the named range and insert the same formulas few times again?
I also tried with this, but it instead of 2 it adds 3 additional lines and I cannot find the cause of this:
For i = 1 To 2
ActiveSheet.Range("range").Cells(1, 1).Offset(1).EntireRow.Insert
Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveSheet.Range("range").Cells(1, 1).EntireRow.Copy
ActiveSheet.Range("range").Cells(1, 1).Offset(1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
Next

I'm not sure I follow what you're trying to do, but this might help.
With Range("range")
.Copy
.Offset(1).Resize(3).Insert shift:=xlDown
End With

Related

Array Formula in vba code with coping the formula down

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

VBA Sort shift delete 2 cells if specific cell is empty

I need some help with a VBA code. I am trying to shift delete 2 cells in 2 columns A and B, if cell in column B is empty. I need to repeat this action 10 times.
I have a VBA code that does this with a filter on blanks in Column B. It works fine on a small dataset, however, since my datasets are very big and the actions need to be repeated 10 times, it takes a lot of time.
Sub del()
For i = 1 To 10
ActiveSheet.Range("A1").AutoFilter Field:=2, Criteria1:="="
Range("b:b" & Cells(Rows.Count, "A").End(xlUp).Row).Select
Selection.SpecialCells(xlCellTypeVisible).Delete Shift:=xlToLeft
Range("a:a" & Cells(Rows.Count, "A").End(xlUp).Row).Select
Selection.SpecialCells(xlCellTypeVisible).Delete Shift:=xlToLeft
Next i
End Sub
I was thinking that instead of filtering, to:
-sort column B to get all blanks at the end
-select from the first blank cell until the end of the row (for both A and B columns)
- delete selected
- repeat this action for 10 times
I tried a few things, but I cannot get a proper code for this.
Any help is very much appreciated!
Thank,
Daniela

Excel: Copy and insert rows on another sheet based on cell

I'm trying to make a code that checks for numbers in a master sheet called All in column D (ex. 780101) and if it meets the criteria, it copies the whole row and inserts (not paste) it to another sheet with the name of the criteria (ex. 780101), starting on row 6.
The code I have doesn't work like I want it to. It doesn't copy all the rows that meet the criteria and sometimes it inserts blank rows.
Sub Insert()
For Each Cell In Sheets("All").Range("D:D")
If Cell.Value = "780101" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow + 1).Select
Selection.Copy
Sheets("780101").Select
Rows("6:6").Select
Selection.Insert Shift:=xlDown
End If
Next
End Sub
I'm just starting to learn VBA, so if it could be possible the names of the sheets would be the criteria of the cell values (the code is made for only one sheet - 780101, but there are 20 of sheets with different names).
It's tough to make recommendations without seeing sample data and what could potentially be causing the problems you are having but you can run this rehash of your existing code.
Sub Insert()
Dim dc As Range
With Sheets("All")
For Each dc In Intersect(.Range("D:D"), .UsedRange)
If dc.Value2 = 780101 Then
dc.Resize(2, 1).EntireRow.Copy
Sheets("780101").Rows(6).Insert Shift:=xlDown
End If
Next
End With
End Sub
The nature of running that from top to bottom means that the results will be reversed. You may wish to consider running the main loop from bottom to top to maintain the order.

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

How to find and select the last column in VBA?

I am trying to create an excel macro which finds the last column of a sheet and then selects the entire column. However, this column will always be different- some days it will be column 'H', other days will be column 'GX' as the data in the sheet is constantly updated. So far I have seen how you can find the last column and then delete it, but it specifically refers to that certain column once the macro runs again. I need it to always refer to the last column, no matter what column that may be. Thanks!
Here is the code. I am new to VBA, etc. and this was created through the macro recorder and other things I found online so bear with me!
`Sub Macro11()
Sheets("Sheet25").Cells(1, 1).Activate
ActiveCell.SpecialCells(xlLastCell).Select
lastCol = ActiveCell.Column
Columns("W:W").Select
Selection.Delete Shift:=xlToLeft
End Sub`
Here is the sample code
Avoid using Select /Activate in your code. To know why refer this link
Sub Macro11()
Dim LastCol As Long
With ThisWorkbook.Sheets("Sheet25")
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Columns(LastCol).Delete
End With
End Sub