It may seem to be an easy one as i am novice at VBA. I am trying to fill the filtered blank visible cells with previous non blank hidden cell value in the same column. The Lookup formula is working fine on excel sheet but using it with variable range in VBA is giving Application defined or object defined Error on lookup formula line.
nlr = Cells(Rows.Count, 9).End(xlUp).Row
With Activehseet
Application.DisplayAlerts = False
Range("A1:K" & nlr).AutoFilter Field:=2, Criteria1:=""
Range("A1:K" & nlr).AutoFilter Field:=1, Criteria1:="P * Suite *"
Range("B2:B" & nlr).SpecialCells(xlCellTypeVisible).ClearContents
For Each c In Range("B1:B" & nlr).Offset(1, 0).SpecialCells(xlCellTypeVisible)
n = c.Row - 1
c.Formula = "=LOOKUP(2,1/($B$2:B&n&<>""),$B$2:B&n&)"
I have already tried it with below too, but it didn't work
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""),$B$2:B" & n & ")"
Please help me resolve this
EDIT: I have already tried this approach, but it didn't work either
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""""),$B$2:B" & n & ")"
This is the correct approach for concatenating a string into another string:
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""),$B$2:B" & n & ")"
However, there is a problem with this part of it:
... & n & "<>""), ...
That will place one double quote into the text. I presume you are expecting it to look like <>"" when parsed, which means the line should look like this:
... & n & "<>""""), ...
Why? Because in a string to put one double quote you need to put a double, double quote: "" = " (in finished string). Therefore a double, double quote becomes: """" = "".
Here is the corrected original code:
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""""),$B$2:B" & n & ")"
Related
So this is what I want to do:
For example, in the sheet named "Outputs", the A1 cell input is
= "abcd" & Input!A5 & "efgh",
where Input!A5 is the cell value of the different sheet in the excel file.
I want to make it change so that after executing all the methods after selecting the A1 cell as
= "abcd" & Input!A5 & "efgh", I want to change the A1 cell as, = "abcd" & Input!A6 & "efgh", and then = "abcd" & Input!A7 & "efgh" and so on. (So it's basically replacing the values as A1 to Ai)
I thought of using Replace function, by writing the replacing string as Ai, and replacing i with i +1 by starting with for loop.
But I don't think this is a right method.
Could anyone shed light on how to address this?
Thanks in advance.
Check this
InputWorksheetLastRow = 7 'place here your last row value
Set ws = Worksheets("Input")
For i = 5 To InputWorksheetLastRow
Worksheets("Outputs").Range("A" & i - 4).Formula = "=" & Chr(34) & "abcd" & Chr(34) & "&Input!" & ws.Cells(i, 1).Address(0, 0) & "&" & Chr(34) & "efgh" & Chr(34)
Next i
This question already has answers here:
Different languages issue when inserting formula from VBA
(1 answer)
How do I put double quotes in a string in vba?
(5 answers)
Closed 1 year ago.
Goodmorning everyone,
I'm a beginner with VBA trying out various things.
Atm I'm trying to get a formula to be placed inside a row of cells but I'm encountering a problem.
I use a number of variables that seem to be causing a problem in my formula.
I already changed some things I found on this site (like changing from ActiveCell to Range and adding the rr and r variables.
However this doesn't fix the error message I'm getting.
I looked up the error but didn't quite understand what the meaning was.
Could anyone tell me what is causing the problem in my code but also what the error message means so I can try and debug it myself.
With F8 I already made sure that all variables were filled in correctly.
the error comes from the Range(r).Formula line.
Sub Zoeker()
Sheets("Invoer").Select
NumRows_Gist = Range("A3", Range("A3").End(xlDown)).Rows.Count
NumRows_Vand = Range("I3", Range("I3").End(xlDown)).Rows.Count
For x = 1 To NumRows_Vand
Dim r As String
rr = Range("O" & 2 + x).Row
r = "O" & Range("O" & 2 + x).Row
Range(r).Formula = "=IF(IF(IFERROR(VLOOKUP(I" & rr & ";$A$3:$B$" & NumRows_Gist & ";2;FALSE);'Niet vorige upload') <> 'Niet vorige upload'; VLOOKUP(I" & rr & ";$A$3:$B$" & NumRows_Gist & ";2;FALSE) + J" & rr & "; 'correct') <> 0; ''; '0')"
Next
End Sub
Excel Formulas in VBA
Try the following:
Range(r).Formula = "=IF(IF(IFERROR(" _
& "VLOOKUP(I" & rr & ",$A$3:$B$" & NumRows_Gist & ",2,FALSE)," _
& """Niet vorige upload"")<>""Niet vorige upload""," _
& "VLOOKUP(I" & rr & ",$A$3:$B$" & NumRows_Gist & ",2,FALSE)+J" _
& rr & ",""correct"")<>0, """", ""0"")"
I used the following procedure until I got it right:
Option Explicit
Sub testFormula()
Const rr As Long = 5
Const NumRows_Gist As Long = 100
Dim s As String
s = "=IF(IF(IFERROR(" _
& "VLOOKUP(I" & rr & ",$A$3:$B$" & NumRows_Gist & ",2,FALSE)," _
& """Niet vorige upload"")<>""Niet vorige upload""," _
& "VLOOKUP(I" & rr & ",$A$3:$B$" & NumRows_Gist & ",2,FALSE)+J" _
& rr & ",""correct"")<>0, """", ""0"")"
Debug.Print s
End Sub
Writing formulas with VBA is tricky.
You have at least the following mistakes in your formula:
You need to write the formula with comma as separator, not semicolon
You need to write strings within the formula with double quotes, not single quotes. But as the VBA compiler would see a double quote character as end of string, you need to double it: 'Niet vorige upload' should be ""Niet vorige upload"", and at the end the empty string needs to be specified as """"
You should always write the formula into an intermediate variable and check the content using the debugger.
dim f as string
f = "=IF(IF(IFERROR(VLOOKUP ... ' <- Put your formula here
Debug.print f
To get an idea how you need to specify a formula in VBA, change to Excel, enter the desired formula into a cell, change back to the VBA editor and enter ? activeCell.formula into the immediate window.
I searched on internet, without any help coming out of that...
I simply would like to be able to have my VBA code to write this formula in a cell :
=IF(C4="-"; "-"; Cars!C4*C4*Data!$C$8)
As you guessed, there is a page called "Cars" and one called "Data" where I pick the informations needed.
Of course, as it is a VBA code, the C4 will be 2 variables, one for the C and one for the 4 that will evolve...
Actually, I tried this :
Worksheets("Calculation").Range(Column & PosStartCalc + 1).Formula = "=" & "IF(" & Column & PosStartCalc & " = " & "" - "" & ";" & " - " & ";" & "Cars!" & Column & PosStart & "*" & Column & PosStartCalc & "*" & "Data!" & "C" & "8" & ")"
(The variable Column contains the column letter and the variable PosStartCalc contains the row number)
This hurts my eyes and apparently VBA's ones too as it gives the error "Run-Time error '13': Type Mismatch'
Could anyone tell me how to do that?
Thanks in advance !
Try the following, assuming the column variable is a string and row a long variable. I might not have all the variables right, but you'll be able to get what I meant to do here.
Sub test()
Dim Col As String: Col = "C"
Dim Rw As Long: Rw = 4
With ThisWorkbook.Sheets("Calculation")
Debug.Print "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
.Cells(Rw + 1, Col).Formula = "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
End With
End Sub
So what you might forget easily is to use the , as parameter delimiter in a VBA programmed formula. When you put this on your sheet Excel will automatically replace that with the appropriate delimiter for your region.
Another thing to keep in mind; whenever you about to use a string value in such an function, don't forget to wrap it in double quotes!
Don't forget to remove the Debug.print .... line. It was merely there to show the output :)
I have a summary sheet which I use as an index to access several sheets via a hyperlink. I'm extending it so they link directly to the appropriate row within each sheet.
I'm newish to VBA and not sure on the syntax.
I'm basically trying to use the value from variable j as the row number in the cell reference.
"'!A" is the first part of my cell ref in the code below. I'm then trying to concatenate the string to add j as the row number. Can this be done using the code below? I tried surrounding j with brackets, apostrophe to no avail.
For j = 2 To LastUsedRow
link = "=Hyperlink(""#'" & (ThisWorkbook.Worksheets(i).Name) & "'!A" & j",""" & (ThisWorkbook.Worksheets(i).Name) & """)"
Many thanks
Debug.Print is your friend. This is what you get with your current code (assuming j = 1 and ThisWorkbook.Worksheets(i).Name = "Sheet1"):
=Hyperlink("#'Sheet1'!A1,"Sheet1")
The formula should look like this:
=HYPERLINK("#'Sheet2'!A1","Sheet2")
So...add just add the missing ":
link = "=HYPERLINK(""#'" & (ThisWorkbook.Worksheets(i).Name) & _
"'!A" & j & """,""" & (ThisWorkbook.Worksheets(i).Name) & """)"
The including of double quotes within VBA strings is tricky.
link = "=Hyperlink(""#'" & (ThisWorkbook.Worksheets(i).Name) & "'!A" & j & """,""" & (ThisWorkbook.Worksheets(i).Name) & """)"
will get link as =Hyperlink("#'SheetName'!A2","SheetName")
You need to call on what you want to pull from j, such as: j.Value
Hello dear StackOverflow users,
My issue is regarding inserting a Formular via Macro into specific range of cells. Again, again and again.
So the Basic function of the formula should be the following:
=WHEN('ZEK '!F3="X";"6";WHEN('ZEK '!G3="X";"8";"1"))
In my macro it has to Change the column for the next specific range.
This is Happening in a for Loop for i growing.
meaning --> =When('ZEK '!$F$"& i+2 &".......
or in any other Syntax that should work. My try fairly does not, thats why I need your help.
This is my Initial try where i exchanged literally everything with strings:
Sub 123()
Dim a,b,c As String
a = 1
b = 6
c = 8
....
'used and declared variables k, x, f, a, b, c
Range(Cells(k + 2, 5), Cells(k + 27, 5)).FormulaR1C1 = _
"=WHEN('ZEK'!$F$" & f & "=" & x & ";" & a & ";WHEN('ZEK'!$G$" & f & "=" & x & ";" & b & ";" & c & "))"
End Sub
With that i get Runtime Error 1004. (changed it to .Formula from .FormulaR1C1)
I hope i gave enough Information, so that you could help me.
This really does not have to be performant, i just Need to get the formula into about 100.000 cells with i changing for each Range of cells
I think it's a language problem... Try
"=IF('ZEK'!$F$" & f & "=" & x & "," & a & ",IF('ZEK'!$G$" & f & "=" & x & "," & b & "," & c & "))"
Notice the use of IF and , as a delimiter instead of ;. Is WHEN even a valid worksheet function? To be honest I don't know if the worksheet functions get translated or not when setting the formula via VBA. The delimiters are adapted though.
edit: there is also the .FormulaLocal property that should work with ; as delimiter! So change .Formula to .FormulaLocal. This will probably cause an error if you execute it on a machine that uses , as default delimiter so I'd try to stick with .Formula.