Msgbox from formula result - excel

I am trying to add a message box if the above formulas return an error or if the cells in the selection are blank, but as of yet have not been able to complete. Any ideas?
LastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("z2").Formula = "=VLOOKUP(A2,'Ship to'!$B:$C,2,0)"
Range("z2").AutoFill Destination:=Range("z2:z" & LastRow)
LastRow = Range("B" & Rows.Count).End(xlUp).Row
Thanks again for your help
Phill

Use the IsError method to check if a cell formula is returning an error:
For Each cell in Range("z2:z" & LastRow)
If IsError(cell) then
' do what you need to do
End If
Next cell

Related

How can I convert this IF statement to use in VBA

I have the following IF statement =IF(LEN(F2)>LEN(H2),F2,H2)
This just checks which is longer between F2 and H2 and populates I2 with the longest. When I put this in VBA it comes out as =IF(LEN(G1048558)>LEN(I1048558),G1048558,I1048558)in my spreadsheet
How could I fix this?
Sub PopulateI()
Range("I2").Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(R[-20]C[-2])>LEN(R[-20]C),R[-20]C[-2],R[-20]C)"
Selection.AutoFill Destination:=Range("I2:I" & Range("F" & Rows.Count).End(xlUp).Row)
End Sub
Skip the AutoFill and write the formula to the entire range in one step.
If it's easier, you can use the A1-style formula instead of the current R1C1-style formula.
Dim lastRow As Long
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Range("I2:I" & lastRow).Formula = "=IF(LEN(F2)>LEN(H2),F2,H2)"
Or as noted by #Scott Craner, you can fix the R1C1-style formula (which is the real issue):
Range("I2:I" & lastRow).FormulaR1C1 = "=IF(LEN(RC[-3])>LEN(RC[-1]),RC[-3],RC[-1])"

Application-defined or objected-defined error only shows with If Vlookup Statement

So i'm running into a problem with my script which fills down a formula to the last row of a column. It works for the most part except when it steps into my if function.
Sub vlookups()
'
' vlookups Macro
'
'
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("C2:C" & LastRow).Formula = "=VLOOKUP($A2,'Refined Raw'!$A:$AH,2,false)"
Range("E2:E" & LastRow).Formula = "=VLOOKUP($A2,'Refined Raw'!$A:$AH,3,false)"
Range("G2:G" & LastRow).Formula = "=VLOOKUP($A2,'Refined Raw'!$A:$AH,4,false)"
Range("I2:I" & LastRow).Formula = "=VLOOKUP($A2,'Refined Raw'!$A:$AH,5,false)"
Range("K2:K" & LastRow).Formula = "=IF(VLOOKUP($A2,'Refined Raw'!$A:$AH,6,false)=0,'',VLOOKUP($A2,'Refined Raw'!$A:$AH,6,false))"
End Sub
This runs fine on the first 4 Range vlookups, however when it hits the last one with the IF(VLOOKUP it shows an error. I'm not too well versed with VBA so i'm not sure what could be causing this issue.
Any help?
You need to double up on the quotes inside a formula:
"=IF(VLOOKUP($A2,'Refined Raw'!$A:$AH,6,false)=0,"""",VLOOKUP($A2,'Refined Raw'!$A:$AH,6,false))"

Auto-filing Formula to Last Row of a Single Column

I need to constantly start at cell R2 and auto-fill a formula down to the last row of column R. However, the number of rows is constantly changing so I need to write a macro that finds the last row and stops their. I've tried this code but keep getting errors. Any thoughts?
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("R" & Rows.Count).End(xlUp).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2" & Lastrow)
The error you are getting is due to this Range("R2" & Lastrow)
The range address concatenates to R21400 if the last row was 1400. So we need to add the :R to the address.
Range("R2:R" & Lastrow)
It will now concatenate to R2:R1400
There is not need to do it with three lines, one will suffice:
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P" & Rows.Count).End(xlUp).Row
Range("R2:R" & Lastrow).FormulaR1C1 = "=RC[-2]/RC[-4]"
End Sub
Not sure if you already tried this:
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P2").End(xlDown).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2" & Lastrow)
It seems to me that Range("R" & Rows.Count) locates you out of current region

Is there any way to make the time stop counting up in excel?

In my excel sheet I have the following code:
=IF(ISERROR(MATCH(D2,'Sheet 2'!A:A,0)),"",NOW())
This basically checks to see if the value in D2 matches any values in column A:A in sheet 2, and then populates the cell with a date and time with NOW().
My problem is the date and time is counting up because I am using the NOW() function whereas what I need is the date to, in a way, take a snapshot of the date or freeze the date. This table I am creating is acting like a log so I need the date to stay as it is when it is put into the cell.
Any help with this is much appreciated.
You could have this automatically run, if you paste it in the code behind your sheet (Where the range theCells is the column where the timestamps are going):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("theCells")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
If Range(Target.Address).Value <> "" Then
Range(Target.Address).Copy
Range(Target.Address).PasteSpecial xlPasteVaues
End If
End Sub
Ok so I solved it, In my VBA I have the following code which pretty much creates a log file when my button is clicked, it takes various information in different cells and populates it in the next defined available row:
Sub copylog()
Dim LastRow As Long, ws As Worksheet
Dim wt As Worksheet
Set ws = Sheets("Create Log")
Set wt = Sheets("PDF Creation")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("A" & LastRow).Value = wt.Range("N11").Value
ws.Range("B" & LastRow).Value = wt.Range("N12").Value
ws.Range("C" & LastRow).Value = wt.Range("N13").Value
ws.Range("D" & LastRow).Value = wt.Range("N14").Value
ws.Range("E" & LastRow).Value = wt.Range("N15").Value
ws.Range("F" & LastRow).Value = wt.Range("N16").Value
ws.Range("G" & LastRow).Value = wt.Range("AT19").Value
ws.Range("H" & LastRow).Value = wt.Range("AT21").Value
ws.Range("I" & LastRow).Value = wt.Range("AT23").Value
ws.Range("J" & LastRow).Value = wt.Range("AT25").Value
ws.Range("K" & LastRow).Value = wt.Range("AT27").Value
ws.Range("L" & LastRow).Value = wt.Range("A2").Value
ws.Range("M" & LastRow).Value = Environ("Username")
End Sub
The code above will populate a table in the next available row, for example, the first line:
ws.Range("A" & LastRow).Value = wt.Range("N11").Value
This will take the value that populates N11 in the PDF create sheet and populate the next available row in column A in the create log sheet (using copy and paste).
The line that has fixed my time problem is the line:
ws.Range("L" & LastRow).Value = wt.Range("A2").Value
In cell A2, I have the NOW() function and the button copies and pastes it into the next available space in column L (copies and pastes as TEXT).

VBA to use sum formula for cells in same row. Row number will change with each loop

I'm using VBA to extract numbers from a website using a loop. I have these numbers imported into column F and G. I'm trying to include a line in my VBA that would allow me to use the sum formula to add these two cells and post the result in column E.
The numbers will always be in the same row, but in columns F and G.
Can i use the (ActiveCell.Row) function in this way?
or should just use an If function at the end of my loop.
Range("E" & (ActiveCell.Row)).Formula = "=SUM("Range("F" & (ActiveCell.Row)) & Range("G" & (ActiveCell.Row))")"
I have these numbers imported into column F and G.
If you want to insert the formula in Col E then use this. This will enter the formula in all the relevant cells in Col E in one go.
See this example
Sub Sample()
Dim lRow As Long
Dim ws As Worksheet
'~~> Change this to the releavnt sheet name
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find Last row in col E which has data
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> This will put the formula from E1 to E last row
.Range("E1:E" & lRow).Formula = "=SUM(F1+G1)"
End With
End Sub
In the forumla dont use Range() as it will not return a String. And also, you forgot the operator, you need to add "+" or ":" into the Forumla:
Range("E" & (ActiveCell.Row)).Formula = "=SUM(F" & ActiveCell.Row & "+" & "G" & ActiveCell.Row & ")"
Otherwise you'll need to access the Address method on the Ranges in the forumla (which is a waste because you have entered the address anyway)
but for clarity the code below demonstrates what I mean:
Range("E" & (ActiveCell.Row)).Formula = "=SUM(" & Range("F" & (ActiveCell.Row)).Address(False,False) & ":" & Range("G" & (ActiveCell.Row)).Address(False,False) & ")"

Resources