+ Reply to Thread
Results 1 to 17 of 17

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

Hybrid View

  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.

    Option Explicit
    
    ' variables within a subroutine are saved, then used, then cleared after the subroutine ends
    Sub subVehicleClear()
        ' declare the variable type (dim = dimension)
        ' instead of a value we use an input box that allows the user to enter a value manually
        'Dim varClearTime As Date
        Dim varRoute As Integer
        varRoute = InputBox("Route Number")
        ' activate the worksheet that the table is on
        wsClears.Activate
        
        ' goes the the first available cell in column 1 in the table
        ' this needs to be a search of some kind for my entered 3 digit value
        Range("A2").End(xlDown).Offset(1, 0).Select
         
        ' enter new route number on the empty row
        ActiveCell.Value = varRoute
        ' enter the timestamp of the clear 1 column right ( i think Offset(0, 3) would put it 3 columns to the right.
        ActiveCell.Offset(0, 1).Value = Format(Now, "HH:mm")
            
    End Sub
    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
    8,031

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

    Give this a try for one method.
    Sub AddTime()
        Dim lc As Long, i As Long
        Dim r As Range
        Dim strInput As String: strInput = InputBox("Enter Run Number", "")
        
        With ThisWorkbook.Sheets("Sheet1") 'CHANGE THIS TO YOUR SHEET NAME
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            Set r = .Columns(1)
            For i = 4 To lc
                If .Cells(1, i) = "Run" Then Set r = Application.Union(r, .Columns(i))
            Next i
        End With
        
        r.Find(strInput, , xlValues, xlWhole).Offset(, 3) = Format(Now, "hh:mm")
    End Sub
    BSB

  3. #3
    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.
        r.Find(strInput, , xlValues, xlWhole).Offset(, 3) = Format(Now, "hh:mm")

  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

    Quote Originally Posted by BadlySpelledBuoy View Post
    Give this a try for one method.
    Sub AddTime()
        Dim lc As Long, i As Long
        Dim r As Range
        Dim strInput As String: strInput = InputBox("Enter Run Number", "")
        
        With ThisWorkbook.Sheets("Sheet1") 'CHANGE THIS TO YOUR SHEET NAME
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            Set r = .Columns(1)
            For i = 4 To lc
                If .Cells(1, i) = "Run" Then Set r = Application.Union(r, .Columns(i))
            Next i
        End With
        
        r.Find(strInput, , xlValues, xlWhole).Offset(, 3) = Format(Now, "hh:mm")
    End Sub
    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.
        r.Find(strInput, , xlValues, xlWhole).Offset(, 3) = Format(Now, "hh:mm")

  5. #5
    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:
    Option Explicit
    
    ' variables within a subroutine are saved, then used, then cleared after the subroutine ends
    Sub subVehicleClear()
    Dim SrchRng As Range, cel As Range
    Dim varRoute As Integer
        varRoute = InputBox("Route Number")
        Set SrchRng = Union(Range("A:A"), Range("E:E"), Range("I:I"))
        ' activate the worksheet that the table is on
        Sheets("Sheet1").Activate
        
    For Each cel In SrchRng
        If InStr(1, cel.Value, varRoute) > 0 Then
            cel.Offset(0, 3).Value = Format(Now, "HH:mm")
        End If
    Next cel
         
    End Sub
    Click the * to say thanks.

  6. #6
    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:
    Option Explicit
    
    ' variables within a subroutine are saved, then used, then cleared after the subroutine ends
    Sub subVehicleClear()
    Dim SrchRng As Range, cel As Range
    Dim varRoute As Integer
        varRoute = InputBox("Route Number")
        Set SrchRng = Union(Range("A:A"), Range("E:E"), Range("I:I"))
        ' activate the worksheet that the table is on
        Sheets("Sheet1").Activate
        
    For Each cel In SrchRng
        If InStr(1, cel.Value, varRoute) > 0 Then
            cel.Offset(0, 3).Value = Format(Now, "HH:mm")
        End If
    Next cel
         
    End Sub
    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.

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

    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.
    Sub AddTime()
        Dim lc As Long, i As Long
        Dim r As Range, r2 As Range
        Dim strInput As String: strInput = InputBox("Enter Run Number", "")
        
        With ThisWorkbook.Sheets("Sheet1")
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            Set r = .Columns(1)
            For i = 4 To lc
                If .Cells(1, i) = "Run" Then Set r = Application.Union(r, .Columns(i))
            Next i
        End With
        
        Set r2 = r.Find(strInput, , xlValues, xlWhole)
        If Not r2 Is Nothing Then
            r2.Offset(, 3) = Format(Now, "hh:mm")
        Else
            MsgBox "No trace of that number I'm afraid.", , ""
        End If
    End Sub
    BSB
    Attached Files Attached Files
    Last edited by BadlySpelledBuoy; 09-30-2021 at 10:25 AM.

  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 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.
    Sub AddTime()
        Dim lc As Long, i As Long
        Dim r As Range, r2 As Range
        Dim strInput As String: strInput = InputBox("Enter Run Number", "")
        
        With ThisWorkbook.Sheets("Sheet1")
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            Set r = .Columns(1)
            For i = 4 To lc
                If .Cells(1, i) = "Run" Then Set r = Application.Union(r, .Columns(i))
            Next i
        End With
        
        Set r2 = r.Find(strInput, , xlValues, xlWhole)
        If Not r2 Is Nothing Then
            r2.Offset(, 3) = Format(Now, "hh:mm")
        Else
            MsgBox "No trace of that number I'm afraid.", , ""
        End If
    End Sub
    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 :
    For i = 4 To lc
    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.

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

    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.

    Sub test()
        Dim r As Range, rng As Range, ff As String, varRoute, myCols, i As Long
        varRoute = InputBox("Route Number")
        If varRoute = "" Then Exit Sub
        myCols = Filter([if(1:1="Run",column(1:1))], False, 0)
        Set rng = Columns(Val(myCols(0)))
        For i = 0 To UBound(myCols)
            Set rng = Union(rng, Columns(Val(myCols(i))))
        Next
        Set r = rng.Find(varRoute, , , 1)
        If Not r Is Nothing Then
            ff = r.Address
            Do
                r(, 4) = Time
                r(, 4).NumberFormat = "hh:mm"
                Set r = rng.FindNext(r)
            Loop Until ff = r.Address
        End If
    End Sub

  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.

    Sub test()
        Dim r As Range, rng As Range, ff As String, varRoute, myCols, i As Long
        varRoute = InputBox("Route Number")
        If varRoute = "" Then Exit Sub
        myCols = Filter([if(1:1="Run",column(1:1))], False, 0)
        Set rng = Columns(Val(myCols(0)))
        For i = 0 To UBound(myCols)
            Set rng = Union(rng, Columns(Val(myCols(i))))
        Next
        Set r = rng.Find(varRoute, , , 1)
        If Not r Is Nothing Then
            ff = r.Address
            Do
                r(, 4) = Time
                r(, 4).NumberFormat = "hh:mm"
                Set r = rng.FindNext(r)
            Loop Until ff = r.Address
        End If
    End Sub
    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
    8,031

    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
    8,031

    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.

    Sub AddTime()
        Dim lc As Long, i As Long
        Dim r As Range, r2 As Range
        Dim strInput As String: strInput = InputBox("Enter Run Number", "")
        
        With ThisWorkbook.Sheets("AM clears")
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            Set r = .Columns(1)
            For i = 4 To lc
                If .Cells(1, i) = "RUN #" Then Set r = Application.Union(r, .Columns(i))
            Next i
        End With
        
        Set r2 = r.Find(strInput, , xlValues, xlPart)
        If Not r2 Is Nothing Then
            r2.Offset(, 4) = Format(Now, "hh:mm")
        Else
            MsgBox "No trace of that number I'm afraid.", , ""
        End If
    End Sub
    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.

    Sub AddTime()
        Dim lc As Long, i As Long
        Dim r As Range, r2 As Range
        Dim strInput As String: strInput = InputBox("Enter Run Number", "")
        
        With ThisWorkbook.Sheets("AM clears")
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            Set r = .Columns(1)
            For i = 4 To lc
                If .Cells(1, i) = "RUN #" Then Set r = Application.Union(r, .Columns(i))
            Next i
        End With
        
        Set r2 = r.Find(strInput, , xlValues, xlPart)
        If Not r2 Is Nothing Then
            r2.Offset(, 4) = Format(Now, "hh:mm")
        Else
            MsgBox "No trace of that number I'm afraid.", , ""
        End If
    End Sub
    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
    8,031

    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.
    Sub AddTime()
        Dim lc As Long, i As Long
        Dim r As Range, r2 As Range
        'SHOW THE INPUT BOX ASKING FOR THE RUN NUMBER
        Dim strInput As String: strInput = InputBox("Enter Run Number", "")
        
        'EVERYTHING BETWEEN THE NEXT LINE AND THE END WITH LINE WILL ACT ON THE AM Clears SHEET
        With ThisWorkbook.Sheets("AM clears")
            'DYNAMICALLY FIND THE LAST USED COLUMN - THIS MEANS IF YOU ADD MORE COLUMNS IN FUTURE YOU WON'T HAVE TO AMEND THE CODE TO ACCOUNT FOR THEM
            lc = .Cells(1, Columns.Count).End(xlToLeft).Column
            'SET A RANGE CONSISTING OF THE FIRST COLUMN (A).
            Set r = .Columns(1)
            'LOOP FROM COLUMN 7 (THE SECOND INSTANCE OF 'RUN#') UNTIL THE LAST USED COLUMN - DETERMINED ABOVE
            For i = 7 To lc
                'CHECK IF THE CURRENT COLUMN IN THE LOOP SAYS "RUN #" IN ROW 1 AND IF SO, ADD IT TO THE RANGE ALONG WITH ANY PREVIOUS COLUMNS THAT FIT THAT CRITERIA
                If .Cells(1, i) = "RUN #" Then Set r = Application.Union(r, .Columns(i))
            Next i
        End With
        
        'SEARCH THROUGH THE RANGE (ALL COLUMNS WITH "RUN #" AS A HEADER) FOR ANY INSTANCE OF THE RUN NUMBER INPUT EARLIER.
        Set r2 = r.Find(strInput, , xlValues, xlPart)
        'CHECK THAT A MATCH WAS FOUND
        If Not r2 Is Nothing Then
            'IF A MATCH WAS FOUND, STAMP THE TIME 4 COLUMNS TO THE RIGHT OF THE CELL WITH THE MATCH
            r2.Offset(, 4) = Format(Now, "hh:mm")
        Else
            'IF NO MATCH IS FOUND, ALERT THE USER.
            MsgBox "No trace of that number I'm afraid.", , ""
        End If
    End Sub
    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
    8,031

    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