Statement invalid outside type block ошибка vba

You have:

excel – VBA Compile Error: Statement invalid outside Type Block

You have:

Dim RangeNOut as Double
Dim RangeNOut as Integer

While the IF statements in there are a nice idea, VBA will not allow you to do that. It doesnt do conditional compilation since it isnt a compiled language. When VBA runs your code, all your variables are declared (no matter where in the code the Dim statement is located), then the code begins executing.

Its a nice idea youve come up with, but attempting this in VBA is like bringing a piece of Silly Putty™ to a gun fight – its just horribly unequipped for the job.

Also, if youre that concerned about execution speed, VBA isnt your weapon of choice, either. I dont have any stats to back it up off the top of my head, but I doubt youd actually see much difference in execution speed based on your three different variable types.

To pass the variable type as a parameter to the function, use this:

Private Sub Function1(ByVal VarType as String)

  #If VarType = Double then
    ...
  #ELSEIF VarType = Single then
    ...
  #ELSEIF VarType = Integer then
    ...
  #ELSE
    MsgBox You passed in a VarType of  & VarType &  - thats not valid
  #ENDIF

Also, I just noticed that in your final Else you have Goto Function1. Im not sure what youre trying to accomplish there, but:

  1. Dont use goto. Except in VBA style error handling, its almost never necessary
  2. You dont have a label defined for the Goto to jump to, anyway.

See also VBA function overloading for another possible option.

Notice: Despite the upvotes and accepted answer status, I tried the following, and it DOES NOT work as requested by OP:

Sub test()
  func Double
  func Single
  func Integer
  func String
End Sub

Function func(v As String)
  #If v = Double Then
    Dim myvar As Double
    Range(A1) = MyVar type is:  & vartype(v)
  #ElseIf v = Single Then
    Dim myvar As Single
    Range(a2) = MyVar type is:  & vartype(v)
  #ElseIf v = Integer Then
    Dim myvar As Integer
    Range(a3) = MyVar type is:  & vartype(v)
  #Else
    Range(A4) = Invalid var type passed:  & v
  #End If

  MsgBox Passed in  & v

End Function

All calls to Func() end up in the #Else section of code, populating Range(A4) with the Invalid var type passed: text.

Sadly, this will not work.

If it is truly necessary to have functions with different variable types doing the exact same thing, I think the following would be the best bet:

Sub Test()
  Dim VType as String

  While Vtype <> Integer and VType <> Double and VType <> Single and VType <> Cancel
    vType = msgBox(Enter variable type)
  Wend

  If VType = Integer then
    MyFuncInt()
  ElseIf VType = Double then
    MyFuncDouble()
  Elseif VType = Single
    MyFuncSingle()
  Else
    MsgBox Function call cancelled
  End if
End Sub

Function MyFuncInt()
  Dim AllTheVars as Integer
  ...
End Function

Function MyFuncDouble()
  Dim AllTheVars as Double
  ...
End Function

Function MyFuncSingle()
  Dim AllTheVars as Single
  ...
End Function

excel – VBA Compile Error: Statement invalid outside Type Block

  • #1

I’m getting the error in the title of the thread and VBA highlights the time_lastcell2 as the culprit.

Code:

Dim first_cell As Integer
Dim time2 As Worksheet
Set time2 = ActiveWorkbook.Sheets("time")


Dim lastday As Date, lastday_time As Date
time_lastcell2 As Integer


With time2
time_lastcell2 = .Range("e65000").End(xlUp).Row
End With


lastday_time = time2.Range("e" & time_lastcell).Address




'Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
    Application.EnableEvents = False




With ActiveSheet
first_cell = .Range("c65000").End(xlUp).Row


lastday = .Range("b" & first_cell).Address

[more code]

Workdays for a market open Mon, Wed, Friday?

Yes! Use «0101011» for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

  • #2

kylefoley76,

Missing Dim….

Dim time_lastcell2 As Integer

  • #3

Missing Dim….

Dim time_lastcell2 As Integer

Also, since it will hold a row number, I would suggest declaring it as a Long instead of as an Integer…

Dim time_lastcell2 As Long

  • #4

Thanks that did it. Also Rick, about the fact that I thought you didn’t like one-liners expressing gratitude. I thought I read that on excelfox but I went back to look for it and I couldn’t find it.

  • Home
  • VBForums
  • Visual Basic
  • Visual Basic 6 and Earlier
  • Statement Invalid outside TypeBlock error

  1. Dec 20th, 2005, 05:27 PM


    #1

    VTuser is offline

    Thread Starter


    New Member


    Question Statement Invalid outside TypeBlock error

    I am getting the following error

    Compile error
    Statement Invalid outside TypeBlock

    «Function DecimalTime» is highlighted in yellow

    what does it mean?? and thoughts on how to fix it??

    code below

    Private Sub cmdIn_Click()
    TimeIn = DecimalTime
    AddToDB
    End Sub

    Function DecimalTime()

    ClockTime As Date

    If Minute(Time) >= 0 And Minute(Time) <= 7 Then
    ClockTime = DateAdd(«n», -Minute(Time), Time)
    ElseIf Minute(Time) >= 8 And Minute(Time) <= 22 Then
    ClockTime = DateAdd(«n», 15 — Minute(Time), Time)
    ElseIf Minute(Time) >= 23 And Minute(Time) <= 37 Then
    ClockTime = DateAdd(«n», 30 — Minute(Time), Time)
    ElseIf Minute(Time) >= 38 And Minute(Time) <= 52 Then
    ClockTime = DateAdd(«n», 45 — Minute(Time), Time)
    Else
    ClockTime = DateAdd(«n», 60 — Minute(Time), Time)
    End If

    DecimalTime = Format(ClockTime * 24, «0.00»)

    End Function


  2. Dec 20th, 2005, 05:36 PM


    #2

    Re: Statement Invalid outside TypeBlock error

    Shouldn’t your function be declared to return something????


  3. Dec 20th, 2005, 05:40 PM


    #3

    Re: Statement Invalid outside TypeBlock error

    Functions return Variants by default so that’s not the problem, but where do you have the function coded? Is there a Type block above it that doesn’t have an End Type?


  4. Dec 20th, 2005, 05:45 PM


    #4

    Re: Statement Invalid outside TypeBlock error

    Just change ‘ClockTime As Date’ to ‘Dim ClockTime As Date’.

    Ash Nazg durbatuluk, Ash Nazg gimbatul, Ash Nazg tharkathuluk, Agh barzum-ishi krimpatul.


  5. Dec 20th, 2005, 05:46 PM


    #5

    Re: Statement Invalid outside TypeBlock error

    ‘Dim’ is required for declaring variables whenever it’s outside a «Type» block.

    Ash Nazg durbatuluk, Ash Nazg gimbatul, Ash Nazg tharkathuluk, Agh barzum-ishi krimpatul.


  6. Dec 20th, 2005, 05:48 PM


    #6

    Re: Statement Invalid outside TypeBlock error

    Quote Originally Posted by Frodo_Baggins

    Just change ‘ClockTime As Date’ to ‘Dim ClockTime As Date’.

    Of course! I missed that because he said that the function header was the problem.


  7. Dec 20th, 2005, 06:57 PM


    #7

    VTuser is offline

    Thread Starter


    New Member


    Re: Statement Invalid outside TypeBlock error

    since I am dealing with the same app I have two more issues

    I am entering the system time, which I am converting to decimal into an access 2003 database field called IN that has data type date/time

    two issues

    1. the system time is 6:37PM the time that’s entered into the access database is 6:51PM

    as you can notice I am rounding up or down depending on the minutes in 15 minute intervals.
    Thus why am I getting that extra minute added. I think it has to do with the access database but not sure
    any ideas????

    2. How can I change in access to the field does not read 6:51PM but 6.50 (decimal format))

    any ideas

    Private Sub cmdIn_Click()
    TimeIn = DecimalTime
    AddToDB
    End Sub

    Function DecimalTime()

    Dim ClockTime As Date

    If Minute(Time) >= 0 And Minute(Time) <= 7 Then
    ClockTime = DateAdd(«n», -Minute(Time), Time)

    ElseIf Minute(Time) >= 8 And Minute(Time) <= 22 Then
    ClockTime = DateAdd(«n», 15 — Minute(Time), Time)

    ElseIf Minute(Time) >= 23 And Minute(Time) <= 37 Then
    ClockTime = DateAdd(«n», 30 — Minute(Time), Time)

    ElseIf Minute(Time) >= 38 And Minute(Time) <= 52 Then
    ClockTime = DateAdd(«n», 45 — Minute(Time), Time)

    Else
    ClockTime = DateAdd(«n», 60 — Minute(Time), Time)
    End If

    DecimalTime = Format(ClockTime * 24, «0.00»)

    Private Sub AddToDB()
    rs.AddNew
    rs.Fields(«IN») = TimeIn
    rs.Fields(«Site») = cboSites.Text
    rs.Fields(«Activity») = cboActivity.Text
    rs.Update
    End Sub


  8. Dec 20th, 2005, 07:00 PM


    #8

    Re: Statement Invalid outside TypeBlock error

    Please use vbcode tags in your post for your code. It makes it more readable.


  9. Dec 20th, 2005, 07:05 PM


    #9

    Re: Statement Invalid outside TypeBlock error

    BTW: Don’t understand what you are doing. 6:37 + 15 = 6:52… Better example please…


  10. Dec 21st, 2005, 11:04 AM


    #10

    VTuser is offline

    Thread Starter


    New Member


    Re: Statement Invalid outside TypeBlock error

    Example

    The system time is 6:37PM

    The click event for cmdIn

    Calls the decimaltime function

    The decimaltime function using if elseif statements determine the statement is applied based on the minutes of the system time. In this case 6:37PM has 37 minutes thus

    ElseIf Minute(Time) >= 23 And Minute(Time) <= 37 Then
    ClockTime = DateAdd(«n», 30 — Minute(Time), Time)

    Is applied and it convert 37 minutes to 30 minutes and the system time know is 6:30PM
    The reason for this is that I want to capture time in 15 intervals only

    From there

    DecimalTime = Format(ClockTime * 24, «0.00»)

    Takes 6:30PM and converts it to decimal format which is 6.50 then that value is placed in the variable TimeIn

    Then it�s added to the database using the following procedure

    VB Code:

    1. Private Sub AddToDB()

    2. rs.AddNew

    3. rs.Fields("IN") = TimeIn

    4. rs.Fields("Site") = cboSites.Text

    5. rs.Fields("Activity") = cboActivity.Text

    6. rs.Update

    7. End Sub

    I hope this clears things up and that you have an idea how to solve my issues. Thanks

    VB Code:

    1. Private Sub cmdIn_Click()

    2. TimeIn = DecimalTime

    3. AddToDB

    4. End Sub

    5. Function DecimalTime()

    6. Dim ClockTime As Date

    7. If Minute(Time) >= 0 And Minute(Time) <= 7 Then

    8. ClockTime = DateAdd("n", -Minute(Time), Time)

    9. ElseIf Minute(Time) >= 8 And Minute(Time) <= 22 Then

    10. ClockTime = DateAdd("n", 15 - Minute(Time), Time)

    11. ElseIf Minute(Time) >= 23 And Minute(Time) <= 37 Then

    12. ClockTime = DateAdd("n", 30 - Minute(Time), Time)

    13. ElseIf Minute(Time) >= 38 And Minute(Time) <= 52 Then

    14. ClockTime = DateAdd("n", 45 - Minute(Time), Time)

    15. Else

    16. ClockTime = DateAdd("n", 60 - Minute(Time), Time)

    17. End If

    18. DecimalTime = Format(ClockTime * 24, "0.00")

    19. Private Sub AddToDB()

    20. rs.AddNew

    21. rs.Fields("IN") = TimeIn

    22. rs.Fields("Site") = cboSites.Text

    23. rs.Fields("Activity") = cboActivity.Text

    24. rs.Update

    25. End Sub

    Edit: Fixed [vbcode][/vbcode] tags. — Hack

    Last edited by Hack; Dec 21st, 2005 at 11:07 AM.


  11. Dec 21st, 2005, 11:36 AM


    #11

    Re: Statement Invalid outside TypeBlock error

    You don’t want to do a dateadd when checking. All you want to do is take the Hour then concatenate the decimal minutes you figured out to that. You are over complicating the whole process.


  12. Dec 21st, 2005, 11:43 AM


    #12

    VTuser is offline

    Thread Starter


    New Member


    Re: Statement Invalid outside TypeBlock error

    can you if me an example

    thanks


  13. Dec 21st, 2005, 11:55 AM


    #13

    Re: Statement Invalid outside TypeBlock error

    Here’s an example:

    VB Code:

    1. Dim mTime As Time

    2. mTime = Time

    3. Select Case mTime

    4.     Case >= 8 and <= 22

    5.         decHours = decHours & ".15"

    6.     Case >= 22 and <= 37

    7.         decHours = decHours & ".30"

    8. End Select


  14. Dec 21st, 2005, 11:57 AM


    #14

    Re: Statement Invalid outside TypeBlock error

    Quote Originally Posted by randem

    Here’s an example:

    VB Code:

    1. Dim mTime As Time

    2. mTime = Time

    3. Select Case mTime

    4.     Case >= 8 and <= 22

    5.         decHours = decHours & ".15"

    6.     Case > 22 and <= 37

    7.         decHours = decHours & ".30"

    8. End Select

    Select Case isn’t going to like this. You would need to do something like

    VB Code:

    1. Case 8 To 22

    2. Case 23 To 37


  15. Dec 21st, 2005, 12:15 PM


    #15

    Re: Statement Invalid outside TypeBlock error


  • Home
  • VBForums
  • Visual Basic
  • Visual Basic 6 and Earlier
  • Statement Invalid outside TypeBlock error


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules


Click Here to Expand Forum to Full Width

  1. 06-04-2010, 04:15 PM


    #1

    biobee07 is offline


    Registered User


    error:Statement invalid outside type block

    I am trying to sort a number of columns in my worksheet based on the Cell Color. The columns have variable length of rows.
    I have two questions:
    1. When I run my current code in debug mode then I get the error: «Statement invalid outside type block». I am not sure which parantheses I should remove to get rid of the error.
    2.Ssecondly I think my current code is not going to do the sort that I would like it to do. Can anyone please suggest what changes I should make in my code to get it working.

    Below I have pasted a sample of my sheet as a link:
    http://picasaweb.google.com/manishab…14238835510658


  2. 06-04-2010, 04:20 PM


    #2

    Re: error:Statement invalid outside type block

    1. Which line gives you the error?
    2. Why post a picture, rather than a sample workbook? It would also help if you explained why you think the sort won’t do what you want, as well as what you actually do want.

    Remember what the dormouse said
    Feed your head


  3. 06-04-2010, 05:06 PM


    #3

    biobee07 is offline


    Registered User


    Re: error:Statement invalid outside type block

    Quote Originally Posted by romperstomper
    View Post

    1. Which line gives you the error?
    2. Why post a picture, rather than a sample workbook? It would also help if you explained why you think the sort won’t do what you want, as well as what you actually do want.

    Thanks romperstomper for replying.

    1. I was able to solve the first problem of the error: Statement invalid outside type block just a few minutes back. I had forgotten to add Dim infront of myRange

    2. I need help with the second part that is sort the rows in each column according to color such that the Red colored cells are on the top and the blue colored on the bottom for each column.

    I pasted a link of the picture of the worksheet to give an example of the format of the data I am dealing with. This time I attach the worksheet to this post for convinience of the readers.

    below is also the edited code that doesnot throw the Statement invalid outside type block error


  4. 06-04-2010, 05:28 PM


    #4

    Re: error:Statement invalid outside type block


  5. 06-04-2010, 07:11 PM


    #5

    biobee07 is offline


    Registered User


    Re: error:Statement invalid outside type block

    thanks a lot!!!!! It worked fine. thanks for your help. My code was quite complicated.

    Quote Originally Posted by romperstomper
    View Post


Понравилась статья? Поделить с друзьями:
  • State react как изменить
  • State of survival ошибка 42002
  • State of decay ошибка при запуске приложения 0xc0000142
  • State of decay ошибка 0xc0000906
  • State of decay yose day one edition crash dump error как исправить