+ Reply to Thread
Results 1 to 12 of 12

my vba keeps saying run time error 9 - what does that mean?

  1. #1
    Forum Contributor
    Join Date
    06-19-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2019
    Posts
    151

    my vba keeps saying run time error 9 - what does that mean?

    Hi.

    I have a button from foam controls and linked a 'macro' on the button, so it takes me to a different excel document when i click the button.

    I create the correct macro and assign it to the button. However, once i close the programme the macro stops working and comes up with a warning 'run time error '9'. subscript out of range.

    Why is this happening? and how do i stop this?

    Thanks.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: my vba keeps saying run time error 9 - what does that mean?

    Quote Originally Posted by ACrossley1 View Post
    once i close the programme the macro stops working
    What do you mean by "once i close the programme"

    Please post your code.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Contributor
    Join Date
    06-19-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2019
    Posts
    151

    Re: my vba keeps saying run time error 9 - what does that mean?

    Hi.

    I have two documents:
    1 main excel document - where all my macros run to & 1 sub document (where these issues are coming from)

    There are about 15-20 sub documents all leading back to this 1 main excel document.

    When the main document is open all the macros run perfectly. Once the main document is closed however, the issues start and the macros do not work and come up with that 'run time error 9'

    Im not sure what i can do.

    ive tried to do them all again but they still dont work.

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: my vba keeps saying run time error 9 - what does that mean?

    Why don't you keep your codes in Personal Xlsb?

  5. #5
    Forum Contributor
    Join Date
    06-19-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2019
    Posts
    151

    Re: my vba keeps saying run time error 9 - what does that mean?

    Hi.

    Im not sure how to do that.

    Im not that good on excel, i just know a couple of simple things on the macros, so i link documents to different documents by pressing a simple button.

    Not sure why this is not working. The name of the document it is supposed to 'acitivate' looks correct

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: my vba keeps saying run time error 9 - what does that mean?

    I would get your code only activates the workbook, which is fine if the workbook is already opened.
    If you close the workbook and then try the code it will fail with error 9.

    You need to check the workbook is open, open it if not, and then activate it.

    Does your code look something like this, where Book2 is replaced with the name of your workbook?
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  7. #7
    Forum Contributor
    Join Date
    06-19-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2019
    Posts
    151

    Re: my vba keeps saying run time error 9 - what does that mean?

    Hi.

    Yeah it looks like that.
    So the macro will only work if the document is open?

    I thought it used to work when it wasnt open and would open the document up itself if needed.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: my vba keeps saying run time error 9 - what does that mean?

    It would not automatically open the workbook unless the code explicitly did that. We have not seen your code so can not say exactly.

  9. #9
    Forum Contributor
    Join Date
    06-19-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2019
    Posts
    151

    Re: my vba keeps saying run time error 9 - what does that mean?

    Ahh right ok.

    How would i write a macro whcih would open it if it was not already open?

    An example is shown below:
    Windows("Book2.xlsx").Activate
    Sheets ("1").Select
    Range ("B5").Select

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: my vba keeps saying run time error 9 - what does that mean?

    One approach

    Please Login or Register  to view this content.
    Is the tab name really "1" in your workbook?

  11. #11
    Registered User
    Join Date
    03-29-2020
    Location
    mysore
    MS-Off Ver
    2016
    Posts
    1

    Re: my vba keeps saying run time error 9 - what does that mean?

    i have a similar problem please help... it says "Run-time error subscript out of range"

    code---

    Sub UploadOrUpdate()

    Const Tbl As String = "Query_Management_Temp"
    Const sCell As String = "strt"

    Dim oSht As Worksheet

    Dim fstRow As Integer, _
    lstRow As Integer
    Dim fstCol As Integer, _
    lstCol As Integer

    Dim sHdrRange As String, _
    DataRange As String

    Dim InsertQuery As String, _
    xlRow As Long, _
    xlCol As Integer, _
    tmp As String
    Dim bFlag As Boolean: bFlag = False

    Application.ScreenUpdating = False
    Set oSht = ThisWorkbook.Sheets(ActiveSheet.Name)

    ''mdlSpinner.StartPreloader
    On Error GoTo ErrHandler

    Dim Arr() As String: Arr() = VBA.Split(Application.UserName, ",", -1, vbTextCompare)

    modGlobal.UnprotectSht oSht

    If oSht.Shapes(Application.Caller).TextFrame2.TextRange.Characters.Text = "Submit New Query" Then GenerateQueryRefrenceNumber: bFlag = True

    fstRow = Range(sCell).Row
    lstRow = oSht.Cells(Application.Rows.Count, Range(sCell).Column).End(xlUp).Row
    fstCol = Range(sCell).Column
    lstCol = oSht.Cells(Range(sCell).Row, Application.Columns.Count).End(xlToLeft).Column

    sHdrRange = Cells(fstRow, fstCol).Address & ":" & Cells(fstRow, lstCol).Address

    DataRange = "[" & oSht.Name & "$" & oSht.Range(Cells(fstRow, fstCol).Address, Cells(lstRow, lstCol).Address).Address(0, 0) & "]"

    Dim sSql As String: sSql = GetExlQuery(oSht, sHdrRange, DataRange)
    Dim oRs As ADODB.Recordset

    InitDatabase "Excel", "", ThisWorkbook.FullName, "", "", True

    Set oRs = gobjDB.GetRecordset(sSql)

    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Temp").Delete
    ThisWorkbook.Sheets.Add
    ThisWorkbook.ActiveSheet.Name = "Temp"
    On Error GoTo 0

    If Not oRs.EOF Then
    RecordsetToExcelSheet oRs, Sheets("Temp"), 1, 1 ''1-Row Position & 1-Column Number
    ThisWorkbook.Sheets("Temp").Cells.WrapText = False
    End If

    DestroyDatabase

    xlRow = 2
    With ThisWorkbook.Sheets("Temp")
    While .Cells(xlRow, 1) <> ""

    For xlCol = 1 To .UsedRange.Columns.Count 'Must match the table structure
    If tmp = vbNullString Then
    tmp = "('" & IIf(Replace(.Cells(xlRow, xlCol), "'", "`") = vbNullString, "NULL", Replace(.Cells(xlRow, xlCol), "'", "`")) & "'"
    Else
    tmp = tmp & ",'" & IIf(Replace(.Cells(xlRow, xlCol), "'", "`") = vbNullString, "NULL", Replace(.Cells(xlRow, xlCol), "'", "`")) & "'"
    End If
    Next xlCol

    If xlRow = .UsedRange.Rows.Count Then
    If InsertQuery = vbNullString Then InsertQuery = tmp & ")" & vbNewLine Else InsertQuery = InsertQuery & tmp & ")" & vbNewLine
    Else
    If InsertQuery = vbNullString Then InsertQuery = tmp & ")," & vbNewLine Else InsertQuery = InsertQuery & tmp & ")," & vbNewLine
    End If

    tmp = vbNullString
    xlRow = xlRow + 1
    Wend

    ''Debug.Print "INSERT INTO " & Tbl & " VALUES " & InsertQuery
    InsertQuery = VBA.Replace(InsertQuery, "'NULL'", "NULL", 1, -1, vbTextCompare)
    ''Debug.Print "INSERT INTO " & Tbl & " VALUES " & InsertQuery

    End With
    If Not InsertQuery = vbNullString Then

    modGlobal.InitDatabase "SQLServer", sServerName, sDatabaseName, sUserID, sPassword

    gobjDB.ExecuteActionQuery ("TRUNCATE TABLE [Reporting].[dbo].[Query_Management_Temp]")

    gobjDB.ExecuteActionQuery ("INSERT INTO " & Tbl & " VALUES " & InsertQuery)

    If oSht.Shapes(Application.Caller).TextFrame2.TextRange.Characters.Text = "Submit New Query" Then
    oSht.Range("A7:AZ100000").ClearContents
    ''sSql = "EXEC Reporting..[Sp_Query_Management_Insert] '" & VBA.Trim(Arr(UBound(Arr))) & " " & VBA.Trim(Arr(LBound(Arr))) & "','" & VBA.Format(Now, "YYYY-MM-DD HH:MM:SS AM/PM") & "'"
    sSql = "EXEC Reporting..[Sp_Query_Management_Insert] '" & VBA.Trim(Environ("UserName")) & "','" & VBA.Format(Now, "YYYY-MM-DD HH:MM:SS AM/PM") & "'"
    Else
    ''sSql = "EXEC Reporting..[Sp_Query_Management_Update] '" & VBA.Trim(Arr(UBound(Arr))) & " " & VBA.Trim(Arr(LBound(Arr))) & "','" & VBA.Format(Now, "YYYY-MM-DD HH:MM:SS AM/PM") & "'"
    sSql = "EXEC Reporting..[Sp_Query_Management_Update] '" & VBA.Trim(Environ("UserName")) & "','" & VBA.Format(Now, "YYYY-MM-DD HH:MM:SS AM/PM") & "'"

    End If

    gobjDB.ExecuteActionQuery (sSql)

    modGlobal.DestroyDatabase

    ''mdlSpinner.FinishPreloader

    If bFlag = True Then
    MsgBox "Submission of new queries was successful.", vbInformation + vbOKOnly, "New Queries Submission"
    Else
    MsgBox "Queries update was successful.", vbInformation + vbOKOnly, "Update Existing"
    End If

    End If

    ErrHandler:
    'newsteps
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Temp").Delete

    Application.DisplayAlerts = True
    On Error GoTo 0

    Range(sCell).Activate

    modGlobal.ProtectSht oSht

    ''mdlSpinner.FinishPreloader

    Set oSht = Nothing
    Application.ScreenUpdating = True


    End Sub

  12. #12
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,447

    Re: my vba keeps saying run time error 9 - what does that mean?

    @nams

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Error message: Run-Time error '1004 '; application-defined or object-defined error
    By Davasu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2015, 06:52 AM
  2. Replies: 1
    Last Post: 10-22-2015, 05:50 AM
  3. VBA ERROR: run time error 1004: Application-defined or Object-defined error in excel 2013
    By AnanthKrishna in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-13-2015, 06:16 AM
  4. Cannot Publish Excel Pivot Chart - Run Time Error
    By crisb184 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-11-2013, 07:04 AM
  5. [SOLVED] Error " Run-time error '1004': application defined or object defined error
    By lengwer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2013, 07:26 AM
  6. run-time error ;2147023179 (800706b5) time automation error interface unknown
    By karthik72 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-02-2012, 09:31 AM
  7. Error "run-time Error '1004': General Odbc Error
    By D4WNO77 in forum Access Tables & Databases
    Replies: 2
    Last Post: 07-16-2012, 09:55 AM

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.6.0 RC 1