+ Reply to Thread
Results 1 to 6 of 6

Thread: Invalid outside procedure

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    Posts
    327

    Invalid outside procedure

    I'm getting the compile error 'Invalid Outside Procedure' when I'm attempting to run the code below; can anyone help me to correct this? Absolute newbie at code...

    '
    Function ExcelCol(ByVal intCol As Long) As String
    ' Columns can be any number from 1 to x
    ' Will give valid data up to Column ZZ 
    ' ASCII A = 65 so since we're starting with 1
    ' chr(intCol + 64 ) = A
    ' Subtract 1 for because intCol = 1 = 'A'
        
    Dim intRemainder As Integer
    Dim intMod As Integer
    Dim intColumn
    
        If intCol <= 26 Then
            ExcelCol = Chr(intCol + 64)
        Else
            intRemainder = intCol Mod 26
            intMod = intCol \ 26
            
            If intRemainder = 0 Then
                ExcelCol = ExcelCol(intMod - 1) & "Z"
            Else
                ExcelCol = ExcelCol(intMod) & Chr(intRemainder + 64)
            End If
        End If
    End Function
    Last edited by BRISBANEBOB; 10-26-2010 at 05:23 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Invalid outside procedure

    Hello Bob,

    The function works fine for me. As a matter of interest, the macro below does the same thing. It works with Excel 2000 and up.
    Function ExcelCol(ByVal intCol As Long) As String
        ExcelCol = Split(Columns(intCol).Address(False, False), ":")(0)
    End Function
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    Posts
    327

    Re: Invalid outside procedure

    HI Leith

    Thanks for your response. The code I am using still does not work so I am not sure if there is some setting issue with my Excel 2007. Reference your code - which part of my code does it replace (I warned you I was a newbie...)

    Thanks

  4. #4
    Valued Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    Posts
    327

    Re: Invalid outside procedure

    Leith

    I am trying to run a version control so the whole thing looks like this:

    'Function ExcelCol(ByVal intCol As Long) As String
    ' Columns can be any number from 1 to x
    ' Will give valid data up to Column ZZ (If you have more than 26^2 versions, you have bigger problems than this)
    ' ASCII A = 65 so since we're starting with 1
    ' chr(intCol + 64 ) = A
    ' Subtract 1 for because intCol = 1 = 'A'
        
    Dim intRemainder As Integer
    Dim intMod As Integer
    Dim intColumn
    
        If intCol <= 26 Then
            ExcelCol = Chr(intCol + 64)
        Else
            intRemainder = intCol Mod 26
            intMod = intCol \ 26
            
            If intRemainder = 0 Then
                ExcelCol = ExcelCol(intMod - 1) & "Z"
            Else
                ExcelCol = ExcelCol(intMod) & Chr(intRemainder + 64)
            End If
        End If
    End Function
    Sub SaveNextVersion()
      Dim FName As String
      Dim FPath As String
      Dim mySheet As Worksheet
      
      Set mySheet = ThisWorkbook.ActiveSheet
      
      'Capture Information for next versions
      FName = mySheet.Range("A3").Text & " " & mySheet.Range("B3").Text & " " & mySheet.Range("C3").Text
      FPath = Range("H4").Value
          
      'Update so that correct version information saved
      mySheet.Range("A2") = mySheet.Range("A3").Value
      mySheet.Range("B2") = mySheet.Range("B3").Value
      mySheet.Range("C2") = mySheet.Range("C3").Value
      
      'Save File as new Version
      ThisWorkbook.SaveAs Filename:=FPath & "\" & FName
    End Sub

  5. #5
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Invalid outside procedure

    Without reviewing the code re: optimisation...

    You might want to remove the apostrophe on line 1
    Given the Function Header does not exist (commented out) the subsequent code lines are invalid / sitting outside of a procedure.

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    Posts
    327

    Re: Invalid outside procedure

    I cannot believe that even as an absolute code amateur that I did that.

    Many credits to your reputation will be added.

    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0