How to Enter a Space in a Macro Code

cklausen

New Member
Joined
May 21, 2015
Messages
8
In a macro, I am trying to enter a formula into a cell and this is the code I am using:

Cells(11, 10).Formula = "=LEFT(I11,(FIND(" ",I11,1)-1))"

The problem is, excel is trying to enter "=LEFT(I11,(FIND(" as the formula's text instead of the entire formula of =LEFT(I11,(FIND(" ",I11,1)-1)). The " " in the formula is the problem, but I don't know how to work around it.

The purpose of the macro is to split the text in I11 into three columns. Here is sample:

07436-72202 AGP STEERING PUMP

to 3 columns of

07436-72202
AGP
STEERING PUMP


Here is the complete macro:

Sub Format()


'Delete Section

Range("B10:H10").Select
Selection.Delete Shift:=xlUp
Range("B10").Select

'Insert Formulas

Cells(11, 9).Formula = "=TRIM(A11)"
Cells(11, 10).Formula = "=LEFT(I11,(FIND(" ",I11,1)-1))"
Cells(11, 11).Formula = "=TRIM(MID(SUBSTITUTE(I11," ",REPT(" ", 100)),100,100))"
Cells(11, 12).Formula = "=RIGHT(I11,LEN(I11)-SEARCH(" ",I11,SEARCH(" ",I11))-4)"
Range("I11:L4000").FillDown

End Sub

Any help is appreciated!
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You just need to double the quotes

Cells(11, 10).Formula = "=LEFT(I11,(FIND("" "",I11,1)-1))"

and the same with the other formula.
 
Last edited:
Upvote 0
Thanks for the response MARK858.

When you double the quotes, it doesn't enter a space in the formula. The formula I am trying to enter requires it to look for the first space in the cell and pull the text to the left of the space.

Any other suggestions?
 
Upvote 0
Thanks for the response MARK858.

When you double the quotes, it doesn't enter a space in the formula. The formula I am trying to enter requires it to look for the first space in the cell and pull the text to the left of the space.

Any other suggestions?
Try this modification to Mark858's suggestion
Cells(11, 10).Formula = "=LEFT(I11,(FIND("&" "&",I11,1)-1))"
 
Upvote 0
Thanks for the response MARK858.

When you double the quotes, it doesn't enter a space in the formula. The formula I am trying to enter requires it to look for the first space in the cell and pull the text to the left of the space.

Any other suggestions?
Code:
Sub sss()
Cells(11, 10).Formula = "=LEFT(I11,(FIND("" "",I11,1)-1))"
End Sub


Works fine for me

Excel Workbook
IJ
114444 mmmk4444
Sheet1
 
Upvote 0
Mark's suggestion is correct. You should have two double quotes, then a space then two more double quotes in the code.
 
Upvote 0
Code:
Sub sss()
Cells(11, 10).Formula = "=LEFT(I11,(FIND("" "",I11,1)-1))"
End Sub


Works fine for me

Sheet1

IJ
114444 mmmk4444

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:137px;"><col style="width:64px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
J11=LEFT(I11,(FIND(" ",I11,1)-1))

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
Actually now that I test Mark's code, it is fine as it is without needing the &'s
 
Upvote 0
I can't believe it was that simple!

They normally are (once you know the answer :) ). Happy it helped and welcome to the forum if nobody has already said it.

@Fishboy
Code:
Cells(11, 10).Formula = "=LEFT(I11,(FIND("&" "&",I11,1)-1))"
will produce the formula
=LEFT(I11,(FIND( ,I11,1)-1))
in the cell which won't actually return the characters to the left of the space.
 
Last edited:
Upvote 0
Thanks again Mark. This is my first post, but I have been learning from this site for a while! Glad you all are around to help out.
 
Upvote 0

Forum statistics

Threads
1,216,468
Messages
6,130,800
Members
449,595
Latest member
jhester2010

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