Simple bit of code needed

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,055
Office Version
  1. 365
Platform
  1. Windows
In column T there are numbers with an R at the beginning eg R 70.00

The number of spaces between the R and the number may vary

I need a bit of code that will replace all of the spaces and R's with nothing, and if it did contain an R the number needs to be turned negative.

Thanks in advance!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi
Try this (enter each line in the Immediate Window (Ctrl+G) of the VBE and press Enter after each one:

Code:
set r = intersect(activesheet.usedrange,range("T:T"))
r.value = evaluate("if(row(),if(left(" & r.address & ")=""R"",-VALUE(MID(" & r.address & ",2,255))," & r.address & "))")
 
Upvote 0
Oops - the R is actually to the right not the left ie 70 R

I had a go at changing left to right in the code, but I think the mid bit is not working properly now.

?
 
Upvote 0
Oops - the R is actually to the right not the left ie 70 R

I had a go at changing left to right in the code, but I think the mid bit is not working properly now.

?
Try this (assumes data starts in T1)

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Convert_R()<br>    <SPAN style="color:#00007F">With</SPAN> Columns("T")<br>        .Replace What:="R", Replacement:="-", LookAt:=xlPart, MatchCase:=False, _<br>            SearchFormat:=False, ReplaceFormat:=False<br>        .TextToColumns Destination:=Range("T1"), DataType:=xlDelimited, Tab:=False, _<br>            Semicolon:=False, Comma:=False, Space:=False, Other:=False, _<br>            FieldInfo:=Array(1, 1), TrailingMinusNumbers:=<SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

If you don't really need code, you can do the same steps manually fairly quickly:

1. Find and replace; Find "R", replace with "-"
2. Text to columns with no delimiters and Trailing minus ticked in the 'Advanced' section of step 3.
 
Upvote 0
Change the r.Value bit to this:
Code:
r.value = evaluate("if(row(),if(right(" & r.address & ")=""R"",-VALUE(LEFT(" & r.address & ",LEN(" & r.address & ")-1))," & r.address & "))")
 
Upvote 0

Forum statistics

Threads
1,214,624
Messages
6,120,591
Members
448,973
Latest member
ksonnia

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