strRight with 6 and sometimes 7 characters

ChrisCione

Board Regular
Joined
Aug 27, 2008
Messages
92
Office Version
  1. 365
Platform
  1. Windows
Hello,

A field in my database is for a control number. The control number varies in length, from 16-20 alphanumeric characters. That control number always* ends in 6 numbers, which I extract and copy to the clipboard using VBA.

*On rare occasion, an alpha character (always an alpha character) may be appended to the end of the number. Example:


"Normal" control numbers

19XYT8C8GGKPPC357395

17JDX8C09YT357395


Modified control numbers

19XYT8C8GGKPPC357395D

17JDX8C09YT357395A


Is it possible to copy the last 7 characters ONLY when an alpha character is appended?

Here's the code currently used:

Code:
Dim str As String, strRight As String
On Error GoTo HandleError

str = [txtRPANumber]

strRight = Right(str, 6)
   
Dim clipboard As Object
Set clipboard = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
clipboard.SetText strRight
clipboard.PutInClipboard

HandleExit:
    Exit Sub
HandleError:
    MsgBox "There is no RPA in this record."
    Resume HandleExit

Many thanks in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Please change the code and replace with this:

Dim Nums As String

Code:
  Nums = "0123456789"
  Str = "19XYT8C8GGKPPC357395"
  
  If InStr(Nums, Right(Str, 1)) = 0 Then
    StrRight = Right(Str, 7)
  Else
    StrRight = Right(Str, 6)
  End If
 
Upvote 0
also equally good I guess is using IsNumeric() function:
Code:
  Str = "19XYT8C8GGKPPC357395"
  If IsNumeric(Right(Str, 1)) Then
    StrRight = Right(Str, 6)
  Else
    StrRight = Right(Str, 7)
  End If
 
Upvote 0
I still confuse IsNumeric with IsNumber. IsNumber returns FALSE if you have a cell formatted as text with a number in it.
 
Upvote 0
It won't matter in Access. There is no IsNumber() formula here (and no cells either, I guess strictly speaking). Good point, though as a reminder for users who work in both Excel and Access applications.
 
Last edited:
Upvote 0
Thank you both. I will replace the code tomorrow when I'm back at work.
 
Upvote 0
Thanks again to both of you for the help! Works beautifully!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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