+ Reply to Thread
Results 1 to 19 of 19

Change code to an input box to enter the file name

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Change code to an input box to enter the file name

    Hi all,

    I have some code which i have tested and is working great, but i would like to change it slightly so that i can input a file name so it picks the correct .xls workbook.

    in my code at the minute it is looking for "sample_BOM" i would like to be able to enter the file name manual every time, i have commented the code so you can see where it is.

    Private Sub OptionButton1_Change()
    If Me.OptionButton1.Value = True Then
    Me.ComboBox1.List = Array("POWER", "CONTROL")
    ElseIf Me.OptionButton2.Value = True Then
    Me.ComboBox1.List = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "CS")
    End If
    End Sub
    Private Sub UserForm_Initialize2()
        Dim ListArray As Variant
            With Sheets("Sheet1")
            ListArray = .Range(.Range("a1"), .Range("a" & Rows.Count).End(xlUp))
            End With
            ListBox1.List = ListArray
    End Sub
    Private Sub UserForm_Initialize()
        Dim lb As MSForms.ListBox
        Dim rcArray() As Variant
        Dim lrw As Long, lcol As Long
        Dim rSource As Range
        Set rSource = Workbooks("Sample_BOM.xls").Worksheets("Sheet1").Range("A7:D100") 'THIS IS THE LINE OF CODE I WOULD LIKE TO CHANGE.
        ReDim Preserve rcArray(1 To rSource.Rows.Count, 1 To rSource.Columns.Count)
        With rSource
            For lcol = 1 To .Columns.Count
                For lrw = 1 To .Rows.Count
                    rcArray(lrw, lcol) = rSource.Cells(lrw, lcol)
                Next lrw
            Next lcol
        End With
        For Each cell In rSource
            If cell.Value <> vbNullString Then
                Set lb = Me.ListBox1
                    With lb
                        .ColumnCount = 4
                        .ColumnWidths = "50,150,100;50"
                        .List = rcArray
                    End With
            End If
        Next cell
    End Sub
    Private Sub CommandButton2_Click()
    Dim wks As Worksheet
    Dim nextAvailableRow As Long
    Dim i As Long
    
    Set wks = Sheets(ComboBox1.Value)
    Sheets(ComboBox1.Value).Activate
        For i = 0 To ListBox1.ListCount - 1
            nextAvailableRow = wks.Range("C" & Rows.Count).End(xlUp).Row + 1
            wks.Range("A" & nextAvailableRow) = ListBox1.Column(0, i)
            wks.Range("B" & nextAvailableRow) = ListBox1.Column(1, i)
            wks.Range("C" & nextAvailableRow) = ListBox1.Column(2, i)
            wks.Range("D" & nextAvailableRow) = ListBox1.Column(3, i)
        Next i
        
    End Sub
    I would also like to only open the userform when a button is pressed in the excel worksheet and not automatically when the workbook is opened. I have this macro attached to a button which opens the user form once the BOM is open.

    Sub Auto_Open()
        Dim ws As Worksheet
        For Each ws In Worksheets
            UserForm1.ComboBox1.AddItem ws.Name
        Next ws
        UserForm1.Show
    End Sub

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,335

    Re: Change code to an input box to enter the file name

    To have the userform not open when worksheet is activated, remove this line
    UserForm1.Show
    from
    Sub Auto_Open()
        Dim ws As Worksheet
        For Each ws In Worksheets
            UserForm1.ComboBox1.AddItem ws.Name
        Next ws
        UserForm1.Show
    End Sub
    Then put this in your button on the worksheet
     UserForm1.Show
    i.e
    Private Sub CommandButton1_Click()
    UserForm1.Show
    End Sub
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Change code to an input box to enter the file name

    Something like this.

    Dim wBook As String
    wBook = Application.InputBox("Enter filename and extension.", "FileName", , , , , , 2)
    Set rSource = Workbooks(wBook).Worksheets("Sheet1").Range("A7:D100")
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    Thank you both of you!!

    this has worked perfectly!

    Would it be possible to add an error message to the code so if the file is not open it tells you rather than creating an error to debug?

    "Bill of Materials Not Open Please Open First"

  5. #5
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I have two separate codes and i'm not sure which bit i would put the code in,


    Private Sub OptionButton1_Change()
    If Me.OptionButton1.Value = True Then
    Me.ComboBox1.List = Array("POWER", "CONTROL")
    ElseIf Me.OptionButton2.Value = True Then
    Me.ComboBox1.List = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "CS")
    End If
    End Sub
    Private Sub UserForm_Initialize2()
        Dim ListArray As Variant
            With Sheets("Sheet1")
            ListArray = .Range(.Range("a1"), .Range("a" & Rows.Count).End(xlUp))
            End With
            ListBox1.List = ListArray
    End Sub
    Private Sub UserForm_Initialize()
        Dim lb As MSForms.ListBox
        Dim rcArray() As Variant
        Dim lrw As Long, lcol As Long
        Dim rSource As Range
        Dim wBook As String
        wBook = Application.InputBox("Enter filename and extension.", "FileName", , , , , , 2)
        Set rSource = Workbooks(wBook).Worksheets("Sheet1").Range("A7:D100")
        ReDim Preserve rcArray(1 To rSource.Rows.Count, 1 To rSource.Columns.Count)
        With rSource
            For lcol = 1 To .Columns.Count
                For lrw = 1 To .Rows.Count
                    rcArray(lrw, lcol) = rSource.Cells(lrw, lcol)
                Next lrw
            Next lcol
        End With
        For Each cell In rSource
            If cell.Value <> vbNullString Then
                Set lb = Me.ListBox1
                    With lb
                        .ColumnCount = 2
                        .ColumnWidths = "200,50"
                        .List = rcArray
                    End With
            End If
        Next cell
    End Sub
    Private Sub CommandButton2_Click()
    Dim wks As Worksheet
    Dim nextAvailableRow As Long
    Dim i As Long
    
    Set wks = Sheets(ComboBox1.Value)
    Sheets(ComboBox1.Value).Activate
        For i = 0 To ListBox1.ListCount - 1
            nextAvailableRow = wks.Range("A" & Rows.Count).End(xlUp).Row + 1
            wks.Range("A" & nextAvailableRow) = ListBox1.Column(0, i)
            wks.Range("B" & nextAvailableRow) = ListBox1.Column(1, i)
    
        Next i
        
    End Sub

    then the macro to open the user form.

    Private Sub Button1_Click()
    
    UserForm1.Show
    
    End Sub

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Change code to an input box to enter the file name

    This opens it for you if still closed.
    Replace "Filename" with wBook.

    Function BookOpen(wbName As String) As Boolean
    Dim wb As Workbook
    On Error Resume Next
    Set wb = Workbooks(wbName)
    BookOpen = Not (Err.Number > 0)
    End Function
    
    Sub tst()
    If Not BookOpen("Filename") Then Workbooks.Open ThisWorkbook.Path & "\" & "Filename"
    End Sub

  7. #7
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I still get an error on the macro, i'm not sure if i have put it in the correct place.

    Private Sub OptionButton1_Change()
    If Me.OptionButton1.Value = True Then
    Me.ComboBox1.List = Array("POWER", "CONTROL")
    ElseIf Me.OptionButton2.Value = True Then
    Me.ComboBox1.List = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "CS")
    End If
    End Sub
    Private Sub UserForm_Initialize2()
        Dim ListArray As Variant
            With Sheets("Sheet1")
            ListArray = .Range(.Range("a1"), .Range("a" & Rows.Count).End(xlUp))
            End With
            ListBox1.List = ListArray
    End Sub
    Function BookOpen(wbName As String) As Boolean
    Dim wb As Workbook
    On Error Resume Next
    Set wb = Workbooks(wbName)
    BookOpen = Not (Err.Number > 0)
    End Function
    Sub tst()
    If Not BookOpen("wBook") Then Workbooks.Open ThisWorkbook.Path & "\" & "Filename"
    End Sub
    Private Sub UserForm_Initialize()
        Dim lb As MSForms.ListBox
        Dim rcArray() As Variant
        Dim lrw As Long, lcol As Long
        Dim rSource As Range
        Dim wBook As String
        wBook = Application.InputBox("Enter filename and extension.", "FileName", , , , , , 2)
        Set rSource = Workbooks(wBook).Worksheets("Sheet1").Range("A7:D100")
        ReDim Preserve rcArray(1 To rSource.Rows.Count, 1 To rSource.Columns.Count)
        With rSource
            For lcol = 1 To .Columns.Count
                For lrw = 1 To .Rows.Count
                    rcArray(lrw, lcol) = rSource.Cells(lrw, lcol)
                Next lrw
            Next lcol
        End With
        For Each cell In rSource
            If cell.Value <> vbNullString Then
                Set lb = Me.ListBox1
                    With lb
                        .ColumnCount = 2
                        .ColumnWidths = "200,50"
                        .List = rcArray
                    End With
            End If
        Next cell
    End Sub
    Private Sub CommandButton2_Click()
    Dim wks As Worksheet
    Dim nextAvailableRow As Long
    Dim i As Long
    
    Set wks = Sheets(ComboBox1.Value)
    Sheets(ComboBox1.Value).Activate
        For i = 0 To ListBox1.ListCount - 1
            nextAvailableRow = wks.Range("A" & Rows.Count).End(xlUp).Row + 1
            wks.Range("A" & nextAvailableRow) = ListBox1.Column(0, i)
            wks.Range("B" & nextAvailableRow) = ListBox1.Column(1, i)
    
        Next i
        
    End Sub

  8. #8
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    bakerman2,

    Where should i put this code? Will it still work if my root folder is different every time?

  9. #9
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    This is what i tried in the macro, but again it came up with an error.

    Function BookOpen(wbName As String) As Boolean
    Dim wb As Workbook
    On Error Resume Next
    Set wb = Workbooks(wbName)
    BookOpen = Not (Err.Number > 0)
    End Function
    
    Sub tst()
    Const fRootPath As String = "\\NEWBENSON\Company\IMC Issue Sheets"
    If Not BookOpen("wBook") Then Workbooks.Open ThisWorkbook.fRootPath & "\" & "wBook"
    End Sub
    
    Private Sub Button1_Click()
    UserForm1.ShowEnd Sub

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Change code to an input box to enter the file name

    Put the function in a standard module.
    In your userform module this.
    Also if your rootfolder is always changing you would have to substitute the InputBox to a FilePicker dialogue box.

    Const fRootPath As String = "\\NEWBENSON\Company\IMC Issue Sheets"
    
    Private Sub UserForm_Initialize()
        Dim lb As MSForms.ListBox
        Dim rcArray() As Variant
        Dim lrw As Long, lcol As Long
        Dim rSource As Range
        Dim wBook As String
        wBook = Application.InputBox("Enter filename and extension.", "FileName", , , , , , 2)
        If Not BookOpen("wBook") Then Workbooks.Open fRootPath & "\" & "wBook"
        Set rSource = Workbooks(wBook).Worksheets("Sheet1").Range("A7:D100")
        ReDim Preserve rcArray(1 To rSource.Rows.Count, 1 To rSource.Columns.Count)
        With rSource
            For lcol = 1 To .Columns.Count
                For lrw = 1 To .Rows.Count
                    rcArray(lrw, lcol) = rSource.Cells(lrw, lcol)
                Next lrw
            Next lcol
        End With
        For Each cell In rSource
            If cell.Value <> vbNullString Then
                Set lb = Me.ListBox1
                    With lb
                        .ColumnCount = 2
                        .ColumnWidths = "200,50"
                        .List = rcArray
                    End With
            End If
        Next cell
    End Sub

  11. #11
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I have decide that the file location will be in the same place every time so no need to worry about that any more

  12. #12
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    Hi Bakerman2,

    I have put the function in a new module, and then inserted the code above, but i get an error saying \\NEWBENSON\Company\IMC Issue Sheets\wBook can not be found, it doesnt seem to be searching for the .xls file.

    any suggestions?

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Change code to an input box to enter the file name

    I just copied your codelines so it didn't occur to me immediatly but wBook needs to be without the double quotes.

    If Not BookOpen(wBook) Then Workbooks.Open fRootPath & "\" & wBook
    Last edited by bakerman2; 03-31-2017 at 04:06 AM.

  14. #14
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I have managed to work out how to get the .xls open and that now works perfectly!

    I have one other little problem now which i was unaware off til just now, Would it be possible to add another input box to get the worksheet name?

    Private Sub UserForm_Initialize()
        Dim lb As MSForms.ListBox
        Dim rcArray() As Variant
        Dim lrw As Long, lcol As Long
        Dim rSource As Range
        Dim wBook As String
        Dim wSheet As String
        
        Const fRootPath As String = "\\NEWBENSON\Company\IMC Issue Sheets"
        wBook = Application.InputBox("Enter filename and extension.", "wBook", , , , , , 2)
        wSheet = Application.InputBox("Enter Sheet Name.", "wSheet", , , , , , 2)
       If Not BookOpen(wBook) Then Workbooks.Open fRootPath & "\" & wBook & ".xls"
       
        Set rSource = Workbooks(wBook).Worksheets("wSheet").Range("A7:D100")
        ReDim Preserve rcArray(1 To rSource.Rows.Count, 1 To rSource.Columns.Count)
        With rSource
            For lcol = 1 To .Columns.Count
                For lrw = 1 To .Rows.Count
                    rcArray(lrw, lcol) = rSource.Cells(lrw, lcol)
                Next lrw
            Next lcol
        End With
        For Each cell In rSource
            If cell.Value <> vbNullString Then
                Set lb = Me.ListBox1
                    With lb
                        .ColumnCount = 2
                        .ColumnWidths = "200,50"
                        .List = rcArray
                    End With
            End IF
        Next cell
    I have just tried to create the code myself but doesnt work thing i have something wrong!

  15. #15
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I'm not sure what that means, very new to vba code sorry!

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Change code to an input box to enter the file name

    Just replace the same line in the code with the line in my previous post.

  17. #17
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Change code to an input box to enter the file name

    Same as wBook, use wSheet without the double quotes.
    Last edited by bakerman2; 03-31-2017 at 05:36 AM.

  18. #18
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I thought i was close! just tried it and it worked perfectly!

    Thanks for your help through all this!

    REP ADDED

    Cheers
    ND

  19. #19
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Change code to an input box to enter the file name

    You're very welcome and thanks for rep+.
    If this answers your question please mark the thread as Solved.

+ 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] VB Code to enter cell value when file is open
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2016, 01:46 PM
  2. Change VBA code using input box
    By LLL0422 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-28-2014, 03:35 PM
  3. Formula to enter data/change the colour of a cell if a file isn't updated
    By alexander.small in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-31-2013, 09:58 AM
  4. Replies: 0
    Last Post: 10-02-2013, 01:47 AM
  5. [SOLVED] VBA Code to Enter File Name, Activate the File, and Run Macro on File
    By DHartwig35805 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-09-2012, 03:49 PM
  6. Doing away with an Input box - how to change this code?
    By HankMcSpank in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2009, 09:22 AM
  7. How to enter automatic entry of file name in to VBA Code?
    By mvel_sky in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-28-2009, 03:43 AM

Tags for this Thread

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