Macro for setting Concatenate formula to entire column? - excel

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

Related

How to use VLOOKUP in Excel VBA to find matching cells

I am just starting out with Excel VBA and to be honest I am not that skilled in using normal Excel either.
I've got one sheet that has unique codes in column C, I also have another sheet that has unique codes in column A (except first rows as they've got column labels).
In case this unique code from sheet 1 is also found in the column in sheet 2, I want column J in the sheet1 to have that code value, otherwise, if it cannot be found, I want it to have #N/A.
Normally in Excel I would do this by selecting J2 and entering following function:
=VLOOKUP(C2,Sheet2!A:A,1,False)
Then I'd just double click the corner of the cell to populate all rows.
How do I do it in VBA? I wrote this code hoping it would do something:
Worksheets("Sheet1").Activate
ActiveSheet.Range("J:J").Value = Application.VLookup(C2,Worksheets("Sheet2").Range("A:A"),1,False)
However, this does not work. I just get #N/A for all cells in the J column. Any suggestions what I can do?
The following code will give you what you want. Note that you would only want to put the formula into rows where values actually exist in column C on sheet1.
Option Explicit
Sub InsertVlookup()
Dim LastRow As Long
LastRow = Sheet1.Cells(Rows.Count, 3).End(xlUp).Row
With Sheet1.Range("J2:J" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC[-7],Sheet2!C[-9],1,FALSE)"
.Value = .Value
End With
End Sub

Macro to Fill new added column with vlookup formula

The macro accesses the last sheet in the workbook (always the same format) and adds a column D, with a header. Until here all is well; the final step is where I get issues.
I want the new column filled with the formula:
=VLOOKUP(C2;'Output'!$A:$B;2;FALSE).
Note that the current sheet is different from where the formula looks (sheet Output).
I have tried various codes that I found here and there... but I cannot manage to make it work correctly right.
Here how my code looks currently:
Sub ColumnInsert()
Dim rws As Long
rws = Cells(Rows.Count, "D").End(xlUp).Row
Sheets(Sheets.Count).Select
Range("D:D").EntireColumn.Insert
Range("D1").Value = "BOX Number"
Range("D2:D" & rws).Formula = "=VLOOKUP(C2;'Output'!$A:$B;2;FALSE)"
End Sub
the last row of code is what does not work... could you please help me?
The problem is in your language settings. If you write your formula in a worksheet you use ";" to separate the variables. In Vba you have to use "," so your formula would be:
"=VLOOKUP(C2,'Output'!$A:$B,2,FALSE)"

Issue with Complex Formula Being Placed Onto Sheet Via VBA and Interacting with Pivot Table

I am having an issue with a formula being placed onto my worksheet via vba. The formula interacts with data on a pivot table. When placed in cell Y8 on the worksheet the following formula functions as desired (non vba):
=IF(OR(L8="(blank)",L8=""),IF((K8-$A$2)/(365/12)<0,0,(K8-$A$2)/(365/12)),IF((L8-$A$2)/(365/12)<0,0,(L8-$A$2)/(365/12)))
The idea is to check if L8 is either null or (blank), if it is then use this formula: IF((K8-$A$2)/(365/12)<0,0,(K8-$A$2)/(365/12)). If L8 has a value (will be a date) then I want to use this slightly differnt formula: IF((L8-$A$2)/(365/12)<0,0,(L8-$A$2)/(365/12))).
Columns L and K are in a pivot table.
I used activecell.formulaR1C1 to translate my on sheet formula to R1C1. The only change I made was adding a set of quotation marks around "(blank)" --> ""(blank)"".
I am still getting a run-time 1004 message on my formula line of vba.
My VBA Code is here:
Sub PerformFormulas()
Dim LastRow As Long
LastRow = Worksheets("Calculator").Range("C" & Rows.Count).End(xlUp).Row
Worksheets("Calculator").Range("Y8:Y" & LastRow - 1).FormulaR1C1 = "=IF(OR(RC[-13]=""(blank)"",RC[-13]=""),IF((RC[-14]-R2C1)/(365/12)<0,0,(RC[-14]-R2C1)/(365/12)),IF((RC[-13]-R2C1)/(365/12)<0,0,(RC[-13]-R2C1)/(365/12)))"
End Sub
I checked that LastRow and Calculator are being recognized correctly and they are (I changed to a simple .select formula and that portion of the code works alright).
Thanks in advance for any help!
You didn't escape the other pair of quotes:
"=IF(OR(RC[-13]=""(blank)"",RC[-13]=""), ...
should be
"=IF(OR(RC[-13]=""(blank)"",RC[-13]=""""), ...

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.

Copy all formulas when inserting new row in Excel worksheet

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

Resources