Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-05-2016, 03:01 PM
krispykreme krispykreme is offline Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match Windows 7 64bit Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match Office 2010 64bit
Novice
Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match
 
Join Date: Sep 2016
Posts: 4
krispykreme is on a distinguished road
Default Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match

Hi



I am attempting to compare 2 columns in separate workbooks, and if a match is found, to return the data in columns 38-40 from one workbook into columns 38-40 of the second workbook. I thought the best way to do this was to progressively loop through the values of the column in WB1, and compare with the other WB using a vlookup function. Unfortunately macros are not my strongest suit and essentially I've been trying to draw upon my knowledge of other coding languages and translate it into macro. This probably isn't the best way. I'm currently going through tutorials in order to find a more efficient way to do this function, but in the meantime, here's what I have:

Code:
Sub DataTransfer()
Dim WBNEW As Workbook 'current week's workbook
Dim WBOLD As Workbook 'previous week's workbook
Dim WSNEW As Worksheet 'current week's worksheet
Dim WSOLD As Worksheet 'previous week's worksheet

'Open all relevant workbooks also make them macro enabled?
Set WBNEW = Workbooks("wk8.xlsm") 'change name of current week's workbook
Set WBOLD = Workbooks("wk7.xlsm") 'change name of previous week's workbook
Set WSNEW = WBNEW.Worksheets("Open cases") 'change if relevant worksheet has different name
Set WSOLD = WBOLD.Worksheets("Open cases") 'change if relevant worksheet has different name

Dim last As Double
With WSNEW
        last = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
'counts the last row

Dim inew As Range
Set inew = WSNEW.Range("A2:A" & last)
'sets the range to be searched in previous week's workbook (Col 1 all rows)

Dim i As Variant
Dim v1 As String
Dim v2 As String
Dim v3 As String

For Each i In inew
    v1 = Application.VLookup(i, WSOLD, 38, False)
    v2 = Application.VLookup(i, WSOLD, 39, False)
    v3 = Application.VLookup(i, WSOLD, 40, False)
    Next
    On Error Resume Next
End Sub
The workbooks will vary on a week by week basis, thus I thought to define them earlier to make it easier to adjust, and the range of active cells in the sheet will vary week by week too (not sure if I've properly defined the array). Code seems to be getting stuck once it hits the Vlookups so I must be doing something wrong there. This is probably a pretty terrible code but any advice would be appreciated.

Thanks!
Reply With Quote
  #2  
Old 09-09-2016, 11:25 AM
charlesdh charlesdh is offline Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match Windows 7 32bit Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match Office 2010 32bit
Expert
 
Join Date: Apr 2014
Location: Mississippi
Posts: 382
charlesdh is on a distinguished road
Default

Hi,

Can you attach the workbooks?
Reply With Quote
Reply

Tags
for each loop, function validation, vlookup



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match How to compare two columns and match data Neydo222 Excel 2 09-12-2015 02:49 AM
Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match Excel Formula: return a range of cells that match tinfanide Excel 4 08-30-2014 07:03 AM
Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match match two columns of data gbaker Excel 1 08-03-2014 04:01 PM
How to separate specific columns from the data kumar Word 1 06-04-2012 01:46 PM
Compare cells in 2 columns in separate WBs and return data in Col 38-40 if there is match Compare data in different columns in an Excel sheet kgfendi Excel 5 05-16-2009 05:42 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft