Opening chrome browser with disabled websecurity [duplicate] - vbscript

I want to insert an if statement in a cell through vba which includes double quotes.
Here is my code:
Worksheets("Sheet1").Range("A1").Value = "=IF(Sheet1!B1=0,"",Sheet1!B1)"
Due to double quotes I am having issues with inserting the string. How do I handle double quotes?

I find the easiest way is to double up on the quotes to handle a quote.
Worksheets("Sheet1").Range("A1").Formula = "IF(Sheet1!A1=0,"""",Sheet1!A1)"
Some people like to use CHR(34)*:
Worksheets("Sheet1").Range("A1").Formula = "IF(Sheet1!A1=0," & CHR(34) & CHR(34) & ",Sheet1!A1)"
*Note: CHAR() is used as an Excel cell formula, e.g. writing "=CHAR(34)" in a cell, but for VBA code you use the CHR() function.

Another work-around is to construct a string with a temporary substitute character. Then you can use REPLACE to change each temp character to the double quote. I use tilde as the temporary substitute character.
Here is an example from a project I have been working on. This is a little utility routine to repair a very complicated formula if/when the cell gets stepped on accidentally. It is a difficult formula to enter into a cell, but this little utility fixes it instantly.
Sub RepairFormula()
Dim FormulaString As String
FormulaString = "=MID(CELL(~filename~,$A$1),FIND(~[~,CELL(~filename~,$A$1))+1,FIND(~]~, CELL(~filename~,$A$1))-FIND(~[~,CELL(~filename~,$A$1))-1)"
FormulaString = Replace(FormulaString, Chr(126), Chr(34)) 'this replaces every instance of the tilde with a double quote.
Range("WorkbookFileName").Formula = FormulaString
This is really just a simple programming trick, but it makes entering the formula in your VBA code pretty easy.

All double quotes inside double quotes which suround the string must be changed doubled. As example I had one of json file strings : "delivery": "Standard",
In Vba Editor I changed it into """delivery"": ""Standard""," and everythig works correctly. If you have to insert a lot of similar strings, my proposal first, insert them all between "" , then with VBA editor replace " inside into "". If you will do mistake, VBA editor shows this line in red and you will correct this error.

I have written a small routine which copies formula from a cell to clipboard which one can easily paste in Visual Basic Editor.
Public Sub CopyExcelFormulaInVBAFormat()
Dim strFormula As String
Dim objDataObj As Object
'\Check that single cell is selected!
If Selection.Cells.Count > 1 Then
MsgBox "Select single cell only!", vbCritical
Exit Sub
End If
'Check if we are not on a blank cell!
If Len(ActiveCell.Formula) = 0 Then
MsgBox "No Formula To Copy!", vbCritical
Exit Sub
End If
'Add quotes as required in VBE
strFormula = Chr(34) & Replace(ActiveCell.Formula, Chr(34), Chr(34) & Chr(34)) & Chr(34)
'This is ClsID of MSFORMS Data Object
Set objDataObj = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
objDataObj.SetText strFormula, 1
objDataObj.PutInClipboard
MsgBox "VBA Format formula copied to Clipboard!", vbInformation
Set objDataObj = Nothing
End Sub
It is originally posted on Chandoo.org forums' Vault Section.

In case the comment by gicalle ever dies:
I prefer creating a global variable:
Public Const vbDoubleQuote As String = """" 'represents 1 double quote (")
Public Const vbSingleQuote As String = "'" 'represents 1 single quote (')
and using it like so:
Shell "explorer.exe " & vbDoubleQuote & sPath & vbDoubleQuote, vbNormalFocus

Related

VB6 Getting Error when inputting apostrophe on Filter, using ADODB

I'm trying to filter datagrid from a textbox it works but not if apostrophe or ' was typed on the textbox, I'm using ADODB and VB6
Public Sub pGetCustomer(Optional vSearch As String)
If vSearch = "'" Then
xRSTree.Filter = adFilterNone
xRSTree.Requery
Else
xRSTree.Filter = "description like '%" & vSearch & "%' or customercode like '%" & vSearch & "%'"
End If
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
As it says in the ADO documentation (when did people fall into this weird habit of calling ADO "ADODB" anyway???):
Note To include single quotation marks (') in the filter Value, use two single quotation marks to represent one. For example, to filter on O'Malley, the criteria string should be "col1 = 'O''Malley'". To include single quotation marks at both the beginning and the end of the filter value, enclose the string with pound signs (#). For example, to filter on '1', the criteria string should be "col1 = #'1'#".
You must also consider wildcard rules here:
If Operator is LIKE, Value can use wildcards. Only the asterisk (*) and percent sign (%) wild cards are allowed, and they must be the last character in the string. Value cannot be null.
But a little confusingly:
In a LIKE clause, you can use a wildcard at the beginning and end of the pattern (for example, LastName Like '*mit*'), or only at the end of the pattern (for example, LastName Like 'Smit*').
You need to "escape" your qoutes or single qoutes. Simple way would be to replace in vSearch all ' with '' and all " with "".

How to encapsulate a very long string in quotation marks?

I want to print a long VBScript program into a file as one string.
program_str = " long 200 line program"
However, placing quotation marks around so long a string does not work, as it stops recognizing the program as a string as soon as it hits another set of quotation marks. Short of separating each line of the program, and concatenating it, how could I take this long program as a string and paste it into a file?
Two options:
Double any quotes in your string literal:
program_str = "This program has a quote like this "" in it."
Use Chr(34) to specify a quote in your string literal (requires concatenating):
program_str = "This program has a quote like this " & Chr(34) & " in it."
If you want to include speech marks in a string, you must precede each of them with another quotation mark "
e.g.
program_str = "He said ""Hello"""

How to define quotation mark as constant in Visual Basic

Simply I'm trying to define the quotation mark (") as a constant in Visual Basic. However, VB automatically inserts another ", hence I can't make the char equals to an ".
You can also use the ASCII character (34) in Visual Basic like
TextBox1.Text = "She said, " & Chr(34) & "You deserve a treat!" & Chr(34)
You can also define a constant for the character, and use it where needed.
Const quote As String = """"
TextBox1.Text = "She said, " & quote & "You deserve a treat!" & quote
Taken from How to: Put Quotation Marks in a String
Two quote marks make an escaped quote mark.
variable = """"

Need to find character within apostrophe substring and replace it, any ideas?

Ok, let me preface this question with the fact that I am extremely new to vb script, but I need to make an addition to a script that another developer created and he is away.
Problem. :
I am getting data that is feed into a process. The script I already have handles some things that I won't go into. One thing I found out that it doesn't handle correctly is commas that occur in double quotes.
So I get some data like this. :
Joe, Bill, James, Alex, "Google, Inc", Rose, "Kickstarter, LLC"
What happens is that when this script kicks to the process application it blows up because of these commas in the double quotes. I need to be able to have this script go in and replace those commas in the Google and Kickstarter blocks with something a little more unique.
If you have the answer to this I would appreciate it, but I wouldn't mind some direction either of something that does something like this, etc.
Any answers are greatly appreciated. Again very new to vbascript just started reading syntax on it today.
You left out a few details about the format of the input data and the replacement text. However, the approach you need to take is to use the regular expression capability of VBScript to identify patterns in your input data, then use the "Replace" method to replace them. Here's a short MS article about regular expressions in VBScript.
Here's an example of what it might look like. This example is definitely NOT bullet proof and it makes some assumptions, but I think it will get you started:
Dim re, strstr, newstrstr
Dim inputstrarray(7)
Set re = new regexp
inputstrarray(0) = "Joe"
inputstrarray(1) = "Bill"
inputstrarray(2) = "James"
inputstrarray(3) = "Alex"
inputstrarray(4) = """" & "Google, Inc" & """"
inputstrarray(5) = "Rose"
inputstrarray(6) = """" & "Kickstarter, LLC" & """"
re.pattern = ",\s" 'pattern is a comma followed by a space
For Each strstr In inputstrarray 'loop through each string in the array
newstrstr = re.Replace(strstr, "_") 'replace the pattern with an underscore
If StrComp(strstr,newstrstr) Then
Wscript.Echo "Replace " & strstr & " with " & newstrstr
Else
Wscript.Echo "No Change"
End If
Next
Output looks like the following:
No Change
No Change
No Change
No Change
Replace "Google, Inc" with "Google_Inc"
No Change
Replace "Kickstarter, LLC" with "Kickstarter_LLC"
No Change

How to use substring in vbscript within a xsl page

I am trying to replace the double quotes in a string with a single quote, got the following code but get error message saying "Object Required strLocation"
Sub UpdateAdvancedDecisions(strLocation)
Dim d
Dim strLLength
strLLength = Len(strLocation) - 1
For d = 0 To strLLength
alert strLocation
strValue = strLocation.Substring(2,3)
If strLocation.substring(d,d+1)=" " " Then
strLLength = strLLength.substring(0, d) + "'" + strLLength.substring(d + 1,strLLength.length)
Next
End Sub
As Helen said, you want to use Replace, but her example assigned the result to your weird strLLength variable. Try this instead:
strLocation = Replace(strLocation, """", "'")
This one line does the job you asked about and avoids all the code currently in your given subroutine.
Other things that are problems in the code you posted:
a variable holding a number like the length of a string would not have a "str" prefix, so strLLength is misleading
strings in VBScript are indexed from 1 through length, not 0 through length-1
there is no "alert" keyword in VBScript
you assign a value to strValue, then never use it again
you need to use Mid to get a substring, there is no "substring" string method in VBScript
c = Mid(strLocation, d, 1) ' gets one character at position d
The more I look at this, the more clear it is that its some JavaScript that you're trying to run as VBScript but are not translating at all correctly.
Use a reference for VBScript like one of the following:
MSDN Library: VBScript Language Reference
W3Schools VBScript Tutorial

Resources