+ Reply to Thread
Results 1 to 17 of 17

Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

Hybrid View

  1. #1
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Tried to see if I could get something like this, yet to see anything though, and just wondering if you guys could help.
    Basically, I have a list of Patients, each with their own row and with 3 different Dispatch dates.

    (1) I basically need a macro (activated by Command Button?) which asks for the user to input a date.

    (2) It will then Look in Columns D , G and J For All Rows and look for that date, E.g. 25/10/13

    (3) Then if it is matching that date in either of those 3 columns, it will copy the cells in column A and B for that row (Branch and Name) into a New Workbook ( and preferably sort that final list by Branch - 100 going down to 500)

    (4) Also, at the same time, in that same Workbook I need it to make another 5 Worksheets( So 6 in total), each one designated for each Branch E.g. Patients in Branch 100 and being dispatched on Inputted date, will be on one sheet, Branch 200 and being dispatched on same date on next one, etc,etc.

    (5) And if possible, the top of each of those final worksheets should Have 2 Cells Saying "Name: " & "Delivery No.:" - This is for the person to write their details beside.

    Sorry for having something so long-winded :D greatly appreciate any help.
    Attached Files Attached Files
    Last edited by pharmerjoe7; 11-26-2013 at 01:42 PM.

  2. #2
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Anyone able to make it do the first 3 points? I could figure out the rest after that.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    A bit confusing.

    Can you post a workbook that should be generated?

  4. #4
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Sorry, I realise my explanation isn't very good . I've attached what I want it to generate (or something which is as close to this visually as possible) and exampledispatchsheet .xls has a total of 6 sheets, which are all the customers to dispatch to on 25/10/13. First sheet is full list, sorted by Branch number, and next 5 are them split into separate branches.

    So It should take user input for the date, and then where I've put 25/10/13, it should put my Inputted date. Hope this is a bit clearer, need any clarification please let me know.
    Attached Files Attached Files

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Let's see if this is what you wanted.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Amazing! Thanks a lot jindon. Might make a few mods, but will post my final example tomorrow so everyone can see. Thanks again!

  7. #7
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Nitpicking here, but is there a way to stop the outputted excel file from squashing the names of the customers? The Date comes up fine, but just seems to be the names where the cell is too small and also in here

                        .Range("d2").Value = myDate
                        .Range("a3").Value = "Name:"
                        .Range("e3").Value = "Delivery No:"
    How to also put those cells in bold?

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Can you show me EXACTLY how you want in a workbook?

  9. #9
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Sorry, trying to make it like this. Just if Name and Delivery No is in bold, and also, if the sheet is made so that it autofits the cells in Column B for the patient names? So they don't get squashed
    And also, on this example, on Sheet 1 it places the list so that it tries to fit on one page as much as possible.
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Try change to
    Option Explicit
    
    Sub test()
        Dim dic As Object, myDate, a, i As Long, e, flg As Boolean, x As Range, t As Long
        Dim r As Range
        Set r = Application.InputBox("Select cell that has the date to pick", Type:=8)
        If Not IsDate(r.Value) Then
            MsgBox "Invalid entry": Exit Sub
        End If
        myDate = r.Value
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        With Sheets("sheet1").Range("a3").CurrentRegion
            a = .Value
            For i = 2 To UBound(a, 1)
                For Each e In Array(4, 7, 10)
                    If a(i, e) = myDate Then
                        flg = True: Exit For
                    End If
                Next
                If flg Then
                    If x Is Nothing Then Set x = .Rows(1).Columns("a:b")
                    If Not dic.exists(a(i, 1)) Then
                        Set dic(a(i, 1)) = .Rows(1).Columns("a:b")
                    End If
                    Set dic(a(i, 1)) = Union(dic(a(i, 1)), .Rows(i).Columns("a:b"))
                    Set x = Union(x, .Rows(i).Columns("a:b")): flg = False
                End If
            Next
        End With
        If Not x Is Nothing Then
            With Workbooks.Add
                x.Copy .Sheets(1).Range("a3")
                With .Sheets(1)
                    .Range("c1").Value = myDate
                    .Range("c1").Font.Bold = True
                    With .Range("a3").CurrentRegion
                        If .Rows.Count > 47 Then
                            For i = 48 To .Rows.Count Step 47
                                t = t + 3
                                .Rows(i).Resize(46).Cut .Cells(1, t)
                            Next
                        End If
                    End With
                    .Columns.AutoFit
                End With
                For i = 0 To dic.Count - 1
                    If .Sheets.Count < i + 2 Then
                        .Sheets.Add after:=.Sheets(.Sheets.Count)
                    End If
                    With .Sheets(i + 2)
                        dic.items()(i).Copy .Range("a5")
                        .Range("d2").Value = myDate
                        .Range("a3").Value = "Name:"
                        .Range("e3").Value = "Delivery No:"
                        .Range("a3,d2,e3").Font.Bold = True
                        .Columns.AutoFit
                        .Name = CStr(dic.keys()(i))
                    End With
                Next
                .SaveAs Replace(ThisWorkbook.Name, ".xlsm", " " & Format$(myDate, "yyyymmdd") & ".xlsx")
            End With
        Else
            MsgBox "No data"
        End If
    End Sub

  11. #11
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Works well, only thing is the columns on the generated excel file are not auto-fitted on every sheet. Some are, some aren't, not sure why. Maybe some line which autofits all column B's in the new Workbook?

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Working perfectly here.

  13. #13
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Ah right, it works on my sample one, but doesn't appear to be working on my actual one. If its ok, could I give you the real file with the macro inserted, I cannot figure out why it doesn't work on that.

  14. #14
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Say I wanted to set the column width for column B of each of the generated sheets, I would insert

    Columns("B").ColumnWidth = 25

    So I'd end up with

    Option Explicit
    
    Sub test()
        Dim dic As Object, myDate, a, i As Long, e, flg As Boolean, x As Range, t As Long
        Dim r As Range
        Set r = Application.InputBox("Select cell that has the date to pick", Type:=8)
        If Not IsDate(r.Value) Then
            MsgBox "Invalid entry": Exit Sub
        End If
        myDate = r.Value
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        With Sheets("sheet1").Range("a3").CurrentRegion
            a = .Value
            For i = 2 To UBound(a, 1)
                For Each e In Array(4, 8, 12)
                    If a(i, e) = myDate Then
                        flg = True: Exit For
                    End If
                Next
                If flg Then
                    If x Is Nothing Then Set x = .Rows(1).Columns("a:b")
                    If Not dic.exists(a(i, 1)) Then
                        Set dic(a(i, 1)) = .Rows(1).Columns("a:b")
                    End If
                    Set dic(a(i, 1)) = Union(dic(a(i, 1)), .Rows(i).Columns("a:b"))
                    Set x = Union(x, .Rows(i).Columns("a:b")): flg = False
                End If
            Next
        End With
        If Not x Is Nothing Then
            With Workbooks.Add
                x.Copy .Sheets(1).Range("a3")
                With .Sheets(1)
                    .Range("c1").Value = myDate
                    .Range("c1").Font.Bold = True
                    With .Range("a3").CurrentRegion
                    End With
                    .Columns.AutoFit
                    Columns("B").ColumnWidth = 25
                End With
                For i = 0 To dic.Count - 1
                    If .Sheets.Count < i + 2 Then
                        .Sheets.Add after:=.Sheets(.Sheets.Count)
                    End If
                    With .Sheets(i + 2)
                        dic.items()(i).Copy .Range("a5")
                        .Range("d2").Value = myDate
                        .Range("a3").Value = "Name:"
                        .Range("e3").Value = "Delivery No:"
                        .Range("a3,d2,e3").Font.Bold = True
                        .Columns.AutoFit
                        Columns("B").ColumnWidth = 25
                        .Name = CStr(dic.keys()(i))
                    End With
                Next
                .SaveAs Replace(ThisWorkbook.Name, ".xls", " " & Format$(myDate, "yyyymmdd") & ".xls")
            End With
        Else
            MsgBox "No data"
        End If
    End Sub

    But it only appears to set Column B to 25 for Sheet 1, and then misses the 2nd and 3rd sheet, then the rest are set to 25. Have I also to insert that code at another point?

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Add a period before those lines.

    i.e
    .Columns("B").ColumnWidth = 25

  16. #16
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Ok so this is my (nearly) finalised code for my main sheet, but one problem I have is that when I select a date which has many, many patients going out on it, like 150+, the Sheet 1 is a bit messed up, and it starts to get printed out on multiple sheets, with the branch code and customer name being split.
    Is there a way to try to fit the names on one page, then when it detects there isn't enough space, it starts a new page and pastes more there and so on?

    I increased the size of the testing file to illustrate it, and modified the code to roughly where I want it, but cannot figure out how I should get Sheet 1 how I want it.
    As an example, select a date like 3/12/2013 which has a lot of customers with that date and you'll see what I mean. really appreciate the help so far.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    09-16-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Copy 2 cells from each Row (into another Workbook) Based on Criteria of a third cell?

    Anyone able to help? Thats pretty much the only thing missing from it and then it'll be perfect.

+ 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. Copy two cells in on workbook to another workbook based on two criteria, but more
    By oldboots in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2013, 06:17 AM
  2. Copy two cells in on workbook to another workbook based on two criteria, but more
    By oldboots in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-16-2013, 05:44 AM
  3. Replies: 2
    Last Post: 07-25-2012, 08:11 AM
  4. Replies: 5
    Last Post: 06-08-2012, 11:26 PM
  5. Copy information from one cell to many cells based on certain criteria
    By thomasrawley in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-07-2006, 12:16 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