|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
How to copy a value from source workbook if if doesn't exist in Active workbook
New to this forum so if I make a mistake, please don't hang me.. I am seeking a method of copying a value from column c in source workbook to column c in Activeworkbook, if it doesn't already exist.
|
#2
|
||||
|
||||
If the cells' row numbers correspond and the blanks you want to fill are truly blank then it could be as simple as:
Code:
'copying from Sheet1 to Sheet2 where sheet2 has true blank cells: For Each cll In Sheets("Sheet2").Range("C3:C15").SpecialCells(xlCellTypeBlanks).Cells Sheets("Sheet1").Range(cll.Address).Copy cll 'or 'cll.Value = Sheets("Sheet1").Range(cll.Address).Value Next cll |
#3
|
|||
|
|||
Thank you P45cal.
|
#4
|
|||
|
|||
I'm not sure if I'm not being clear enough in my request. Source workbook Sheet 1 Column c contains a list of names and I'm looking to copy these over to the Active Workbook, Sheet 1 Column C. If the name already exists in the Target workbook then don't copy, as Active workbook Sheet 1.column C needs to be a unique list of names.
My original thinking was to simply copy the full column over and then create a Unique list afterwards, but then I thought why not simply copy the name over if it didn't already exist. Am I wrong on both accounts as the best method? |
#5
|
||||
|
||||
Probably not
That may depend on how big the two ranges are and the arrangement of your data. What version of Excel are you using? If MS365 then you can put a temporary formula at the bottom of the existing list of the active sheet, then convert the results of that formula to plain values: Code:
Sub blah() Set SourceSht = Workbooks("Book2").Sheets("Sheet1") 'adjust this so that SourceSht is your source sheet. With SourceSht Set RngSource = Range(.Range("C2"), .Cells(.Rows.Count, "C").End(xlUp)) 'this is your source data range; adjust C2 to the top cell of that range. 'SourceAddress = RngSource.Address(external:=True) 'for the .formula2 version below. SourceAddressR1C1 = RngSource.Address(, , xlR1C1, external:=True) 'for the .Formula2R1C1 version below. End With With ActiveSheet Set DestnCell = .Cells(.Rows.Count, "C").End(xlUp).Offset(1) 'this is the single cell where the formula will go. Set RngExisting = .Range(.Range("C3"), DestnCell.Offset(-1)) 'this is your existing data on the active sheet, adjust C3 to the top cell of your data. 'ExistingAddress = RngExisting.Address 'for the .formula2 version below. ExistingAddressR1C1 = RngExisting.Address(, , xlR1C1) 'for the .Formula2R1C1 version below. End With 'choose one of these lines below: '********************************* DestnCell.Formula2R1C1 = "=LET(a," & SourceAddressR1C1 & ",FILTER(a,(ISERROR(MATCH(a," & ExistingAddressR1C1 & ",0)))*(a<>"""")))" 'DestnCell.Formula2 = "=LET(a," & SourceAddress & ",FILTER(a,(ISERROR(MATCH(a," & ExistingAddress & ",0)))*(a<>"""")))" '********************************* 'Convert results to plain values: If DestnCell.SpillingToRange Is Nothing Then If IsError(DestnCell) Then DestnCell.ClearContents Else DestnCell.Value = DestnCell.Value Else DestnCell.SpillingToRange.Value = DestnCell.SpillingToRange.Value End If End Sub Older versions of Excel: Code:
Sub blah2() Set SourceSht = Workbooks("Book2").Sheets("Sheet1") 'adjust this so that SourceSht is your source sheet. With SourceSht RngSource = Range(.Range("C2"), .Cells(.Rows.Count, "C").End(xlUp)).Value 'this is your source data; adjust C2 to the top cell of that range. End With With ActiveSheet Set DestnCell = .Cells(.Rows.Count, "C").End(xlUp).Offset(1) 'this is the single cell where the results will begin. RngExisting = .Range(.Range("C3"), DestnCell.Offset(-1)).Value 'this is your existing data on the active sheet, adjust C3 to the top cell of your data. End With For Each v In RngSource If Not IsEmpty(v) Then If Len(Application.Trim(v)) > 0 Then xx = Application.Match(v, RngExisting, 0) If IsError(xx) Then DestnCell.Value = v Set DestnCell = DestnCell.Offset(1) End If End If End If Next v End Sub |
#6
|
|||
|
|||
Okay I'll give that a go. Thank you once again P45cal.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Display workbook filename in title bar and use personal workbook | Jamal NUMAN | Excel Programming | 3 | 08-04-2022 02:56 AM |
How do I copy a workbook to another workbook and keep the formatting within the new workbook | excelforsue | Excel | 3 | 09-15-2020 03:37 AM |
data entered in one workbook should be updated in other relevant workbook based on the date | vedha | Excel | 0 | 04-24-2015 08:45 PM |
Workbook with daily active sheet | MDMeyers | Excel | 1 | 01-10-2015 04:48 AM |
Range(Cell1,Cell2) Error on another workbook controlling some other workbook? | tinfanide | Excel Programming | 1 | 02-09-2012 04:08 PM |