Copy all formulas when inserting new row in Excel worksheet - excel

I want to copy all formulas when inserting a new row in the Excel worksheet. I have some calculation formulas and I am taking some values from another Excel file.
Formulas I am using in this Excel worksheet are as follows:
=IFERROR(VLOOKUP(C4,'list.xlsx]Sheet1'!$A$4:$L$261,3,FALSE),"")
=IFERROR(G6*F6,"")
I googled for a solution. I got 2 ideas.
one to make a table with these data.
I tried this one. Formula no:1 worked perfectly. But 2nd formula didn't copy to next line.
The second one to use a VBA code. which works only one time. When open excel next time I have to create VBA code again. also, it needs to click in between cells to create next row.
I want to create a new row when right click on the sl no(leftmost column) and click "insert".
Please help.

Sorry im new in SO but i want to help you:
You using conditional blocking for example : $A$4:$L$261
=IFERROR(G6*F6,"") doesnt copy anything
In VBA you got function PasteSpecial whats copeing a formulas from
cell, here some ex. of using that:
With Worksheets("YourWorksheet_name")
.Range("Your_cell_input").Copy
.Range("Your_cell_output").PasteSpecial Operation:=xlPasteSpecialOperationAdd
End With
If i good understand u want a maro what pasting a forulas when new rod is added. I recommend to use something like this:
Let's make a demo that You have formulas on colmn C to copy that(its affecting column A)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = ActiveCell.Column Then
refRow = Target.Row - 1
thisRow = Target.Row
Range("C" & refRow).Copy Range("B" & thisRow)
End If
End Sub
Anyway please add some more code what u have or screenshoots what u expecting

Related

Macro for setting Concatenate formula to entire column?

I am looking for a VBA code that I can use for a button that I want to have that runs the Concatenate formula to the entire column going down. So the formula I have for the first cell is '=CONCATENATE($F$4,$H$4,$J$4)'. I want the button to add Concatenate to the first row (row 4) and all the rows after that inside the "AS" column...
I tried recording a macro changing the formula of the cell to CONCATENATE and inputting the values but it does not seem to work when applying it to my button.
I am fairly new to VBA coding with formulas so I am sure there is a very simple way to do this. Sorry in advance if I do not understand some of the basics of your explanation :)
CURRENT CODE:
Private Sub ParetoButton_Click()
Range("AS3").Formula = "=CONCATENATE(F3,"" "", H3,"" "",J3)"
Range("AS").FillDown
End Sub
Found the solution after more research and playing around w/ other codes...
Private Sub ParetoButton_Click()
Range("AS3").Formula = "=CONCATENATE(F3,"" "", H3,"" "",J3)"
Dim LastRow As Long
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("AS3:AS" & LastRow).FillDown
End Sub

VBA: Deleting rows that have a blank in a certain column and making a button in Excel

So I am trying to write VBA code to delete an entire row in Excel if a cell in that row in a certain column is blank. I am completely new to VBA, so I literally just had to learn as I coded. I think I have the methodology down, but it always leaves one last row with a blank in that specific column in the worksheet. In some cases, it leaves 5 rows with a blank in that specific column. Any help fixing would be appreciated, thank you!
I also want to have it where I can click on a button in the workbook, more than likely on a separate sheet, and it will do the deleting rows methodology for a specific sheet. If that is possible, am I able to move all of that methodology from one from workbook to another?
I've already tried some ways of implementing both of these using VBA, but this is my first time ever using VBA, so a lot is still new to me.
Private Sub Button_Click()
Dim LR As Long
Application.ScreenUpdating = False
For LR = Sheets("Test2").Range("AB" & Rows.Count).End(xlUp).Row To 2 Step 1
If Range("AB" & LR).Value = "" Then
Rows(LR).EntireRow.Delete
End If
Next LR
Application.ScreenUpdating = True
End Sub
I expect all the rows with blanks in that column to be deleted when I press the button in the worksheet.
If the blank cells in that column are truly blank and not zero length strings returned by formulas (e.g. "") then SpecialCells can remove them all at once.
Private Sub Button_Click()
Intersect(Me.Columns("AB"), Me.Cells.SpecialCells(xlCellTypeBlanks)).EntireRow.Delete
End Sub
For your own code, you should step backwards in your loop (Step -1). Further, you should not look at column AB for the last cell. If there were blanks in the last row or rows, they would be ignored.

Excel Macro copy paste in right till data in left cell

I have two excel columns in a worksheet, consider as A(left) and B(right) and I have recorded a macro where it will calculate a formula and copy/paste it to all the right side columns till where the left side column has data. but when next time some extra data is added to the left column and when I run a macro to copy/paste then it is only considering the previous range but not extending to the newly added cells.
example : A1:A5 is left side and B1:B5 is the right side and my formula in B range which is right range calculate based on A1:A5 and my macro works fine and restricted only to B1:B5 even when I added new data like A1:A10 only copying B1:B5. what is the method I can use my macro automatically till the data range of A side column?
Better next time, you provide a screenshot of your data & also VBA code in question.
You are using a static range, while you require is dynamic range.
try this
Sub test()
Dim i As Integer
i = WorksheetFunction.CountA(Range("A:A"))
Range("B1:b" & i).Select 'instead of selecting you can provide your formula to whole range.
End Sub
For any other issue, feel free to comment
As per your description just try below.
Sub FillFormula()
Dim i As Long
i = Range("A1").End(xlDown).Row
Range("B1").FormulaR1C1 = "=RC[-1]+5"
Range("B1").AutoFill Destination:=Range("B1:B" & i)
End Sub

Insert the same Non-Blank Rows every nth row

I need to be able to insert the same 5 rows of data between every row on an Excel sheet. The 5 rows are not blank but contain specific data that needs to be repeated. The source of the 5 rows could be on a second sheet or rows 2 through 6 on sheet 1, which ever works best. Copy and pasting manually unfortunately is not an option as there are hundreds of lines. Is anyone able to provide some guidance as to how to accomplish this task?
You can use a formula in your sheet 2 like below:
=INDIRECT("Sheet1!"&ADDRESS(FLOOR(ROW()-1;5)/5+1;1))
By using this formula you will have each rows 5 times dynamically, that as soon as any change on sheet1; data in sheet 2 will be updated.
If you want to make them static Copy your data and Paste them with Values Only option.
You need to use a macro to do this:
Sub PasteRangeEveryNthRow()
Dim RW As Long, i As Long
RW = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A1:A5").Copy
For i = 1 To RW Step 1
Range("A" & i).PasteSpecial Paste:=xlPasteFormulas
Next i
End Sub
This code presupposes that you are copying the range A1:A5 from SHEET2 and pasting it every one line until the last data in the column A.
basically, you need to press ALT+F11 to open the VBA window in excel , then go to INSERT>MODULE and paste this code there. Then run this macro from the developer tab in excel.
if you don't see the developer tab in your excel, go to the options and enable it.

Copy range and store them on a different sheet

I think this is not a too difficult task to do, but the problem is that I actually don't know anything about programming and I need to do this on my current job. This is my issue:
The problem is that I have to develop a Macro, and assign it to a button, that copies range E3:K14 from Page 1(sheet1) and paste it on A1 on Page 2(sheet2). This first task is rather easy, but the problem comes when if I hit again the button's assigned Macro it has to copy the same range from Page 1 and paste it on Page 2 but if the first has to check if there already something pasted on A1, if there is, then it must copy it on cell I1 and if I click the button again to Q1 and so on.
When the range is pasted it must be pasted with "Paste Vales" option.
If someone could just put me the exact code (with some comments if possible) for me just to paste it would be really helpful.
Any help would be really appreciated!
Try below code :
Sub sample()
With Sheets("Sheet2")
Sheets("sheet1").Range("E3:K14").Copy .Cells(.Range("A" & .Rows.End(xlUp).Row) + 1, 1)
End With
End Sub
Explanation :
Have used the Range Copy Method and provided a destination where to
paste.
.Range("A" & .Rows.End(xlUp).Row) + 1 checks for last used cell in sheet2 column A and adds 1 so that data is pasted on last used row.(Assuming the column E does not have blank cells)
Dude you can use this code to get the last column with data, this is the key for solve your problem.
Sub SelectLast()
Dim LastColumn As Long
With ActiveSheet
LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
End With
MsgBox LastColumn
Cells(18, LastColumn + 2).Select
End Sub

Resources