Comments on: Excel
http://ask.metafilter.com/107244/Excel/
Comments on Ask MetaFilter post ExcelWed, 19 Nov 2008 12:14:48 -0800Wed, 19 Nov 2008 12:14:48 -0800en-ushttp://blogs.law.harvard.edu/tech/rss60Question: Excel
http://ask.metafilter.com/107244/Excel
With VBA, how can I create a macro that puts the value of a cell in the middle of the text of another cell? <br /><br /> I want to have two columns (A and B) like this:<br>
<br>
A B<br>
hello ( ) 1<br>
hello ( ) 4<br>
hello ( ) 6<br>
hello ( ) 3<br>
hello ( ) 6<br>
hello ( ) 9<br>
hello ( ) 4<br>
<br>
end up like this:<br>
<br>
hello (1) <br>
hello (4) <br>
hello (6) <br>
hello (3) <br>
hello (6) <br>
hello (9) <br>
hello (4)<br>
<br>
How can I make this happen?post:ask.metafilter.com,2008:site.107244Wed, 19 Nov 2008 11:53:08 -0800amsterdam63excelBy: arniec
http://ask.metafilter.com/107244/Excel#1546475
pretty easy... are the parenthesis always in the same place, or variable locations? (i.e. is it ALWAYS hello<space>( or is it <br>
<br>
hello(<br>
hi (<br>
<br>
etc</space>comment:ask.metafilter.com,2008:site.107244-1546475Wed, 19 Nov 2008 12:14:48 -0800arniecBy: Dec One
http://ask.metafilter.com/107244/Excel#1546483
You don't need VBA. You can do this with some combination of the concatenate, left and right formulas. If the space is variable like arniec is asking about, you'll need the find formula, but that's it.comment:ask.metafilter.com,2008:site.107244-1546483Wed, 19 Nov 2008 12:17:23 -0800Dec OneBy: amsterdam63
http://ask.metafilter.com/107244/Excel#1546488
Yea, it would always be immediately after hello, but the number inside would sometimes be 3 digits.comment:ask.metafilter.com,2008:site.107244-1546488Wed, 19 Nov 2008 12:19:15 -0800amsterdam63By: pompomtom
http://ask.metafilter.com/107244/Excel#1546491
There's a zillion ways to do this, depending upon what is important in your ssimplified (I presume) example.<br>
<br>
Here's one:<br>
<br>
Sub thing()<br>
For Each c In Range("A1:A7")<br>
c.Value = Left(c.Value, 6) & c.Offset(0, 1).Value & Right(c.Value, 1)<br>
Next c<br>
End Subcomment:ask.metafilter.com,2008:site.107244-1546491Wed, 19 Nov 2008 12:21:35 -0800pompomtomBy: arniec
http://ask.metafilter.com/107244/Excel#1546494
Forget the question, here's what you need.<br>
<br>
Sub MergeCols()<br>
<br>
Dim i As Integer<br>
Dim numOfRows As Integer<br>
Dim sourceCol As String<br>
Dim targetCol As String<br>
Dim openParen As Integer<br>
Dim tempstring As String<br>
<br>
numOfRows = 10 'change this to the number of rows you have<br>
targetCol = "A" 'change this to the letter of the column that has "hello ()"<br>
sourceCol = "B" 'change this to the letter of the column that has the number<br>
<br>
For i = 1 To numOfRows<br>
tempstring = Range(targetCol & i)<br>
openParen = InStr(tempstring, "(")<br>
Range(targetCol & i) = Left(tempstring, openParen) & _<br>
Range(sourceCol & i) & Right(tempstring, Len(tempstring) - openParen)<br>
<br>
Range(sourceCol & i) = ""<br>
Next i<br>
End Subcomment:ask.metafilter.com,2008:site.107244-1546494Wed, 19 Nov 2008 12:21:52 -0800arniecBy: amsterdam63
http://ask.metafilter.com/107244/Excel#1546497
Sub hi()<br>
<br>
MyVar = "123"<br>
ActiveSheet.Range("A1").Value = "hello(" & MyVar<br>
<br>
End Sub<br>
<br>
results in hello(123<br>
which is close, but I can't figure out how to get the last ) tacked on there.comment:ask.metafilter.com,2008:site.107244-1546497Wed, 19 Nov 2008 12:22:51 -0800amsterdam63By: bzn
http://ask.metafilter.com/107244/Excel#1546507
Try this:<br>
<br>
Sub hi()<br>
<br>
MyVar = "123"<br>
ActiveSheet.Range("A1").Value = "hello(" & MyVar & ")"<br>
<br>
End Subcomment:ask.metafilter.com,2008:site.107244-1546507Wed, 19 Nov 2008 12:29:48 -0800bznBy: odinsdream
http://ask.metafilter.com/107244/Excel#1546510
Here's the formula I came up with:<br>
<br>
=LEFT(A1,(FIND("()",A1,1)))&B1&")"<br>
<br>
Where column A has your "hello()" and column B has your number.<br>
<br>
If your hello text parentheses have a space between them, add it into the FIND part:<br>
<br>
=LEFT(A1,(FIND("( )",A1,1)))&B1&")"comment:ask.metafilter.com,2008:site.107244-1546510Wed, 19 Nov 2008 12:32:36 -0800odinsdream