Expression expected ошибка vba

I am working on some homework and I can’t figure out why my code won’t work. The assignment is to request a name then respond in red text in B10.

Here is my code:

enter image description here

Any help would be great- Thanks!

Community's user avatar

asked Apr 7, 2014 at 1:21

user3504930's user avatar

2

This error comes from the line With RAnge("b10").Font.ColocarIndex = 3, With supose to be used only to objects (not expressions)

sName = Application.InputBox(Prompt:="Please enter your name.", Type:=2)    
With Sheets("MyNewSheet").Range("B10")
    .Font.ColorIndex = 3
    .Value = sName 
End With

answered Apr 7, 2014 at 2:18

Roger Barreto's user avatar

Roger BarretoRoger Barreto

2,0041 gold badge17 silver badges21 bronze badges

Consider:

Sub qwerty()
    Sheets("MyNewSheet").Activate
    With Range("B10")
        .Font.ColorIndex = 3
        strName = Application.InputBox(Prompt:="Please enter your name.", Type:=2)
        .Value = strName
    End With
End Sub

answered Apr 7, 2014 at 2:09

Gary's Student's user avatar

Gary’s StudentGary’s Student

95.8k10 gold badges59 silver badges99 bronze badges

Recently when I run an excel VBA command in excel immediate window, it prompts the error message compile error expected expression. This article will tell you how to fix this error easily.

1. How To Produce The Excel Compile Error Expected Expression.

  1. Open an excel workbook, and click the Developer tab —> Visual Basic icon in the Code group.
  2. It will open the VBA editor window. Click the View —> Immediate Window sub-menu item in the VBA window top menu bar.
  3. Then it will open the immediate window on the VBA window bottom right area.
  4. Input the command Range(‘A1’).End(xlToRight) in the immediate window and press the Enter key to execute it.
  5. Then it will pop up a dialog with the error message like below.
    Compile error:
    Expected: expression.

2. How To Fix The Excel Compile Error Expected Expression.

  1. Fixing this error is very easy, the error happened because VBA uses the single quotes character ( ‘ ) to add a comment.
  2. So what you need to do is to use the double quotes ( ” ” ) to replace the single quotes ” in your excel VBA command.
    Range("A1").End(xlToRight)
    
  3. Now when you press the Enter key to run the VBA command again, the error will disappear.

Выдаёт ошибку Expected: expression
в строке

Range("F1").DataSeriesRowcol := xlColumns,Type:=xlLinear,Step:=x_шаг,Stop:=x_пз,Trend:=False


  • Вопрос задан

  • 3569 просмотров

Присваиваете свойству кучу параметров, будто функцию вызываете…
Может лучше так — Range(«F1»).DataSeriesRowcol xlColumns,Type:=xlLinear,Step:=x_шаг,Stop:=x_пз,Trend:=False

Пригласить эксперта

Никита, здравствуйте.

Между методом DataSeries и его аргументом Rowcol надо поставить пробел.

Ссылка на официальную справку Микрософт ЗДЕСЬ.

Update: не видел, что в комментарии к своему ответу rosperitus уже озвучил про пробел.


  • Показать ещё
    Загружается…

21 сент. 2023, в 13:38

2500 руб./за проект

21 сент. 2023, в 13:29

5000 руб./за проект

21 сент. 2023, в 12:59

15000 руб./за проект

Минуточку внимания

When working with Visual Basic for Applications (VBA), it is not uncommon to come across compile errors. One of the most common compile errors is the ‘Expected: Expression’ message, which can be frustrating to deal with for beginners in particular.

In this article, we will dive deeper into what the ‘Expected: Expression’ message means, its possible causes, and how to resolve it.

Understanding the ‘Expected: Expression’ Error Message

The ‘Expected: Expression’ error message typically pops up when VBA encounters a code that requires an expression, but it cannot find one. An expression, in VBA, is any code that returns a value. When the compiler does not find any valid expression in the code, it returns the ‘Expected: Expression’ error message. The message signifies that VBA expects to find an expression but could not find one in the specific code line.

Possible Causes of the ‘Expected: Expression’ Error

There are several reasons why you may encounter the ‘Expected: Expression’ error message in VBA.

The first one is a syntax error. It occurs when you make a mistake in your VBA code, such as using the wrong operator or omitting a required keyword.

The second possible cause is a logical error. It means that while the syntax of the code is correct, the logic behind it is not. For example, you might have used an incorrect variable or forgot to declare a variable altogether.

The third possible cause is a typographical error. It is when you make a mistake and your code reads differently than what you intended.

Resolving the ‘Expected: Expression’ Error Message

To resolve the ‘Expected: Expression’ error message, you will have to address whatever caused the error in the first place. Below are a few tips on how to do this:

  1. Check the syntax of your code. Double-check and ensure that there are no syntax errors or typo errors.

  2. Ensure that all VBA expressions in the code are valid. Check the code line where you received the error message and ensure that all expressions in that line are valid.

  3. Debug your code. You can use VBA’s built-in debugger tool to locate and resolve the error.

  4. Declare your variables properly. If you are using variables, make sure they have been declared properly and that you are referring to them correctly throughout your code.

  5. Ask for help. If all else fails, it is always an excellent idea to ask for help from experienced VBA programmers or seek out online resources.

In conclusion, the ‘Expected: Expression’ error message in VBA can be frustrating to deal with, but it is not insurmountable. By understanding the possible causes of the error message, you can take steps to address whatever caused it in the first place. By double-checking your code, debugging, and seeking help, you will be well on your way to resolving VBA compile errors with ease.

Hi guys I am really stuck on this error code, Expression expected? It is on line 33 and 46

Wow the formatting really got messed up when I pasted it here sorry but the error is highlighted below

   
Sub Main()

       
‘Declarations

       
Dim intRentalDays
As
Integer = 0

       
Dim dblRentalFee
As
Double = 2.0

       
Dim intDaysLate As
Integer = 0

       
Dim dblLateFees As
Double = 1.5

       
Dim intLateCharges
As
Integer = 0

       
Dim intTotalDue As
Integer = 0

       
‘Get DaysRented

       
getRentalDays(intRentalDays)

       
‘Calculate IfLate

       
calculateIfLate(intRentalDays)

       
‘Calculate Yes

       
CalculateYes(intRentalDays, intDaysLate, dblLateFees, intLateCharges, dblRentalFee, intTotalDue)

       
‘Calculate No

       
CalculateN0(dblRentalFee, intTotalDue)

       
‘Display Total

       
displayTotal(dblRentalFee, intTotalDue)

       
terminateProgram()

   
End Sub

   
Private Sub getRentalDays(ByRef intRentalDays
As Integer)

       
Console.Write(«How many days rented? «)

       
Console.WriteLine()

   
End Sub

This is where the “expression expected error is

   

   

       
If intRentalDays     > 3
Then

   

            
CalculateYes(ByVal intRentalDays     As Integer,
ByVal     intDaysLate As Integer,
ByVal     dblLateFees As Double,
ByVal     intLateCharges As Integer,
ByVal dblRentalFee as Double,
ByVal
intTotalDue As Integer)

   

       
Else

   

           
CalculateN0(ByVal dblRentalFee     As Double,
ByVal     intTotalDue As Integer)

   

   

       
End    
If

              

   
End Sub

   
Private Sub CalculateYes(ByVal intRentalDays
As Integer,
ByVal
intDaysLate As
Integer
, ByRef dblLateFees
As
Double, ByRef intLateCharges
As Integer,
ByVal
dblRentalFee As
Double
, ByRef intTotalDue
As
Integer)

       
intDaysLate = intRentalDays — 3

       
Console.Write(«Your DVD is late»)

       
intLateCharges = CInt((intDaysLate * dblLateFees))

       
Console.Write(«Your late fees are « & intLateCharges)

       
intTotalDue = CInt((dblRentalFee + intLateCharges))

   
End Sub

   
Private Sub CalculateN0(ByVal dblRentalFee
As Double,
ByRef
intTotalDue As
Integer
)

       
intTotalDue = CInt(dblRentalFee)

       
Console.Write(«Thank you for returning on time.»)

   
End Sub

   
Private Sub displayTotal(ByVal dblRentalFee
As Double,
ByRef
intTotalDue As
Integer
)

       
Console.WriteLine(«Your rental fee is « & dblRentalFee)

       
Console.WriteLine(«Your total due is « & intTotalDue)

   
End Sub

   
Private Sub terminateProgram()

       
Console.WriteLine()

       
Console.Write(«Press enter key to terminate the program.»)

       
Console.Read()

   
End Sub

End
Module

Понравилась статья? Поделить с друзьями:
  • Express ошибка 500
  • F01 protherm skat ошибка
  • Express exe ошибка при выключении
  • F01 001 ошибка шакман евро 4
  • Expected array ошибка vba excel