Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Ampersand commas and quotations in VBA formula

Posted on 2014-12-04
3
164 Views
Last Modified: 2014-12-04
Hi

I have a this formula that is part of my manual code:

=If(A2=A1,B1&", "&C2,C2)

I want to convert this to a VBA process and I am having trouble with the literals & and " in the output.   Here is what I am trying and it isn't working.

Range("B2:B" & lastrow).Formula = "IF(A2=A1,B1&" & "", "" &  "&C2" & "," & "C2)

It isn't working and I am just not good at putting in the literals.  Help please.
0
Comment
Question by:farmingtonis
3 Comments
 

Author Comment

by:farmingtonis
ID: 40481275
Sorry, forgot to put the = before the if.
0
 
LVL 37

Accepted Solution

by:
Neil Russell earned 500 total points
ID: 40481296
=Range("B2:B" & lastrow).Formula = "IF(A2=A1,B1&" & """, """ &  "&C2" & "," & "C2")
Is that better?
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40481328
@Neilsr's looks good.
In VBA Chr(34) is your friend
It is a double quote
=If(A2=A1,B1&", "&C2,C2)
I Think
=Range("B2:B" & lastrow).Formula = "IF(A2=A1,B1&" & " & chr(34) & ", " & chr(34) & " &  " & C2 ,C2)"
is right

Try
Dim theFormula as string
theFormula =  "IF(A2=A1,B1" 'The first bit
theFormula =theFormula & " & " 'The first ampersand
theFormula =theFormula & chr(34) & ", " & chr(34) 'quotes with a space and comma in them
theFormula =theFormula & " & " 'the second ampersand
theFormula =theFormula & ",C2)" ' the end bit
Range("B2:B" & lastrow).Formula =theFormula

Open in new window


You can then messagebox between each line as you test and you can watch it being built up and catch at each part where something may go wrong.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Auto Apply Receivables Credit Memos to Invoices in Microsoft Dynamics GP 1 136
Problem to adjust sheet 1 92
dialer.exe 4 63
windbg taking it's time 32 93
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question