dcsimg
Results 1 to 7 of 7

Thread: Vlookyp and paste special

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    58

    Vlookyp and paste special

    Hiya.

    Quick and easy one for the pro's on here.

    I need a macro to quickly lookup the date in the Control tab cell c7, look this up in column C on Data tab and paste special values the corresponding FX rate in that same row. i.e C9.


    Name:  Capture1.JPG
Views: 94
Size:  38.5 KBName:  Capture 2.JPG
Views: 92
Size:  47.1 KB

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,560

    Re: Vlookyp and paste special

    Something like this:

    Code:
    Sub getVal()
        Dim wb As Workbook
        Dim wsC As Worksheet
        Dim wsD As Worksheet
        Dim lr As Long
        Dim J As Long
        Dim found As Boolean
        Dim currentWorkDay As String
        Dim fxRate As Single
        
        Set wb = ActiveWorkbook
        Set wsC = wb.Worksheets("control")
        Set wsD = wb.Worksheets("data")
        lr = wsD.Range("c1").End(xlDown).Row
        currentWorkDay = wsC.Range("c8").Value
        For J = 3 To lr
            If wsD.Range("c" & J).Value = currentWorkDay Then
                found = True
                fxRate = wsC.Range("c10").Value
                wsD.Range("a" & J).Value = fxRate
                Exit For
            End If
        Next J
        If found = False Then
            MsgBox "Could not find matching day"
        End If
    End Sub

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,497

    Re: Vlookyp and paste special

    to use vlookup the lookup column (in this case day) would have to be to left of the return value
    rather than looping all the cells till the day matches, you could use excel's find method to return the range for the day value, then the offset of that cell
    Code:
    set fnd = sheets("data").range("c:c").find(sheets("control").range("c7")
    if not fnd is nothing then
        sheets("control").range("c10") = fnd.offset(,-2)
        else
        msgbox "Day not found"
    end if
    Last edited by westconn1; Jul 10th, 2019 at 04:15 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    58

    Re: Vlookyp and paste special

    Morning.

    Just re-read my original post and I my original requirements were wrong...


    I need a macro to quickly lookup the date in the Control tab cell c7, look this up in column C on Data tab and paste special values the corresponding FX rate in that same row. i.e C9.

    When I said C9.... this was wrong

    I need to paste value special the FX rate in column A alongside the correct day found in tab 'control' cell 'I7'

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,497

    Re: Vlookyp and paste special

    tab 'control' cell 'I7'
    what has I7 got to do with the sheets shown?

    FX rate in column A alongside the correct day
    try like
    Code:
    if not fnd is nothing then
        fnd.offset(,-2) = sheets("control").range("c10")
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    58

    Re: Vlookyp and paste special

    Sorry I meant C7 on the control tab instead of I7.

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,910

    Re: Vlookyp and paste special

    Quote Originally Posted by westconn1 View Post
    to use vlookup the lookup column (in this case day) would have to be to left of the return value
    rather than looping all the cells till the day matches, you could use excel's find method to return the range for the day value, then the offset of that cell
    Code:
    set fnd = sheets("data").range("c:c").find(sheets("control").range("c7")
    if not fnd is nothing then
        sheets("control").range("c10") = fnd.offset(,-2)
        else
        msgbox "Day not found"
    end if
    There is a combination of the INDEX/MATCH-functions, that work the same as VLOOKUP, but the lookup-column can be anywhere on your sheet (yes, even to the right)
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width