VBA adding space in concatenate formula

silentcarl

New Member
Joined
Feb 29, 2024
Messages
8
Office Version
  1. 2010
Platform
  1. Windows
How do I add a space in this instance?
VBA Code:
.Formula = "=CONCATENATE(RawData!A" & i & "," & " " & ",RawData!B" & i & ")"

I tried & " " & then tried & "" "" &.

The second one gives a compile error: Expected: End of statement.


Thanks!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try

VBA Code:
.Formula = "=CONCATENATE(RawData!A" & i & ","" """ & ",RawData!B" & i & ")"
 
Upvote 1
Solution
Try

VBA Code:
.Formula = "=CONCATENATE(RawData!A" & i & ","" """ & ",RawData!B" & i & ")"
Life saver. Thank you. I will mark it solved in a few. BUT, can you please help me with this next one?
VBA Code:
.Formula = "=IF(RawData!D" & 1 + (i + 3) / 6 & "="","",RawData!D" & 1 + (i + 3) / 6 & ")"
returns:
Excel Formula:
=IF(RawData!D2=",",RawData!D2)

But I need:
Excel Formula:
=IF(RawData!D2="","",RawData!D2)
 
Upvote 0
solved:
VBA Code:
.Formula = "=IF(RawData!D" & 1 + (i + 3) / 6 & "="""","""",RawData!D" & 1 + (i + 3) / 6 & ")"
 
Upvote 0
Life saver. Thank you. I will mark it solved in a few. BUT, can you please help me with this next one?
VBA Code:
.Formula = "=IF(RawData!D" & 1 + (i + 3) / 6 & "="","",RawData!D" & 1 + (i + 3) / 6 & ")"
returns:
Excel Formula:
=IF(RawData!D2=",",RawData!D2)

But I need:
Excel Formula:
=IF(RawData!D2="","",RawData!D2)
in this case maybe you have to be observant in reading comma punctuation and double quotes, I think you understand now😉
 
Upvote 0
As you have found, breaking up a formula like that to introduce variables can easily lead to confusion/errors.
I find the following method generally leads to less of both

.Formula = Replace("=CONCATENATE(RawData!A#,"" "",RawData!B#)", "#", i)

The blue part looks just like the actual formula that will be in the cell, apart from doubling up the quote marks and the # being a place-holder for the variable you want to insert.
Then the red part simply drops the variable value in where it is required.

For your second example it would look like this. Note that it also means that calculation of row number based on the variable i only needs to be done once.

.Formula = Replace("=IF(RawData!D#="""","""",RawData!D#)", "#", 1 + (i + 3) / 6)
 
Upvote 1

Forum statistics

Threads
1,224,547
Messages
6,179,436
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top