Capture and remove last two in a string

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I can remove and/or replace first an last x number of characters from strings but have no idea how to cut it to the clipboard to be used as another string...

I'm scanning a barcode. The barcode reader appends tick symbols at the start and end of the barcode. I remove those, then remove the first few characters from the now numeric (no other symbols or text, just numbers), and before I remove the last two characters from the string I want to copy them to another cell, they are important in their own right. I've got most of it working so far but I'm stuck on the copy and paste of the last two characters. Here's what I'm working with so far... Any direction is always greatly appreciated.
VBA Code:
Option Explicit
Sub EnterTheScan()
Dim Barcode, LMBarcode As String
Dim DateEntered As Date
Dim ws As Worksheet, ws2 As Worksheet
Dim lRow As Long

    With ThisWorkbook
        Set ws = .Worksheets("DataEntry")
        Set ws2 = .Worksheets("Details")
    End With
    
    lRow = ws2.Range("A" & Rows.Count).End(xlUp).Row
    
    ws.Range("B3").Copy
    ws2.Range("A" & lRow + 1).PasteSpecial xlPasteValues
    
    ws2.Range("A" & lRow).Replace what:="`", Replacement:="", _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=True, _
    SearchFormat:=False, ReplaceFormat:=False
    
    lRow = ws2.Range("A" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    
    Barcode = ws2.Range("A" & lRow).Value
    LMBarcode = Right(Barcode, Len(Barcode) - 4)
    LMBarcode = Mid(LMBarcode, 1, Len(LMBarcode) - 3)
    
    ws2.Range("B" & lRow).Value = LMBarcode
    ws2.Range("D" & lRow).Value = Date
    ws2.Range("E" & lRow).Value = Now()
    ws2.Range("G2").Value = ws2.Range("B" & lRow).Value
    
    Application.CutCopyMode = False
    
    ws.Range("E4").Value = lRow - 1
    
    ws.Range("B3").ClearContents
    ws.Range("B3").Select
    Application.ScreenUpdating = True
    
    Exit Sub
    
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi tourless,

The following post has some code that will copy a string to the clipboard.
Hope it helps,

Doug
 
Upvote 0
Thanks Doug. You gave me an idea and I created another variable to hold the value of the last two before I removed them.
VBA Code:
Barcode = ws2.Range("A" & lRow).Value
    LMBarcode = Right(Barcode, Len(Barcode) - 3)
    LMCart = Right(LMBarcode, Len(LMBarcode) - 7)
    LMBarcode = Mid(LMBarcode, 1, Len(LMBarcode) - 2)

Works like a charm!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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