help me convert VBA

dominko

New Member
Joined
Jul 13, 2023
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
hello,

I have a problem with scanning QR codes, tried everything but everytime I scan with a 2D barcode scanner it scans the whole number from QR code, all I need is the last 4 numbers from it. The serial number. pls if you have any formulas or anything

i have this code that works in excel but in libre doesnt
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Exit if more than one cell updated at once
    If Target.CountLarge > 1 Then Exit Sub
   
'   Only run only column C, on row 2 or below
    If Target.Column = 3 And Target.Row >= 2 Then
'       Trim entry to last four characters
        If Len(Target) > 4 Then
            Application.EnableEvents = False
            Target = Right(Target, 4)
            Application.EnableEvents = True
        End If
    End If

End Sub

Maybe can someone help me write a new one? Thanks a lot

EDIT:
I just need to convert this VBA code to API code if anyone know how I would I appreciate it. thanks a lot
 
Last edited by a moderator:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Problem solved:

Option Explicit

Sub onContentChanged(oEvent As Variant)
Dim oSheet As Variant
Dim oCursor As Variant
Dim oRange As Variant
Dim aData As Variant
Dim i As Long
Rem From the "event" parameter (the object that called the code), we get the current sheet:
oSheet = oEvent.getSpreadsheet()
Rem The easiest and fastest way to determine the area of the sheet being used is
Rem to create a cursor and give it the command "go to the end of the data"
oCursor = oSheet.createCursor()
oCursor.gotoEndOfUsedArea(True)
Rem Now we know the number of the last used row - oCursor.getRangeAddress().EndRow
oRange = oSheet.getCellRangeByPosition(2, 0, 2, oCursor.getRangeAddress().EndRow)
aData = oRange.getDataArray()
Rem Data from the cells of column C in the array, let's process this: '
For i = LBound(aData)+1 To UBound(aData)
aData(i)(0) = Right(aData(i)(0), 4)
Next i
Rem And return the array back to the range cells:
oRange.setDataArray(aData)

then: Look at the screenshot above. You need to right-click on the sheet tab, select the “Sheet events” context menu item, in the form that opens, assign this macro to the “Content changed” event. After that, there is no need to run the macro manually, this will happen automatically when any cell of this sheet is changed.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,148
Messages
6,123,301
Members
449,095
Latest member
Chestertim

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