+ Reply to Thread
Results 1 to 31 of 31

Open window with data from other sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Question Open window with data from other sheet

    Say im in sheet 1 at B2. Am looking to create a button so when I press it a small window opens and displays the data that is it sheet 2 (which is just 2 columns of a list of products and price.
    Then I for example double click on a product and it will be added in the current cell.

    How could I do that?

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    yo would need a userform with a listbox, or use data validation
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    I have tried but the problem is i have 2 columns, one description and once price. So when I click on a products its supposed to get both the description and price and put in the appropriate columns. Also the form I used one only put in the reference number of where it is located.
    I also dont know how to get a button so when I click it it opens the form.

    See the attachement for an example.
    Attached Files Attached Files

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    Your source list of products isn't really ideal, you have sub titles. I have added a dynamic named range called products to act as the data source for the listbox. When you enter a description cell the form opens, click an item in the listbox to add it to the list
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    sure you attached the right file?

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    I've just checked the example & I see you have merged cells in the invoice, this must be stopping the code working.

    Amend the code in the worksheet event to
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Column <> 3 Then Exit Sub
        If Target.Row < 12 Or Target.Row > 13 Then Exit Sub
        UserForm1.Show
    End Sub
    Copy the code above
    Select the worksheet in which you the code to run
    Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
    Where the cursor is flashing, choose Edit | Paste

  7. #7
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Ah I see yea, But doesnt copy the Price into the field. Also where did you create that userform so I know for future use.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    The price is inserted into the sheet, it's picking up the wrong price. I'll adjust this later
    You create UserForms in the VB Editor
    Last edited by royUK; 08-12-2009 at 08:27 AM.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    Here's the amended code to add the price
    
    Private Sub lbxProducts_Click()
        With Me.lbxProducts
            ActiveCell.Value = .Value
            ActiveCell.Offset(0, 1).Value = .List(.ListIndex, 1)
        End With
    End Sub

  10. #10
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Little confused where I put that piece of code?

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    Just replace the existing lbxProducts_Click code in the userform.

    Open the VB EDitor
    Double click on the UserForm & you should see the existing code

  12. #12
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Yea I tried that, replaced
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Column <> 3 Then Exit Sub
        If Target.Row < 12 Or Target.Row > 13 Then Exit Sub
        UserForm1.Show
    End Sub
    with this:
    Private Sub lbxProducts_Click()
        With Me.lbxProducts
            ActiveCell.Value = .Value
            ActiveCell.Offset(0, 1).Value = .List(.ListIndex, 1)
        End With
    End Sub
    Just nothing happens

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    It won't do, the ode that you have replaced displays the form.

    The code that you need to replace is in the userform, see the instructions in my last post

  14. #14
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Ah yep cheers,
    One last thing, if I was refering to another excel file that contained the same formatted data at in the Products sheet would i change the code like this:
    Private Sub UserForm_Initialize()
    Dim Col1Wid As Long
    Dim Col2Wid As Long
    
    Col1Wid = Range("'C:\Users\Taurus\Desktop\[2.xlsx]Sheet1'").Columns(1).Width
    Col2Wid = Range("'C:\Users\Taurus\Desktop\[2.xlsx]Sheet1'").Columns(2).Width
    
    With Me.lbxProducts
    .ColumnCount = 2
    .ColumnWidths = Col1Wid & ";" & Col2Wid
    .RowSource = Range("'C:\Users\Taurus\Desktop\[2.xlsx]Sheet1'").Address(external:=True)
    .ListIndex = 0
    End With
    End Sub
    Tried with no success, got the error:
    Method 'Range' of object '_Global' failed

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    It's no that easy, the code is totally different. It would help if you stated the exact scenario in the first post to avoid wasting time.
    Private Sub UserForm_Initialize()
        Dim oWbk   As Workbook
        Dim rdata  As Range
        Dim Col1Wid As Long
        Dim Col2Wid As Long
    
        Col1Wid = Range("Products").Columns(1).Width
        Col2Wid = Range("Products").Columns(2).Width
        On Error GoTo exit_handler
        Application.ScreenUpdating = False
        'check if workbook containing source is open if not open it.
        If Not IsOpen("2.xlsx") Then
            Set oWbk = Workbooks.Open("C:\Users\Taurus\Desktop\[2.xlsx")
        Else: Set oWbk = Workbooks("C:\Users\Taurus\Desktop\[2.xlsx")
        End If
        'this is the data to load to combobox
        With oWbk.Worksheets(1)
            Set rdata = .Range(.Cells(1, 1).Cells(.Rows.Count, 2).End(xlUp))
        End With
        With Me.lbxProducts
            .ColumnCount = 2
            .ColumnWidths = Col1Wid & ";" & Col2Wid
            .RowSource = rdata.Address(external:=True)
            .ListIndex = 0
        End With
    
    exit_handler:
    
        Set oWbk = Nothing
        Set rdata = Nothing
        Application.ScreenUpdating = True
        On Error GoTo 0
    End Sub
    Function IsOpen(wbName As String) As Boolean
        Dim Wb     As Workbook
        On Error Resume Next
        Set Wb = Workbooks(wbName)
        If Err = 0 Then IsOpen = True
    End Function

  16. #16
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Sorry, it was just I realized it would be easier to keep up to date the price list etc. Sorry for the inconvenience.

    I pasted the code into the form:
    Option Explicit
    
    
    Private Sub lbxProducts_Click()
        With Me.lbxProducts
            ActiveCell.Value = .Value
            ActiveCell.Offset(0, 1).Value = .List(.ListIndex, 1)
        End With
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim oWbk   As Workbook
        Dim rdata  As Range
        Dim Col1Wid As Long
        Dim Col2Wid As Long
    
        Col1Wid = Range("Products").Columns(1).Width
        Col2Wid = Range("Products").Columns(2).Width
        On Error GoTo exit_handler
        Application.ScreenUpdating = False
        'check if workbook containing source is open if not open it.
        If Not IsOpen("2.xlsx") Then
            Set oWbk = Workbooks.Open("C:\Users\Taurus\Desktop\[2.xlsx")
        Else: Set oWbk = Workbooks("C:\Users\Taurus\Desktop\[2.xlsx")
        End If
        'this is the data to load to combobox
        With oWbk.Worksheets(1)
            Set rdata = .Range(.Cells(1, 1).Cells(.Rows.Count, 2).End(xlUp))
        End With
        With Me.lbxProducts
            .ColumnCount = 2
            .ColumnWidths = Col1Wid & ";" & Col2Wid
            .RowSource = rdata.Address(external:=True)
            .ListIndex = 0
        End With
    
    exit_handler:
    
        Set oWbk = Nothing
        Set rdata = Nothing
        Application.ScreenUpdating = True
        On Error GoTo 0
    End Sub
    Function IsOpen(wbName As String) As Boolean
        Dim Wb     As Workbook
        On Error Resume Next
        Set Wb = Workbooks(wbName)
        If Err = 0 Then IsOpen = True
    End Function
    It opens the form but nothing in it?

    Also do I have to have the other file open? Because I did try this:
    ='C:\Users\Taurus\Desktop\[2.xlsx]Sheet1'!A3
    and it updated without being opened.

  17. #17
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    The code checks if the file is open, if not it opens it. Check the path file name are correct

  18. #18
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Okay yea fixed, but still the form doesnt contain any data. See the attachments.

    So am guessing it has to be open, why? when before i tried a simpler way and it didnt need to be open?
    Attached Files Attached Files

  19. #19
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    A couple of changes
    Private Sub UserForm_Initialize()
        Dim oWbk   As Workbook
        Dim rdata  As Range
        Dim Col1Wid As Long
        Dim Col2Wid As Long
    
        Col1Wid = Range("Products").Columns(1).Width
        Col2Wid = Range("Products").Columns(2).Width
        On Error GoTo exit_handler
        Application.ScreenUpdating = False
        'check if workbook containing source is open if not open it.
        If Not IsOpen("2.xlsx") Then
            Set oWbk = Workbooks.Open("C:\Users\Taurus\Desktop\2.xlsx")
        Else: Set oWbk = Workbooks("2.xlsx")
        End If
        'this is the data to load to combobox
    
        With oWbk.Sheets(1)
            Set rdata = .Range(.Cells(3, 1), .Cells(.Rows.Count, 2).End(xlUp))
        End With
        With Me.lbxProducts
            .ColumnCount = 2
            .ColumnWidths = Col1Wid & ";" & Col2Wid
            .RowSource = rdata.Address(external:=True)
            .ListIndex = 0
        End With
    
    exit_handler:
        oWbk.Close False
    
        Set oWbk = Nothing
        Set rdata = Nothing
        Application.ScreenUpdating = True
        On Error GoTo 0
    End Sub
    Function IsOpen(wbName As String) As Boolean
        Dim Wb     As Workbook
        On Error Resume Next
        Set Wb = Workbooks(wbName)
        If Err = 0 Then IsOpen = True
    End Function

  20. #20
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Damn Memory problems it says

  21. #21
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    Just checked & I get a message about memory. Not sure why but this change stops it
    Private Sub UserForm_Initialize()
        Dim oWbk   As Workbook
        Dim rdata  As Range
        Dim Col1Wid As Long
        Dim Col2Wid As Long
    
        Col1Wid = Range("Products").Columns(1).Width
        Col2Wid = Range("Products").Columns(2).Width
        On Error GoTo exit_handler
        Application.ScreenUpdating = False
        'check if workbook containing source is open if not open it.
        If Not IsOpen("2.xlsx") Then
            Set oWbk = Workbooks.Open("C:\Users\Taurus\Desktop\2.xlsx")
        Else: Set oWbk = Workbooks("2.xlsx")
        End If
        'this is the data to load to combobox
    
        With oWbk.Sheets(1)
            Set rdata = .Range(.Cells(3, 1), .Cells(.Rows.Count, 2).End(xlUp))
        End With
        With Me.lbxProducts
            .ColumnCount = 2
            .ColumnWidths = Col1Wid & ";" & Col2Wid
            .List = rdata.Value
            .ListIndex = 0
        End With
    
    exit_handler:
        oWbk.Close False
    
        Set oWbk = Nothing
        Set rdata = Nothing
        Application.ScreenUpdating = True
        On Error GoTo 0
    End Sub

  22. #22
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Cheers works like a charm

  23. #23
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Question Re: Open window with data from other sheet

    Hey man, You know that userform where the products are listed, its 1 line, is it possible to make for each products 2 lines? so its easier to see?

    I also have another idea. See the problem right now is when I click new row, the userform doesnt open for the new rows. Only opens for the first 2. Now can have a button that that I click next to the rows and that way opens then userform instead of clicking on the cell?
    See attachement for what I mean.

    cheers
    Attached Files Attached Files
    Last edited by philmetz; 08-14-2009 at 11:21 PM.

  24. #24
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    You can't split the lines in a listbox.

    If you want to learn VBA & produce projects then study the code that you are given.The ros that the form works in is set in the code.

  25. #25
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Well I dont exactly want to produce projects. This is a one off thing am doing for ma cousins company.

    So am not an expert at all in VBA so need some help with this. I saw the code where the rows work in but I dont know how to increment it with the increase of a line. I thought it would do it automatically but hasnt?

  26. #26
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    I also have a problem as when it gets the price and inserts it into the unit price field, it deletes the formula which was in that field. It was this:
    =C17*IF($F$15="Unit Price ZMK",$G$11,1))

    Where C17 is the cell that has the unit price. The formulas basically converts between currencies.
    How could i leave a formula in the cell?

  27. #27
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    You can't add a unit price without overwriting the formula.

    I think you will find that problems will continue to crop up. The best thing is to pln your project step by step before starting, strangely in this computer age a pen & paper helps. If you do this you will then be able to check if any steps will cause problems later.

    The simplest way to add rows is to et up upur template with sufficient rows & hide the extra ones. Then in the code you can unhide rows as required.
    Last edited by royUK; 08-17-2009 at 11:15 AM.

  28. #28
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    ah ok guess Ill create a macro for that. Also how can I increase the macro for the userform whenever I add a new row? If you check the file above I click on a button and it adds a new row, so wouldnt it be possible for the macro that opens the userform to be applied to that new one?
    Last edited by philmetz; 08-17-2009 at 11:28 AM.

  29. #29
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    Quote Originally Posted by philmetz View Post
    ah ok guess Ill create a macro for that. Also how can I increase the macro for the userform whenever I add a new row?
    add a new row where? If you mean the sheet then change the code to this, it will work on C12 to C22
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       If Intersect(ActiveCell, Range("C12:C22")) Is Nothing Then UserForm1.Show
        UserForm1.Show
    End Sub

  30. #30
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: Open window with data from other sheet

    Yea i mean in the sheet, like when I add a new row (ie new product to the quotation), then when i click the description field it should open the userform.

  31. #31
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open window with data from other sheet

    Try the code above
    Attached Files Attached Files

+ 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