Erase specific amount of characters from a string in VBA

adibakale

Board Regular
Joined
Apr 10, 2015
Messages
52
I have some VBA code in Access which inserts data from a form in Access into a Word Document.

Code:

.ActiveDocument.Bookmarks("XXXX").Select
.Selection.Text = (CStr(Forms!frm_Main!ACCOUNT_NUMBER))

The account number in Access is 16 digits long. I have it working to where it will insert the 16 digit account number into Word where I have the (XXXX) bookmarked.

What I need to do is only insert the last 4 digits of the 16 digit account number into word. I need to either remove the first 12 digits, or only select the last 4 digits from the account number.

Any help with this would be greatly appreciated - I can provide more details if necessary.

Thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You should be able to peel off the last 4 digits with the "Right" string function.

Code:
Dim n As String
n = "1001 2002 3003 4004"
n = Right(n, 4)
MsgBox n
 
Upvote 0
Thanks for your help. I think this should work.

Can you please explain these 2 lines to me and their purpose?

n = "1001 2002 3003 4004"
MsgBox n
 
Upvote 0
They serve no purpose other than to create a string that is at least 16 chars long, and then show you a four digit result in a msgbox.

I probably could've just as easily said, "Go look for examples of how to use the "Right" string function in VBA."

Your application might be:
Code:
.Selection.Text = Right(CStr(Forms!frm_Main!ACCOUNT_NUMBER),4)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,424
Members
448,896
Latest member
MadMarty

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