Hi everyone I have a problem with VBA when I try to use vlookup formula like this:
Range("H21").Formula = "=VLOOKUP("cleared",'mortgage'!A2:F12,4,FALSE)"
it keeps telling me the part "cleared" is an syntax error. Can someone tell me how to deal with that? Thank you in advance.
双引号:
Range("H21").Formula = "=VLOOKUP(""cleared"",'mortgage'!A2:F12,4,FALSE)"
Your VBE thinks the word cleared is not part of the string as you've terminated it with double-quotes beforehand . You then appear to be starting a new string immediately after the word cleared.
To use double quotes (aka speech-marks) within a string, you'll need to double them up like so:
Range("H21").Formula = "=VLOOKUP(""cleared"",'mortgage'!A2:F12,4,FALSE)"
Use single quotes around cleared (DOES NOT WORK) :
Range("H21").Formula = "=VLOOKUP('cleared','mortgage'!A2:F12,4,FALSE)"
EDIT : Correction made by OP :
Excel uses single quotes only on the sheet name, not to denote a string literal
Or another option:
Sub TestMe
debug.print selection.formula
End Sub
Option Explicit
Public Sub PrintMeUsefulFormula()
Dim strFormula As String
Dim strParenth As String
strParenth = """"
strFormula = Selection.Formula
strFormula = Replace(strFormula, """", """""")
strFormula = strParenth & strFormula & strParenth
Debug.Print strFormula
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.