Extracting the 2nd Character From the Right in a String

Philip1957

Board Regular
Joined
Sep 30, 2014
Messages
185
Office Version
  1. 365
Platform
  1. Windows
Greetings,

I have 3 and 4 character strings (column E) and I need to extract the second character from the right and place it in another cell (column O).
My code is giving me the last 2 characters. Is there a way to get just the second character or do I have to run a second macro to separate them?

I also need this to step & repeat through the selected range in column O but I haven't started that yet.

VBA Code:
Option Explicit

Sub ExtractLocCode()

Application.ScreenUpdating = False


Dim CodeString
CodeString = ActiveCell.Offset(0, -10).Value

Dim LocCode
LocCode = Right(CodeString, 2)     ' Returns Loc code.

ActiveCell.Value = LocCode


Application.ScreenUpdating = True


End Sub 'ExtractLocCode


Thanks in advance for any assistance,
~ Phil
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
This is the line that is returning the last 2.
Excel Formula:
LocCode = Right(CodeString, 2)

So just take the first character of that, by updating that line to:
Excel Formula:
LocCode = Left(Right(CodeString, 2),1)
 
Upvote 0
You can also use the MID and LEN functions to achieve this.

Secondly you can avoid a loop by evaluating a formula using MID and LEN functions. The formula to do this might be:
Excel Formula:
=MID(E2:E9,LEN(E2:E9)-1,1)

You can evaluate an entire range in Excel 365 using something like the below, which is basically the formula above but run on a range using VBA:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant
   
    Set rng = Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row) ' Range of 3 and 4 character strings
   
    var = Evaluate("MID(" & rng.Address & ",LEN(" & rng.Address & ")-1,1)") ' Evaluate the formula using the above range
    Range("O2").Resize(UBound(var), 1) = var ' Output the evaluated result to a cell, in this case cell O2
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,756
Messages
6,174,320
Members
452,555
Latest member
colc007

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