+ Reply to Thread
Results 1 to 15 of 15

Never pastes in right line. Nearest number to number in a given cell.

  1. #1
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Never pastes in right line. Nearest number to number in a given cell.

    I have column A (contains numbers) column B (contains names).

    With a new customer I need to enter new info in A2 and B2 and then press button.

    The inserted line will appear after the nearest number bellow the given number.

    I.E. the new account number is 19 and the current accounts are 1, 2, 5, 6, 15, 20, 25.
    So the new line will appear between account 15 and 19.

    The code bellow sort of works but only if there is a account 18 but then inserts it above this line.



    Please Login or Register  to view this content.
    Last edited by Wales MB; 11-29-2012 at 01:45 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Find nearest number to given cell

    Hi

    Why don't you just sort the relevant cells based on column A?

    however:
    Please Login or Register  to view this content.

    rylo
    Last edited by rylo; 11-26-2012 at 06:24 PM.

  3. #3
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Find nearest number to given cell

    Because the data in my spreadsheet is copied/typed in from a printed sheet thats in a set order.

    Column A on the printed sheet is formatted by text (i know - why) so every thing starting with 1 is bunched together.

  4. #4
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Find nearest number to given cell

    PHP Code: 
    Option Explicit
    Sub WakesMB
    ()
     
    Dim WF As ObjectRng As RangesRng As Range
     Dim Rws 
    As LongMin_ As LongjJ As LongTmp As Long
     
     Rws 
    Cells(Rows.Count"a").End(xlUp).Row
     Set Rng 
    Range("A3:A" Rws)
     
    Set WF Application.WorksheetFunction
     Min_ 
    WF.Min(Range(Cells(3"A"), Cells(Rws"A")))
     For 
    jJ Cells(14).Value 1 To Min_ Step -1
        Set sRng 
    Rng.Find(jJ, , xlFormulasxlWhole)
        If 
    sRng Is Nothing Then
            Tmp 
    jJ
        
    Else
            
    sRng.Offset(1).EntireRow.Insert
            sRng
    .Offset(1).Resize(, 4).Value Cells(2"A").Resize(, 4).Value
            Cells
    (2"A").Resize(, 4).Value ""
            
    Exit For
        
    End If
     
    Next jJ
    End Sub 
    Pic121127.JPG

  5. #5
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Find nearest number to given cell

    rylo,

    Thanks for that, just one problem if my new account number is the new highest number it does not work....?

    Please Login or Register  to view this content.
    And the next question is how do I make this search also happen on Sheet 2 and Sheet 3.???????

    Been at this to long tonight i just cant get in straight in my head......
    
    Regards Wales MB

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Find nearest number to given cell

    Hi

    Expanded for a new max item, or new min item.

    Please Login or Register  to view this content.
    You should be able to run this on subsequent sheets, assuming that they all follow the same construct. Or do you mean that you want to take an item from sheet 1, and put it to all sheets?

    rylo

  7. #7
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Find nearest number to given cell

    Can not get this to work - i thought it was but just going round in circles.....

    I have attached the test file...

    This is one code i have played with.....

    Please Login or Register  to view this content.
    and this is the other

    Please Login or Register  to view this content.
    Neither really work.....

    What i want to do is enter a new customer name in A5 and account number in A6...

    Then run code that looks down column b find the nearest lower match to A6,
    then copies row5 and inserts it after the line that contains the lower match.

    I have tried loads of different idea's you can find them in the attached.
    Attached Files Attached Files
    Last edited by Wales MB; 11-27-2012 at 05:00 PM.

  8. #8
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Find nearest number to given cell

    Sorry just realised I amended the post instead of inserting a new one.

  9. #9
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Find nearest number to given cell

    Bump please

  10. #10
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Never pastes in right line. Nearest number to number in a given cell.

    Hi

    Your example file and your recut of my code don't seem to align. Can you recheck your example file and check and confirm exactly where the customer name and ID are going to be located.

    Also, the data in column B is either not sorted, or text sorted. Exactly what is going to be the situation regarding this data, and any new data to be included (with regard to value cf text).

    rylo

  11. #11
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Never pastes in right line. Nearest number to number in a given cell.

    Hi Rylo

    The attachment is a snap shot of the file i am working on.

    Row 5 is the new customer line, A5 and B5 are customer name and account number.
    B6 down is the current customers account number and A6 down would be there names,
    there are over 500 on the original file, but they have to stay in the order they are in.

    The daily data is supplied in hard copy only, never electronic so i cant change the
    sort..... I suppose that column B is sorted as text hence the strange order, but this
    can not be changed.... life is never straight forward.

    When new customers decide to use this system we have to add them to this list normally
    it is a new highest number, but occasionally one of the older numbers come on line.
    and just to be difficult some of the really old ones have alpa codes (not numeric).

    I tried to get the code you supplied to work hence it not linking up as you put it, I am very
    new to all this so I have got confused, trying to make it work..... Sorry.

    Hopefully that helps you understand.

    Regards Mike

  12. #12
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Never pastes in right line. Nearest number to number in a given cell.

    Hi

    Open your example file from #7, delete row 42 (to get rid of the blank row item in column B), then try

    Please Login or Register  to view this content.
    rylo

  13. #13
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Never pastes in right line. Nearest number to number in a given cell.

    Thanks for that, I can see that I mixed up in a number of places.

    I have loaded the code you supplied, and have attached file for you to see the following.

    1. When putting in account 1 the code insert a line but leaves it blank? Account 1 is
    using so I would not need to add them but i can not see why?

    2. When putting in a new highest number 10074, it placed it on row 39 and not on
    row 18, 10073 is on line 17, 9988 is on line 38.

    Thanks again for your support.....
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Never pastes in right line. Nearest number to number in a given cell.

    Hi

    next go.

    Please Login or Register  to view this content.
    rylo
    Last edited by rylo; 11-28-2012 at 06:16 PM.

  15. #15
    Registered User
    Join Date
    11-25-2012
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Never pastes in right line. Nearest number to number in a given cell.

    Thanks for you help.....

    The finished code.....

    Please Login or Register  to view this content.

+ 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