+ Reply to Thread
Results 1 to 6 of 6

VBA code to copy from another workbook using multiple cell values

Hybrid View

  1. #1
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    VBA code to copy from another workbook using multiple cell values

    Hi all,

    I have this code which searches for the value in F6 in another workbook and returns the relevant row from that workbook.

    This might be an elementary question, but say I want to add another criteria , ie, I want the user to enter a value into cell F6 and, for example, F7, what changes would I need to make for this to happen?

    It needs to be done as just searching by F6 doesn't differentiate between duplicates, and I need the user to also enter a name, again for example, in F7 to locate the exact row required.

    Thanks.

    Sub LookforText()
    Dim bk1 As Workbook, sh1 As Worksheet, cell As Range
    Dim bk2 As Workbook, sh2 As Worksheet, r As Range
    
    Set bk1 = Workbooks("Credit Hire Review Assistant 4.02.xlsm")
    Set sh1 = bk1.Worksheets("Settlement")
    Set cell = sh1.Range("F6")    ' this remembers the cell where the search string is
      ' and can be used to produce the value on demand
    
    Set bk2 = Workbooks.Open("S:\Claims\Credit Hire\Credit Hire Spreadsheet\C Hire MI Sept 2011-.xlsx")
    Set sh2 = bk2.Worksheets("Sheet1")
    Set r = sh2.Range("D:D").Find(What:=cell.Value, _
      After:=Range("D3"), _
      LookIn:=xlValues, _
      LookAt:=xlWhole, _
      SearchOrder:=xlByRows, _
      SearchDirection:=xlNext, _
      MatchCase:=False)
    If r Is Nothing Then
     MsgBox "Not found"
    Else
     Application.Goto Reference:=r
    End If
    
    ActiveCell.EntireRow.Copy
    ActiveCell.Offset(1, 0).Select
    
    Application.DisplayAlerts = False
    
    Workbooks("Credit Hire Review Assistant 4.02.xlsm").Worksheets("Settlement").Activate
    Range("A40").Select
        Range("A40").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
            Range("F11").Select
            
    
    Workbooks("C Hire MI Sept 2011-.xlsx").Activate
    ActiveWorkbook.Close SaveChanges:=True
    
    
    
    
    
    
    End Sub

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA code to copy from another workbook using multiple cell values

    Value F6 is being matched to column D, F7 is matched to column ??
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: VBA code to copy from another workbook using multiple cell values

    Hi,

    Knew I'd forgotten something important...

    F7 would match with Column F

    Thanks,

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA code to copy from another workbook using multiple cell values

    Assuming the name is column A, something like this:
    Option Explicit
    
    Sub LookforText()
    Dim bk1 As Workbook, sh1 As Worksheet, cell As Range, cName As Range, cFIRST As Range
    Dim bk2 As Workbook, sh2 As Worksheet, r As Range
    
    Set bk1 = Workbooks("Credit Hire Review Assistant 4.02.xlsm")
    Set sh1 = bk1.Worksheets("Settlement")
    Set cell = sh1.Range("F6")      ' this remembers the cell where the search string is
                                    ' and can be used to produce the value on demand
    Set cName = sh1.Range("F7")
    
    Set bk2 = Workbooks.Open("S:\Claims\Credit Hire\Credit Hire Spreadsheet\C Hire MI Sept 2011-.xlsx")
    Set sh2 = bk2.Sheets("Sheet1")
    Set r = sh2.Range("D:D").Find(What:=cell.Value, After:=sh2.Range("D3"), LookIn:=xlValues, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
    If r Is Nothing Then
        MsgBox "Not found"
        Exit Sub
    End If
    
    Set cFIRST = r                  'remember the first match found
    Do
        If sh2.Range("A" & r.Row).Value = cName.Value Then          'check for name in column A
            r.EntireRow.Copy                                        'if matched, copy row
            sh1.Range("A40").PasteSpecial Paste:=xlPasteValues      'paste values to sh1
            bk2.Close SaveChanges:=True                             'close opened bk2
            Exit Sub
        End If
        Set r = sh2.Range("D:D").FindNext(r)                        'if not matched, find next row
    Loop Until r.Address = cFIRST.Address                           'stop searching when first match occurs again
    
    MsgBox "Not Found"                                              'no luck if we reached this point
    
    End Sub

  5. #5
    Registered User
    Join Date
    03-27-2012
    Location
    Crawley
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: VBA code to copy from another workbook using multiple cell values

    That works perfectly, thankyou.

    The one thing that I may need to change, it now comes up with the message box about there being a large amount of data on the clipboard and do I want to keep it (after pasting into sh1).

    How do I set this to automatically say no?

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA code to copy from another workbook using multiple cell values

    Application messages like tht can be suppressed by adding this near the top of your macro:

    Application.DisplayAlerts = False

    If that takes care of your need, please select Thread Tools from menu above and set this topic to SOLVED.

+ 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