Syntax error in VBA

Asked

Viewed 1,247 times

2

I need help to resolve a syntax error in Visual basic of Excel, the command is as follows:

Application.ScreenUpdating = False

Dim wsh As Worksheet
Set wsh = Excel.ActiveSheet

Dim data As String
Dim r As Long 'The row index
r = 1
Do
  data = wsh.Cells(r, 1) 'A1, A2, A3...
  If Len(data) = 0 Then Exit Sub 'The code stops at the first empty cell

  Dim rng As Range
  Set rng = wsh.Cells(r, 2) 'Placing barcodes into B1, B2, B3...

  Dim shp As Shape
  Set shp = wsh.Shapes.AddOLEObject("STROKESCRIBE.StrokeScribeCtrl.1")

  shp.LockAspectRatio = msoFalse
  shp.Width = rng.Width
  shp.Height = rng.Height
  shp.Left = rng.Left
  shp.Top = rng.Top

  Dim barcode As StrokeScribe
  Set barcode = shp.OLEFormat.Object.Object

  barcode.Alphabet = QRCODE
  barcode.Text = data

  If barcode.Error Then
    MsgBox barcode.ErrorDescription
    Exit Do
  End If
  r = r + 1
Loop
Application.ScreenUpdating = True
How to find and modify existing barcode objects
Dim sh As Shape
Dim ss As StrokeScribe

Dim wsh As Worksheet
Set wsh = Excel.ActiveSheet

For Each sh In wsh.Shapes
  If sh.Type = msoOLEControlObject And sh.OLEFormat.progID = "STROKESCRIBE.StrokeScribeCtrl.1" Then
      Set ss = sh.OLEFormat.Object.Object
      ss.Alphabet = DATAMATRIX
      ss.Text = "1234ABCD"
  End If
Next
  • 1

    What error occurs?

  • Use the option to snippet and paste the code into it. It looks better to check.

1 answer

0

Well, the error is only of code indentation and a very simple thing in VBA to Excel: you have to put everything inside a subroutine.

Sub exemplo()
'comeco da subrotina
Application.ScreenUpdating = False

'declara variaveis
Dim wsh As Worksheet
Set wsh = Excel.ActiveSheet
Dim data As String
Dim r As Long 'The row index
r = 1
'loop
Do
    data = wsh.Cells(r, 1) 'A1, A2, A3...
    If Len(data) = 0 Then Exit Sub 'The code stops at the first empty cell
    'declara variaveis
    Dim rng As Range
    Set rng = wsh.Cells(r, 2) 'Placing barcodes into B1, B2, B3...
    Dim shp As Shape
    Set shp = wsh.Shapes.AddOLEObject("STROKESCRIBE.StrokeScribeCtrl.1")
        
    shp.LockAspectRatio = msoFalse
    shp.Width = rng.Width
    shp.Height = rng.Height
    shp.Left = rng.Left
    shp.Top = rng.Top
        
    Dim barcode As StrokeScribe
    Set barcode = shp.OLEFormat.Object.Object
        
    barcode.Alphabet = QRCODE
    barcode.Text = data
    
    If barcode.Error Then MsgBox
        barcode.ErrorDescription
        Exit Do
    End If
    r = r + 1
Loop
Application.ScreenUpdating = True

Dim sh As Shape
Dim ss As StrokeScribe
Dim wsh As Worksheet
Set wsh = Excel.ActiveSheet
    
For Each sh In wsh.Shapes
    If sh.Type = msoOLEControlObject And sh.OLEFormat.progID = "STROKESCRIBE.StrokeScribeCtrl.1" Then Set ss = sh.OLEFormat.Object.Object
        ss.Alphabet = DATAMATRIX
        ss.Text = "1234ABCD"
    End If
Next
'fim da subrotina
End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.