+ Reply to Thread
Results 1 to 11 of 11

InputBox Error.

Hybrid View

  1. #1
    Registered User
    Join Date
    06-13-2008
    Posts
    21

    InputBox Error.

    Hi,

    I am using an inputbox to get a range a data from the user.

    Set rngToChart = Application.InputBox(Prompt:="Select the range you wish to chart!", Title:="Select a range", Type:=8)
    This works ok if the user selects a range, however if i select the cancel button i get a Run-time error 424, object required.

    jonnyuk3

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good morning jonnyuk3

    There isn't a dedicated method to catch cancel from the Application.Inputbox object. Just use Excel's generic error trapping method immediately before calling the input box, and reset it straight after, thus :
    On Error Resume Next
    Set rngToChart = Application.InputBox(Prompt:="Select the range you wish to chart!", Title:="Select a range", Type:=8)
    On Error GoTo 0
    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    06-13-2008
    Posts
    21
    Hi Dominic,

    I've tried what you suggested and i still get the same error.

    On Error Resume Next
        Set rngToChart = Application.InputBox(Prompt:="Select the range you wish to chart!", Title:="Select a range", Type:=8)
    On Error GoTo 0

  4. #4
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi jonnyuk3

    This code runs fine on its own. The error must be that the empty variable is causing a problem further on. Use something like this ...

    On Error GoTo Last
        Set rngToChart = Application.InputBox(Prompt:="Select the range you wish to chart!", Title:="Select a range", Type:=8)
    On Error GoTo 0
    
    '  More code goes here
    
    Last:
    Exit Sub
    I can't be any more precise than that with only one line of code to look at.

    HTH

    DominicB

  5. #5
    Registered User
    Join Date
    06-13-2008
    Posts
    21
    DominicB,

    Thanks for your help, but i still cant get this to work.

    Here is the full code for the macro.

    Sub CreateChart()
        Dim strSheetName As String
        Dim rngToChart As Range
        
        ' get the range to chart
        Set rngToChart = Application.InputBox(Prompt:="Select the range you wish to chart.", _
            Title:="Select a range.", Type:=8)
        rngToChart.Select
                
        ' Assign the address of the selected range of cells to a variable.
        myRange = Selection.Address
                
        ' Assign the name of the active sheet to a variable. This line is
        ' used in order to allow a chart to be created on a separate chart
        ' sheet.
        mySheetName = ActiveSheet.Name
            
        ' Create a name for the new sheet
        myGraphSheetName = mySheetName & " Graph"
        strSheetName = myGraphSheetName
            
        If IsWorkSheetPresent(strSheetName) Then
            ' delete the worksheet
            On Error Resume Next
            Application.DisplayAlerts = False
            Sheets(myGraphSheetName).Delete
            Application.DisplayAlerts = True
            On Error GoTo 0
        End If
        
        If IsChartSheetPresent(strSheetName) Then
            ' delete the chart sheet
            On Error Resume Next
            Application.DisplayAlerts = False
            Charts(myGraphSheetName).Delete
            Application.DisplayAlerts = True
            On Error GoTo 0
        End If
        
        ' Add a chart to a seperate sheet
        Charts.Add
        Application.CutCopyMode = False
            
        ' Create the chart
        ActiveChart.ChartWizard _
            Source:=Sheets(mySheetName).Range(myRange), _
            Gallery:=xlLine, Format:=1, PlotBy:=xlColumns, _
            CategoryLabels:=1, SeriesLabels:=1, HasLegend:=1, _
            Title:=mySheetName, CategoryTitle:="", _
            ValueTitle:="", ExtraTitle:=""
          
        ' Rename the sheet
        ActiveSheet.Name = myGraphSheetName
        
    End Sub
    The code stops at the Set rngToChart = Application.Inputbox.......... line

    I appreciate your help.

    jonnyuk3

  6. #6
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi jonnyuk3

    I'm not sure why you're having this problem - I've worked one of my previous answers in your code and it seems to work OK for me. I can't fully test it as you seem to be making use of a custom function (IsChartSheetPresent) but see if it works for you :
    Sub CreateChart()
        Dim strSheetName As String
        Dim rngToChart As Range
        
        ' get the range to chart
        On Error GoTo Last:
        Set rngToChart = Application.InputBox(Prompt:="Select the range you wish to chart.", _
            Title:="Select a range.", Type:=8)
        On Error GoTo 0
        rngToChart.Select
                
        ' Assign the address of the selected range of cells to a variable.
        myRange = Selection.Address
                
        ' Assign the name of the active sheet to a variable. This line is
        ' used in order to allow a chart to be created on a separate chart
        ' sheet.
        mySheetName = ActiveSheet.Name
            
        ' Create a name for the new sheet
        myGraphSheetName = mySheetName & " Graph"
        strSheetName = myGraphSheetName
            
        If IsWorkSheetPresent(strSheetName) Then
            ' delete the worksheet
            On Error Resume Next
            Application.DisplayAlerts = False
            Sheets(myGraphSheetName).Delete
            Application.DisplayAlerts = True
            On Error GoTo 0
        End If
        
        If IsChartSheetPresent(strSheetName) Then
            ' delete the chart sheet
            On Error Resume Next
            Application.DisplayAlerts = False
            Charts(myGraphSheetName).Delete
            Application.DisplayAlerts = True
            On Error GoTo 0
        End If
        
        ' Add a chart to a seperate sheet
        Charts.Add
        Application.CutCopyMode = False
            
        ' Create the chart
        ActiveChart.ChartWizard _
            Source:=Sheets(mySheetName).Range(myRange), _
            Gallery:=xlLine, Format:=1, PlotBy:=xlColumns, _
            CategoryLabels:=1, SeriesLabels:=1, HasLegend:=1, _
            Title:=mySheetName, CategoryTitle:="", _
            ValueTitle:="", ExtraTitle:=""
          
        ' Rename the sheet
        ActiveSheet.Name = myGraphSheetName
    
    Last:
    MsgBox "Aborted"
    Exit Sub
    End Sub
    If you're still getting an error you may have to post a cut down version of your file.

    HTH

    DominicB

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