+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: Macro/VBA needed for opening excel file

  1. #1
    Registered User
    Join Date
    10-24-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007
    Posts
    24

    Macro/VBA needed for opening excel file

    Hi,

    I need to create a macro and I have no idea what I'm doing using VBA and recording doesn't really work. Can anyone help??

    To help, I've attached some files. The first file (Macro template) is the file I want data pasted into. The second and third files contain the data I need.

    I think I will need several macros but I want to be able to look at a cell in the macro template file (in this case cells B8, B47 and B87) which has my "Pack ID" and then open the second file which has the data. The data needs to be sorted in alphabetical order (currently A01, C01, E01 etc etc, needs to be A01, A02, A03 etc). and then paste just the data (column B) into the 'tube sent' column in the original file. It then does the same for the second data file and pastes into the 'tube received' column.

    I have no experience of VBA, so I really don't know how to open the area I need to edit the VBA or enter any VBA or anything. Recording a macro at the moment does everything I want but is limited to only opening the file I use while recording the macro, where I want it to open the file specific to B8, B47, B87 etc.

    cheers
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    re: Macro/VBA needed for opening excel file

    Why not just rename your tube positions to those in your file i.e put a 0 in and use VLookup?
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  3. #3
    Registered User
    Join Date
    10-24-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007
    Posts
    24

    re: Macro/VBA needed for opening excel file

    I'm wanting to search my hard drive in a specific folder for the file names in B8, B47 and B87. VLookup wouldn't do this I don't believe

  4. #4
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    re: Macro/VBA needed for opening excel file

    Is this resolved?

  5. #5
    Registered User
    Join Date
    10-24-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007
    Posts
    24

    re: Macro/VBA needed for opening excel file

    No I have not been able to resolve this.

  6. #6
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    re: Macro/VBA needed for opening excel file

    Please provide a few examples of the files names that will contain the data for Tube Received and Tube Sent and where will both these file names be saved in the macro template?
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  7. #7
    Registered User
    Join Date
    10-24-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro/VBA needed for opening excel file

    Hi,

    An example filename would be XR00000008.xlsx. The file will be saved in a specific file location and the only data in that file would be the list of XR.... files. eg. C:\Documents and Settings\Tubes Sent or C:\Documents and Settings\Tubes Received

    Is that what you were wanting to know?

    Thanks,
    Toby

  8. #8
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Macro/VBA needed for opening excel file

    In the macro template, would you be putting the entire path in any cell or just the filename? If the filename, in which cell would it be?
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  9. #9
    Registered User
    Join Date
    10-24-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro/VBA needed for opening excel file

    Hi,

    It would only be the filename that would be put into the cell, I want the Macro to find that file on the harddrive based on whats in the cell.
    Its actually multiple cells to be honest, I want to put the filename in the cell next to 'Rack ID' (so B9, B48, B88 etc etc) and have a button next to each of these seperate cells, which you press and accesses the macro to find the filename and populate the table below. If it's not possible to do this for multiple cells, it would just be B9 and then I'd have to use a new spreadhseet for each file I want to locate.

    Cheers,
    Toby

  10. #10
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Macro/VBA needed for opening excel file

    We lost the files that you attached due to some small issue in the upgrade. Can you please attach the files again?
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  11. #11
    Registered User
    Join Date
    10-24-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro/VBA needed for opening excel file

    Hi,

    That's ok, I've re-attached the files.

    Toby
    Attached Files Attached Files

  12. #12
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Macro/VBA needed for opening excel file

    I just needed one last clarification before i work on your issue (I know its been a while, but surely i should have a good solution this time).
    You would be inputting the file name for tube Sent in B8 and for tube received in B9 right?
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  13. #13
    Registered User
    Join Date
    10-24-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro/VBA needed for opening excel file

    Hi,

    Sorry for not responding sooner.

    No this is not the case. The filename will only be in B8. The two files (tubes sent and tubes received spreadsheets) will be saved in two seperate folders on the hard drive, but will have the same name. This means for the tubes sent data, it will look specifically in 1 folder for that file name and will look specificially in the other folder for the file name for the tubes received data.

    B9 is used for another piece of data which irrelevant.

    Thanks,
    Toby

  14. #14
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Macro/VBA needed for opening excel file

    I apologize for the delay. I have got the code ready for you. Try this.
    Option Explicit
    Dim lrow As Long
    Dim SentPath As String
    Dim RecPath As String
    Dim lastrow As Long
    Dim i As Long
    Dim FName As String
    
    Sub populate_file()
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    lrow = ThisWorkbook.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
    SentPath = "D:\Documents and Settings\DWS\DWS\Tubes Sent\"
    RecPath = "D:\Documents and Settings\DWS\DWS\Tubes Received\"
    'SentPath = "C:\Documents and Settings\Tubes Sent\"
    'RecPath = "C:\Documents and Settings\Tubes Received\"
    
    For i = 8 To lrow Step 40
        
        'TUBES SENT
        Workbooks.Open SentPath & ThisWorkbook.Worksheets(1).Range("B" & i).Value
        FName = ActiveWorkbook.Name
        
        Workbooks(FName).Worksheets(1).Sort.SortFields.Clear
        Workbooks(FName).Worksheets(1).Sort.SortFields.Add Key:=Range("A:A") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With Workbooks(FName).Worksheets(1).Sort
            .SetRange Range("A:B")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
        lastrow = Workbooks(FName).Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
        Workbooks(FName).Worksheets(1).Range("B1:B" & lastrow).Copy
        ThisWorkbook.Worksheets(1).Range("C" & i + 5).PasteSpecial Paste:=xlValues, operation:=xlNone, skipblanks:= _
            False, Transpose:=False
        Workbooks(FName).Close
        
        'TUBES RECEIVED
        Workbooks.Open RecPath & ThisWorkbook.Worksheets(1).Range("B" & i).Value
        FName = ActiveWorkbook.Name
        Workbooks(FName).Worksheets(1).Sort.SortFields.Clear
        Workbooks(FName).Worksheets(1).Sort.SortFields.Add Key:=Range("A:A") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With Workbooks(FName).Worksheets(1).Sort
            .SetRange Range("A:B")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
        lastrow = Workbooks(FName).Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
        Workbooks(FName).Worksheets(1).Range("B1:B" & lastrow).Copy
        ThisWorkbook.Worksheets(1).Range("D" & i + 5).PasteSpecial Paste:=xlValues, operation:=xlNone, skipblanks:= _
            False, Transpose:=False
        Workbooks(FName).Close
    
    Next i
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    End Sub
    Note: You can have as many pack IDs as you want in this one file, you just need to ensure that one pack id is placed 40 rows below the other (as i have programmed it this way). In this report, I inserted a row before the 2nd pack id, so that the 2nd pack id is at row 48, since the 1st one is on row 8.

    Do let me know if you face any issues.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  15. #15
    Registered User
    Join Date
    10-24-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro/VBA needed for opening excel file

    Thanks so much for your post and your help. I am a complete novice at this sort of thing though, and have no idea where to input this information. Once the information is in, how do I get it to act or will it work automatically?

    Thanks again for your help and I really appreciate the work you have put into it.

    Toby

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0