+ Reply to Thread
Results 1 to 5 of 5

VBA Display Input Box to Add Value to Cell

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Display Input Box to Add Value to Cell

    Hi, I wonder whether someone may be able to help me please.

    I'm using the code below to create multiple sheets in a given workbook.

        Dim shArray() As Variant             'Declare the sheet Name array and a
        Dim i As Long                        'counter variable
        
            shArray = Array("Monthly Direct", _
                "Monthly Enhancements", _
                "Monthly Indirect", _
                "Monthly Overheads", _
                "Monthly Projects", _
                "Yearly Direct", _
                "Yearly Enhancements", _
                "Yearly Indirect", _
                "Yearly Overheads", _
                "Yearly Projects", _
                "C&R In Flight Projects", _
                "S&A In Flight Projects", _
                "C&R Graph Data", _
                "S&A Graph Data", _
                "C&R Flexible Resource Profile", _
                "S&S Flexible Resource Profile", _
                "C&R Flexible Resource KPI", _
                "C&R Resource Capacity")    'Populate the array
                
                For i = LBound(shArray) To UBound(shArray)  'Loop through the elements
                    Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = shArray(i)
                    shArray(i).Range("B3").Value = InputBox("Enter the month of the data you wish to use?")
                Next
    
                Call MonthlyExtract
                Call DirIndOVHExtract
                Call YearlyExtract
                Call MonthlyFormat
                Call YearlyFormat
    
    End Sub
    The code works except for this line:
    shArray(i).Range("B3").Value = InputBox("Enter the month of the data you wish to use?")
    What I'm trying to achieve, is to dispaly an 'Input Box' to allow the user to enter a date which will populate cell B3 in each of the created sheets. However, when I run this, although I can display the 'Input Box', when I enter the data, and click 'Ok', I receive a 'Run time' error highlighting this line as the cause.

    I just wondered whether someone may be able to look at this please and let me know where I'm going wrong.

    Many thanks and kind regards

    Chris

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

    Re: VBA Display Input Box to Add Value to Cell

    shArray is a variant array of text. Not actual sheets.

    Sheets(shArray(i)).Range("B3").Value = InputBox("Enter the month of the data you wish to use?")
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Display Input Box to Add Value to Cell

    Hi Andy, thank you very much for coming back to me with this.

    I tried the code you kindly provided, and although this displays the 'Input Box' and allows the user to enter the date, the 'Input box' is displayed for each sheet in the array, rather than once, which I was hoping for.

    Many thanks and kind regards

    Chris

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

    Re: VBA Display Input Box to Add Value to Cell

    Start by taking the InputBox command out of the loop.
    Get the value first then apply to each sheet in loop

        Dim shArray() As Variant             'Declare the sheet Name array and a
        Dim i As Long                        'counter variable
        Dim vntMonth As Variant
        
        vntMonth = InputBox("Enter the month of the data you wish to use?")
        If vntMonth = False Then Exit Sub
        
        shArray = Array("Monthly Direct", _
                "Monthly Enhancements", _
                "Monthly Indirect", _
                "Monthly Overheads", _
                "Monthly Projects", _
                "Yearly Direct", _
                "Yearly Enhancements", _
                "Yearly Indirect", _
                "Yearly Overheads", _
                "Yearly Projects", _
                "C&R In Flight Projects", _
                "S&A In Flight Projects", _
                "C&R Graph Data", _
                "S&A Graph Data", _
                "C&R Flexible Resource Profile", _
                "S&S Flexible Resource Profile", _
                "C&R Flexible Resource KPI", _
                "C&R Resource Capacity")    'Populate the array
                
        For i = LBound(shArray) To UBound(shArray)  'Loop through the elements
            Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = shArray(i)
            Sheets(shArray(i)).Range("B3") = vntMonth
        Next

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Display Input Box to Add Value to Cell

    Hi Andy, thank you for coming back to me with this with the code and the explanation on the change.

    The code works perfectly!

    All the best and kind regards

    Chris

+ 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] VBA to display Input box and enter cell value
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-13-2014, 05:54 PM
  2. Display all data associated with input in given cell
    By davo1224 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-12-2010, 11:48 AM
  3. Can name range display value based on the input of specific cell?
    By rusoo7 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-24-2010, 11:17 AM
  4. Replies: 1
    Last Post: 08-29-2009, 01:10 PM
  5. Cell Category (Number) input Display
    By Bracknell in forum Excel General
    Replies: 1
    Last Post: 07-16-2007, 10:06 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