Expected identifier vba ошибка

You might want to read up a bit on what classes are and how they work.


    Dim va As Class
    Set va = New Class
    'Assign values to every variable in the object
    va.Country = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 0)
    va.Node_name = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 1)
    va.Active = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 4)
    va.From = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 5)
    va.To = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 6)

As you already observed, classes don’t (exactly) work this way. The problem here is that the compiler currently has no idea about the class you tell it to use. You need to define it first. Think of classes as a blueprint to create objects. They contain all the information that is needed for the object to be created, to do some work, and finally: to get destroyed.
To create a class, you need to add a Class Module to your VBA project. Rename it to INeedADescriptiveName (or whatever seems fitting for its purpose, but I’ll go with that name for this little demonstration). Then you add to the class’ definition until it satisfies your requirements, for example as such:

Option Explicit

Private Type TDataContainer
    Country As String
    Node_Name As String
    Active As Boolean
    From As Date
    To As Date
End Type

Private Data As TDataContainer

Public Property Get Country() As String
    ' Some conditional code might go here
    Country = Data.Country
End Property

Public Property Let Country(ByVal inpVar As String)
    ' Validation code here
    Data.Country = inpVar
End Property

Public Property Get Active() As Boolean
    ' Some conditional code might go here
    Active = Data.Active
End Property

Public Property Let Active(ByVal inpVar As Boolean)
    ' Validation code here
    Data.Active = inpVar
End Property

' repeat the above pattern for all other fields you want to expose

With this information in place, you can then create an instance of that class just like you already tried:

Dim va as INeedADescriptiveName
Set va = New INeedADescriptiveName

Then the compiler knows that the object va has the fields va.Country, va.Active, … And you can go ahead and access those fields as you did:

    va.Country = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 0)
    va.Active = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 4)
    ' ...

Of course, only storing data is not what makes a class useful. It would make it a much more useful construct if you also had procedures to go with the data, to transform it in a meaningful way. As such, an easier approach to your problem (as far as I can tell) would be to only use a user-defined type (UDT):

' Add this to the header of your standard module, i.e. above all Subs/Functions in that module:
Private Type TDataContainer
    Country As String
    Node_Name As String
    Active As Boolean
    From As Date
    To As Date
End Type

The next part goes right where you originally had Dim va As Class:

Dim va As TDataContainer
' Since va is now a mere data structure (UDT), not an object, no New-ing up is required
'Assign values to every variable in the UDT
va.Country = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 0)
va.Node_name = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 1)
va.Active = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 4)
va.From = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 5)
va.To = ActiveSheet.ListObjects("Schema").DataBodyRange.Cells(i, 6)

I am trying to figure out an problem for class where the idea is to «shorten» the code using the With command. Below is the code I have so far. I am curious why you cannot use the shtThisSheet variable under the with command when there is a parenthesis directly after the .

Thanks all!

Option Explicit

Sub Formatting()
Dim shtThisSheet As Worksheet
Set shtThisSheet = ActiveWorkbook.Worksheets("Sheet1").Range


With shtThisSheet

    .Font.Bold = True
    .Font.Size = 14
    .HorizontalAlignment = xlLeft
    . ("A3:A6").Font.Bold = True
    .("A3:A6").Font.Italic = True
    .("A3:A6").Font.ColorIndex = 5
    .Range("A3:A6").InsertIndent 1
    .Range("B2:D2").Font.Bold = True
    .Range("B2:D2").Font.Italic = True
    .Range("B2:D2").Font.ColorIndex = 5
    .Range("B2:D2").HorizontalAlignment = xlRight
    .Range("B3:D6").Font.ColorIndex = 3
    .Range("B3:D6").NumberFormat = "$#,##0"

End With
End Sub

Rory's user avatar

Rory

32.8k5 gold badges32 silver badges35 bronze badges

asked Feb 10, 2016 at 4:47

Zack Withrow's user avatar

Option Explicit
Sub Formatting()
Dim shtThisSheet As Worksheet

Set shtThisSheet = ActiveWorkbook.Worksheets("Sheet1")

With shtThisSheet
    With .Range("A3:A6")
        .Font.Bold = True
        .Font.Size = 14
        .HorizontalAlignment = xlLeft
        .Font.Bold = True
        .Font.Italic = True
        .Font.ColorIndex = 5
        .Range("A3:A6").InsertIndent 1
    End With
    With .Range("B2:D2")
        .Font.Bold = True
        .Font.Italic = True
        .Font.ColorIndex = 5
        .HorizontalAlignment = xlRight
    End With
    With .Range("B3:D6")
        .Font.ColorIndex = 3
        .NumberFormat = "$#,##0"
    End With
End With

End Sub

answered Feb 10, 2016 at 4:54

Sixthsense's user avatar

SixthsenseSixthsense

1,9272 gold badges17 silver badges38 bronze badges

1

  • Remove From My Forums
  • Question

  • Hello:

    i’m making a VB 6.0 project to copy files to WCE 5.0 Device. i copied a code i found in msdn but when trying to compile it send me an error: Expected: Identifier

    http://support.microsoft.com/default.aspx/kb/307256

    Private Declare Function WaitForSingleObject Lib «kernel32» (
      ByVal _ hHandle As Long
      ByVal dwMilliseconds As Long) As Long

    anybody have an idea what could be…???

Answers

  • Vb_2007,

    When you see the Expected: Identifier Error message, it means that you use the reserved words in Visual Basic and it is forbidden to use the reserved words as the name of consts or variables.

    According to your description, I suggest you to follow the steps as the KB articles tells you. After you finished implementing this example, just copy and paste some certain code but pay more attention to the names of the variables you use in your project.

    Pay more attention to the edition of your IDE, too. This forum is just about to support the Visual Basic .NET. If you have any other problems on Visual Basic 6 from now on, please post your question on the right forums in order to get better answers.

    In a word, thank you for your question!

Hi all,

I am new to VBA coding, I don’t fully understand some aspects of coding and I’m still learning so please bare with me on this, I am in the middle of making an inventory control workbook with Microsoft Excel to manage my works returns inventory and workload. I’m currently trying to create buttons to hide and unhide sheets to impersonate actual buttons, keep my workbook tidy and just have my Dashboard visible on start up.

On the Dashboard I have the following buttons to apply macros:

— Inventory (In this sheet I will also have 3 buttons needing Macros, Aisle G Inventory, Aisle H Inventory and Inbound Inventory)
— Pending Area
— Testing Overview
— Product Catalogue
— FSE Index
— Branch Index
— Collections
— Meeting Queries
(Every sheet will have a back button to direct me back to the dashboard with all the sheets hidden)

What i wanted to happen previously: (The code I’ve been trying to solve)

— Click ‘Pending Area» button = Unhides ‘Pending Area’ — instantly directs me to ‘Inventory’
— Click ‘Back’ button = Hides ‘Pending Area’ — instantly directs me to ‘Dashboard’

What i want to happen now: (The previous code but updated and now trying to achieve)
— Click ‘Pending Area» button = Unhides ‘Pending Area’ — instantly directs me to ‘Pending Area’ — Hides ‘Dashboard’
— Click ‘Back’ button = Unhides ‘Dashboard’ — instantly directs me to ‘Dashboard’ — Hides ‘Pending Area’
(I want this to happen with each sheet individually)

I have done some online research in regards of doing this and found some threads and youtube video tutorials, which has kind of helped but I’m at a standstill due to a ‘Compile Error: Expected: Identifier’.
I have followed someone’s instructions via a youtube video tutorial and i consistently come to this error (Even though it was working perfectly fine on the tutorial video). I have done research on this error also, during this i found out that it indicates multiple reasons why I receive this error and I cant seem to find/understand where I have gone wrong in the coding or cross reference my coding to the similar issues stated in threads.

My coding what i wanted to happen previously is:

Option Explicit

Sub ViewPendingArea()

Sheets(«Pending Area»).Visible = True
Sheets(«Pending Area»).Select

End Sub

Sub HidePendingArea()

Sheets(«Pending Area»).Visible = False

End Sub

The coding what i want to happen now is:

I haven’t created this yet due to the error I’m receiving with my previous coding, I want to understand where i have went wrong with my previous coding before I update it to my new one. *What i want to happen now is stated above*

Compile Error: Expected: Identifier indications:

I understand that when I receive this error, the debugger will appear and highlight the issue in yellow.

Here is what i receive:

Option Explicit

> Sub ViewPendingArea()

Sheets(«Pending Area»).Visible = True
Sheets(«Pending Area»).Select

End Sub

Sub HidePendingArea()

Sheets(«Pending Area»).Visible = False

End Sub

Keep in mind that I am still learning how to use VBA and some aspects i don’t fully understand so i may not understand everything you explain, so please try and simplify your replies.
I would like someone with professional experience to advise me, Any advice or guidelines will be much appreciated. thank you.

Hi everybody,

I’m trying to create a macro that takes information from certain sheets in one excel workbook and copies it into the sheets with the same name in another workbook. I think the macro below might work, but whenever I try to run it I get the message «compile error — expected:identifier». I’ve tried looking around, but I can’t find what might be causing this.

the error

However, I tried the same thing with a recorded macro that just copies from cell A1 to cell B2 and it gives the same error so I think it has to do with the file. I saved it as .XLSM so macro’s are enabled.

Any tips would be greatly appreciated! Thanks in advance

Edit:CodeBlock

Sub CF()

Dim SheetName As Range
Dim CopyRange As Range
Dim DestinationRange As Range
Dim PL_BS_Template As String
Dim CF_template As String

PL_BS_Template = ThisWorkbook.Path & "\" & "2021.04_PL&BS_template_2.xlsm"
CF_template = ThisWorkbook.Path & "\" & "2021.04_CF_Conso_template_2.xlsx"


Workbooks.Open CF_template

For Each SheetName In Range("sheets")
    If SheetName.Value <> "" And SheetName.Value <> 0 Then
    
    Set CopyRange = Workbooks(PL_BS_Template).Sheets(SheetName.Value).Range("C4:D500")
    Set DestinationRange = Workbooks(CF_template).Sheets(SheetName.Value).Range("C4:D500")
    
    CopyRange.Copy
    DestinationRange.PasteSpecial xlPasteValues
    
    End If
    
Next

End Sub

Понравилась статья? Поделить с друзьями:
  • Explorer exe ошибка файловой системы 2144927436
  • Expected function or variable vba ошибка
  • Explorer exe ошибка файловой системы 2018374635
  • Expected end with ошибка
  • F00a ошибка сети can