+ Reply to Thread
Results 1 to 17 of 17

VBA Search for value in separate columns then paste the date 3 columns right

  1. #1
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    VBA Search for value in separate columns then paste the date 3 columns right

    Sorry for the long header.

    I tried searching the forum and google but could not find precisely what I was looking for.
    I am very new to VBA.
    I have a worksheet with the following structure (this is a smaller example)
    It is not in a table format. Just individual cells with text in them

    Column A Column B Column C Column D Column E Column F Column G Column H Column I Column J Column K Column L
    Run Route Number Time Run Route Number Time Run Route Number Time
    100 150123 2079 200 810123 1309 300 840123 20146
    101 150101 2048 201 810101 2027 301 840102 20144
    102 150202 1325 202 810256 1305 302 840302 16190

    I can put a button on the worksheet and assign a subroutine to it (i know how to do this part).
    When i press that button I want an input box to pop up.
    Then I manually enter a 3 digit value, lets say 202, and press ok
    The subroutine should search the worksheet 202 I entered then input the current time as HH:MM in column D, H or L.

    I started with this code so far but then got kind of stumped on how to search only columns A, E, I.
    If there is a better or cleaner way to search for the values in only those columns, then I would love to see that.
    I cant search the entire worksheet because, in my 202 example, that 202 appears in other cells too.

    Here is the code I started with All it does is take the 202 value then place the time (HH:MM) beside it then moves to the next row and does it again with every new value I have.
    I don't know how to get it to do the search for 202 and place the time value (HH:MM) 3 columns to the right.
    Sorry about the excessive comments. I do that for my own benefit.

    If you need more clarification. please let me know.

    Please Login or Register  to view this content.
    I feel like this needs a LOOP where it searches 1 column for the 3 digit value if if positive enter "HH:MM" and end sub and if negative, go to next column and repeat.

    Thank you, Gray.
    Last edited by GrayWolf; 09-30-2021 at 09:53 AM. Reason: more clarity added

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Give this a try for one method.
    Please Login or Register  to view this content.
    BSB

  3. #3
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Or to this method:
    Please Login or Register  to view this content.
    Click the * to say thanks.

  4. #4
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    This one gives me an error
    Runtime error "91":
    Object variable or With block variable not set

    the debug hi-lights the following line.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Quote Originally Posted by BadlySpelledBuoy View Post
    Give this a try for one method.
    Please Login or Register  to view this content.
    BSB
    This one gives me an error
    Runtime error "91":
    Object variable or With block variable not set

    the debug hi-lights the following line.
    Please Login or Register  to view this content.

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    That would be because you entered a number that it couldn't find.

    It can be error handled to cope with that.
    Please Login or Register  to view this content.
    BSB
    Attached Files Attached Files
    Last edited by BadlySpelledBuoy; 09-30-2021 at 10:25 AM.

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

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Assuming the header in 1st row.
    Search the value in "Run" column(s) and put time to the 3 cells to the right.
    Find method.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Quote Originally Posted by PaulM100 View Post
    Or to this method:
    Please Login or Register  to view this content.
    Hi PaulM100, I like your method and I understand most of it so it is easy for me to alter based on the size of other tables.
    This code however runs slower than the other methods and so won't be as useful for me.
    I need the search and paste of time to be almost instant due to the rapid nature in which I'll need to use this code.
    Also in hind sight I noticed that a few of the values I am searching in the "RUN #" look like M-202 or 202-M instead of the normal 202. Is there a way for the search to still fine 202?

    Thank you for your assistance.

  9. #9
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Quote Originally Posted by BadlySpelledBuoy View Post
    That would be because you entered a number that it couldn't find.

    It can be error handled to cope with that.
    Please Login or Register  to view this content.
    BSB
    Hello again BadlySpelledBuoy,
    I was able to run the code in the file you provided but I was not able to adapt this to my full size worksheet.
    This will find the values in Column A and accurately stamp the HH:MM in the correct column (with some tweaks) but cannot find values in any other columns.
    I tried to modify this value in the code :
    Please Login or Register  to view this content.
    to a different number but it would not work on any other columns than just column A.
    since my RUN columns on the larger sheet are in A, G, M, S, Y (5 columns between Runs.

    Also I noticed that a few of the values I am searching in the "RUN #" look like M-202 or 202-M instead of the normal 202.

    Thank you very much for you assistance.
    Last edited by GrayWolf; 09-30-2021 at 11:58 AM.

  10. #10
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Quote Originally Posted by jindon View Post
    Assuming the header in 1st row.
    Search the value in "Run" column(s) and put time to the 3 cells to the right.
    Find method.

    Please Login or Register  to view this content.
    Thank you very much jindon. This seems to be working very well for me although I do not understand most of this at all.
    I did have 2 questions:
    1) a few of the values I am searching in the "RUN #" look like M-202 or 202-M instead of the normal 202. Is there a way for the search to still fine 202?
    2) how do I add some error handling for a value if not found similar to what BadlySpelledBuoy had in their code.

    Thank you very much for your help.

  11. #11
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Attach a copy of your workbook layout and we can adapt code to fit it. I wrote the code to work on the layout you demonstrated in your original post and it works fine on that. I'm sure it would be an easy tweak to make it work on your actual data layout.

    As for M-202 / 202-M, if you change xlWhole to xlPart in my code it will find 202 in any part of a cell.

    BSB

  12. #12
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Quote Originally Posted by BadlySpelledBuoy View Post
    Attach a copy of your workbook layout and we can adapt code to fit it. I wrote the code to work on the layout you demonstrated in your original post and it works fine on that. I'm sure it would be an easy tweak to make it work on your actual data layout.

    As for M-202 / 202-M, if you change xlWhole to xlPart in my code it will find 202 in any part of a cell.

    BSB
    Here is the attached file.
    The AM clear columns need the HH:MM timestamp and you can see some of the fields have -M appended to the route number. on other sheets it's M-

    Hope that helps.
    Attached Files Attached Files

  13. #13
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    I said it would be a nice easy tweak to make it work!
    The parts in red I've changed.

    Please Login or Register  to view this content.
    BSB
    Attached Files Attached Files
    Last edited by BadlySpelledBuoy; 09-30-2021 at 12:21 PM.

  14. #14
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Quote Originally Posted by BadlySpelledBuoy View Post
    I said it would be a nice easy tweak to make it work!
    The parts in red I've changed.

    Please Login or Register  to view this content.
    BSB
    WOW thank you ... this is kind of weird though, because my tweaks lookd identical to yours with the exception of xlPart and xlWhole and yet mind did not search anything after the first column.
    But when I copied your code over top of mine and saved it and ran it.. BOOM .. worked fine.
    Oh well. It works. ha ha .

    Would it be too much to ask what is going on in the code, so I can comment it for myself, and others.

    Thank you so much for your help.

  15. #15
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Happy to help and glad we got there in the end!

    Code again below with some comments added to explain what's going on.
    Please Login or Register  to view this content.
    Let me know if you need more of an explanation than that.

    BSB
    Last edited by BadlySpelledBuoy; 09-30-2021 at 12:37 PM.

  16. #16
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Huge thank you to BadlySpelledBuoy for you help and patience.
    Special thank you to everyone who offered Assistance. Thank you very much everyone.
    Marked as Solved.
    Last edited by GrayWolf; 09-30-2021 at 01:19 PM.

  17. #17
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: VBA Search for value in separate columns then paste the date 3 columns right

    Glad we could help

    BSB

+ 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. Search by columns headers and paste all matching columns to another sheet
    By maamon in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-13-2021, 10:10 AM
  2. Replies: 5
    Last Post: 01-17-2020, 06:21 AM
  3. Replies: 1
    Last Post: 01-09-2020, 02:48 PM
  4. Loop through all txt files and paste as separate columns in excel
    By bjcowen9000 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-20-2018, 12:26 AM
  5. Replies: 2
    Last Post: 06-01-2015, 05:25 AM
  6. Split date into 3 separate columns
    By paulr24 in forum Excel General
    Replies: 4
    Last Post: 01-25-2012, 01:51 PM
  7. Search columns and paste rows
    By chambone in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-04-2009, 10:56 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