+ Reply to Thread
Results 1 to 11 of 11

Input box to needs different type of variable type (RC vs. A1?)

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Input box to needs different type of variable type (RC vs. A1?)

    I'm trying to use an input box to prompt the user to select the row containing the data they want plotted. I am having a problem with the way VBA likes to have its rows and column references formatted (as a letter?) vs. what is returned from selecting an active cell (as a number?).

    Here's the example data that applies:

    Book1.xlsx

    The portion of the code I'm having trouble with comes just after the first If statement.



    Sub MakeFigures()
    
        Dim intFirstRow As Integer      ' First row of the current chart series
        Dim intLastRow As Integer       ' Last row of the current chart series
        Dim intStartRow As Integer      ' First row of data in the spreadsheet
        Dim intRow As Integer           ' Current row being processed
        Dim strBH As String             ' Current monitoring well
        Dim strDataColumn As Integer
        Dim xlChart As Chart
        Dim xlsheet As Worksheet
        Dim Parameter1 As Range         ' Column containing 1st parameter chosen by user to plot
    
        ' First select a chart
        If ActiveChart Is Nothing Then
            MsgBox "Please select the empty chart tab before running the macro.", , "No Chart Selected"
            Exit Sub
        End If
        
        '   Hard-coded chart, sheet, row and column selections here could be replaced by prompts to the user
        Set xlsheet = ActiveWorkbook.Sheets("Data")
        Set xlChart = ActiveChart
        intStartRow = 3    ' First row with data in it
        Parameter1 = Application.InputBox(Prompt:="Select the cell containing the name of the parameter you want to plot.", Default:="Navigate to your data and select the header cell for the data of interest.", Type:=8)
        strDataColumn = Parameter1      ' User's first parameter selection to be plotted
        
        '   Start in the first data row
        intRow = intStartRow
        strBH = xlsheet.Cells(intRow, 1).Value
        intFirstRow = intRow
        
        Do
            '   Step down the rows
            '   Is this a new borehole number? If so, create a chart series for the previous rows.
            If xlsheet.Cells(intRow, 1).Value <> strBH Then
                intLastRow = intRow - 1             ' The series ends at the row we just passed
                With xlChart
                    Dim s As Series
                    .SeriesCollection.NewSeries
                    Dim intSeries As Integer
                    intSeries = .SeriesCollection.Count
                    Set s = .SeriesCollection(intSeries)
                    s.Name = RangeAddress(xlsheet, "A", intFirstRow)        ' Set the series name to the monitoring well number
                    s.XValues = RangeAddress(xlsheet, "B", intFirstRow, intLastRow) ' Set x values to the date column
                    s.Values = RangeAddress(xlsheet, strDataColumn, intFirstRow, intLastRow)  ' Set y values to the data column
                End With
                
                '   Reset to start a new series for the next hole
                intFirstRow = intRow
                strBH = xlsheet.Cells(intRow, 1).Value
            End If
            
            '   Now move down one row
            intRow = intRow + 1
    
            ' Stop at the first row with no date
        Loop Until strBH = ""
        
        MsgBox "Chart completed!", , "Done"
    
    End Sub
    
    Private Function RangeAddress(xlsheet As Worksheet, strColumn As String, intFirstRow As Integer, Optional intLastRow As Integer) As String
        '   Returns a cell address or range address suitable for input to the x or y values
        '   property of a chart series.
        
        RangeAddress = "=" & xlsheet.Name & "!$" & strColumn & "$" & intFirstRow
        If intLastRow > 0 Then RangeAddress = RangeAddress & ":$" & strColumn & "$" & intLastRow
    
    End Function
    Last edited by Niedermee; 08-18-2014 at 01:11 PM. Reason: Fixed asignments in "intStartRow" and "ActiveWorkbook.sheets"

  2. #2
    Registered User
    Join Date
    01-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Input box to needs different type of variable type (RC vs. A1?)

    To make this simpler, how do I change the format of the row/column variables in VBA?

    The input box returns a selected cell format as Row 6 when I selected row "H". This is cell is used later in the code, but needs is to be formatted as a letter and not a number so it crashes.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Input box to needs different type of variable type (RC vs. A1?)

    Have you looked at using Cells?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    01-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Input box to needs different type of variable type (RC vs. A1?)

    I don't understand "using Cells".

    I'm a novice at VBA and am trying to change code that originally hard coded the row assignment the same as it currently does for the 1st row.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Input box to needs different type of variable type (RC vs. A1?)

    If you want the user to select a cell/range use this.
     Set Parameter1 = Application.InputBox(Prompt:="Select the cell containing the name of the parameter you want to plot.", Default:="Navigate to your data and select the header cell for the data of interest.", Type:=8)

  6. #6
    Registered User
    Join Date
    01-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Input box to needs different type of variable type (RC vs. A1?)

    Now it gives me a Compile error: Object Required and highlights Parameter1 before the "="

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Input box to needs different type of variable type (RC vs. A1?)

    Strange, that's the error I get if I don't use Set.

    Did you change anything else?

    When you got the error had you selected a range?

    By the way, the reason you should use Set is because Application.InputBox with the Type set to 8 returns a Range object.

  8. #8
    Registered User
    Join Date
    01-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Input box to needs different type of variable type (RC vs. A1?)

    In addition to adding "Set" to the Parameter1 assignment, I needed to change the value to an address (Parameter1.address) and then used the mid function to select just the row letter (this assumes I never have any more than Z rows), but it works. See attached.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    01-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Input box to needs different type of variable type (RC vs. A1?)

    How do I mark post as Solved?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Input box to needs different type of variable type (RC vs. A1?)

    Glad it's solved.

    Mind you there's no reason to use Address/Mid to get the row/column number.

    In fact if you do that you may run into problems if the column goes past Z.

    Instead of using Address/Mid you can use the Row and Column properties of Parameter1.

  11. #11
    Registered User
    Join Date
    01-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Input box to needs different type of variable type (RC vs. A1?)

    That was what my initial and ultimate goal, but couldn't figure it out. Forgive me if the answer is embedded in someone's reply, but I couldn't get it to work for me.

    How do I "use the Row and Column properties of Parameter1"?

+ 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] multiple conditions to count large amount of data by Type, month & activity type
    By norfolk_lass in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-03-2014, 12:54 PM
  2. Replies: 0
    Last Post: 07-06-2013, 12:59 PM
  3. Proper handling of "cancel" for range-type input box response (type 8)
    By MCCCLXXXV in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-28-2011, 11:19 AM
  4. Type mismatch using rnge as Range with Type 8 Input Box
    By STEVE BELL in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-03-2005, 01:10 AM
  5. Help: Compile error: type mismatch: array or user defined type expected
    By lvcha.gouqizi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2005, 05:05 PM

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