Copying data only if it doesn't already exist

EllieOCon

New Member
Joined
Jan 23, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm a VBA newb and need some expert help please.

I've got the following already but I need the part after "'COPY DATA FROM qryHIRES TO EA INTEL LIGHT" only if the data doesn't already exist in the second sheet. What do I need to add?

Sub EALightReport()
'
' EALightReport Macro


' ENTER YES, DATE AND NAME

Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "A").End(xlUp).Row ' <-- get last row in column "A"
.Range("A" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = "Yes"
Windows("qryHires_onHire.xlsm").Activate

End With

Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "B").End(xlUp).Row ' <-- get last row in column "B"
.Range("B" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Format(Date, "DD/MM/YY")
Windows("qryHires_onHire.xlsm").Activate

End With

Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "C").End(xlUp).Row ' <-- get last row in column "C"
.Range("C" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Application.UserName
Windows("qryHires_onHire.xlsm").Activate

End With




'COPY DATA FROM qryHIRES TO EA INTEL LIGHT


Range("B143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "D").End(xlUp).Row ' <-- get last row in column "D"
.Range("D" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("D" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate
End With


Range("C143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "E").End(xlUp).Row ' <-- get last row in column "E"
.Range("E" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("E" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("E143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "F").End(xlUp).Row ' <-- get last row in column "F"
.Range("F" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("F" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("F143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "G").End(xlUp).Row ' <-- get last row in column "G"
.Range("G" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("G" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("I143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "H").End(xlUp).Row ' <-- get last row in column "H"
.Range("H" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("H" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("K143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "I").End(xlUp).Row ' <-- get last row in column "I"
.Range("I" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("I" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("N143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "K").End(xlUp).Row ' <-- get last row in column "K"
.Range("K" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("K" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("O143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "L").End(xlUp).Row ' <-- get last row in column "L"
.Range("L" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("L" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("P143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "M").End(xlUp).Row ' <-- get last row in column "M"
.Range("M" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("M" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("Q143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "N").End(xlUp).Row ' <-- get last row in column "N"
.Range("N" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("N" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With


Range("R143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "O").End(xlUp).Row ' <-- get last row in column "O"
.Range("O" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("O" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With

Range("T143").Select
Selection.Copy
Windows("EA Intelligent Lights.xlsx").Activate
With Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")
RowCount = .Cells(.Rows.Count, "Q").End(xlUp).Row ' <-- get last row in column "Q"
.Range("Q" & RowCount + 1) = workOrderDescription
Sheets("2023").Range("Q" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Windows("qryHires_onHire.xlsm").Activate

End With
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try :-
VBA Code:
Sub EALightReport()
    ' ENTER YES, DATE AND NAME
    Dim ws As Worksheet
    Set ws = Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")

    With ws
        RowCount = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("A" & RowCount + 1) = workOrderDescription
        If Not ValueExistsInColumn("B", workOrderDescription, ws) Then
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = "Yes"
        End If
        .Range("C" & RowCount + 1) = workOrderDescription
        If Not ValueExistsInColumn("D", workOrderDescription, ws) Then
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Format(Date, "DD/MM/YY")
        End If
        .Range("E" & RowCount + 1) = workOrderDescription
        If Not ValueExistsInColumn("F", workOrderDescription, ws) Then
            .Range("F" & Rows.Count).End(xlUp).Offset(1, 0) = Application.UserName
        End If

        ' COPY DATA FROM qryHIRES TO EA INTEL LIGHT
        Windows("qryHires_onHire.xlsm").Activate
        Range("B143").Copy
        Windows("EA Intelligent Lights.xlsx").Activate
        If Not ValueExistsInColumn("D", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "D").End(xlUp).Row
            .Range("D" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("C143").Copy
        If Not ValueExistsInColumn("E", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "E").End(xlUp).Row
            .Range("E" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("E143").Copy
        If Not ValueExistsInColumn("F", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "F").End(xlUp).Row
            .Range("F" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("F143").Copy
        If Not ValueExistsInColumn("G", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "G").End(xlUp).Row
            .Range("G" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("I143").Copy
        If Not ValueExistsInColumn("H", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "H").End(xlUp).Row
            .Range("H" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("K143").Copy
        If Not ValueExistsInColumn("I", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "I").End(xlUp).Row
            .Range
 
Upvote 0
Hi EllieOCon,

maybe

VBA Code:
Sub EALightReport()
' https://www.mrexcel.com/board/threads/copying-data-only-if-it-doesnt-already-exist.1227771/
'
' EALightReport Macro


' ENTER YES, DATE AND NAME

Dim wbLights As Workbook
Dim wbQryHires As Workbook
Dim wsSrc As Worksheet
Dim ws2023 As Worksheet
Dim rowCount As Long
Dim strAdrSrc As String
Dim strArdTarg As String
Dim varSrc As Variant
Dim varTarg As Variant
Dim lngCnt As Long

strAdrSrc = "B143,C143,E143,F143,I143,K143,N143,O143,P143,Q143,R143,T143"
strArdTarg = "D,E,F,G,H,I,K,L,M,N,O,Q"
varSrc = Split(strAdrSrc, ",")
varTarg = Split(strArdTarg, ",")

Set wbQryHires = Workbooks("qryHires_onHire.xlsm")
Set wsSrc = wbQryHires.ActiveSheet

Set wbLights = worksbooks("EA Intelligent Lights.xlsx")
Set ws2023 = wbLights.Worksheets("2023")

With ws2023
  rowCount = .Cells(.Rows.Count, "A").End(xlUp).Row ' <-- get last row in column "A"
  'the term workOrderDescription isn`t defined in this procedure???
  .Range("A" & rowCount + 1).Value = workOrderDescription
  .Range("A" & rowCount + 2).Value = "Yes"
  
  rowCount = .Cells(.Rows.Count, "B").End(xlUp).Row ' <-- get last row in column "B"
  .Range("B" & rowCount + 1).Value = workOrderDescription
  .Range("B" & rowCount + 2).Value = Format(Date, "DD/MM/YY")
  
  rowCount = .Cells(.Rows.Count, "C").End(xlUp).Row ' <-- get last row in column "C"
  .Range("C" & rowCount + 1).Value = workOrderDescription
  .Range("C" & rowCount + 2).Value = Application.UserName
  'I suggest you use Environ("UserName") which holds the name of teh user logged into the computer
  '.Range("C" & rowCount + 2)value = Environ("UserName")
  
  rowCount = .Cells(.Rows.Count, "D").End(xlUp).Row ' <-- get last row in column "D"
  .Range("D" & rowCount + 1).Value = workOrderDescription
  .Range("D" & rowCount + 2).Resize(1, 2).Value = wsSrc.Range("B143").Resize(1, 2).Value

  For lngCnt = LBound(varSrc) To UBound(varSrc)
    If WorksheetFunction.CountIf(.Range(varTarg(lngCnt) & 1).EntireColumn, wsSrc.Range(varSrc(lngCnt)).Value) = 0 Then
      rowCount = .Cells(.Rows.Count, varTarg(lngCnt)).End(xlUp).Row ' <-- get last row in column in question
      .Range(varTarg(lngCnt) & rowCount + 1).Value = workOrderDescription
      .Range(varTarg(lngCnt) & rowCount + 2).Value = wsSrc.Range(varSrc(lngCnt)).Value
    End If
  Next lngCnt

Set ws2023 = Nothing
Set wbLights = Nothing
Set wsSrc = Nothing
Set wbLights = Nothing

End Sub

Please have a look at How to Post Your VBA Code for posting codes in the future.


@sleek12:

a part of your code is missing.

Ciao,
Holger
 
Upvote 0
Hi EllieOCon,

maybe

VBA Code:
Sub EALightReport()
' https://www.mrexcel.com/board/threads/copying-data-only-if-it-doesnt-already-exist.1227771/
'
' EALightReport Macro


' ENTER YES, DATE AND NAME

Dim wbLights As Workbook
Dim wbQryHires As Workbook
Dim wsSrc As Worksheet
Dim ws2023 As Worksheet
Dim rowCount As Long
Dim strAdrSrc As String
Dim strArdTarg As String
Dim varSrc As Variant
Dim varTarg As Variant
Dim lngCnt As Long

strAdrSrc = "B143,C143,E143,F143,I143,K143,N143,O143,P143,Q143,R143,T143"
strArdTarg = "D,E,F,G,H,I,K,L,M,N,O,Q"
varSrc = Split(strAdrSrc, ",")
varTarg = Split(strArdTarg, ",")

Set wbQryHires = Workbooks("qryHires_onHire.xlsm")
Set wsSrc = wbQryHires.ActiveSheet

Set wbLights = worksbooks("EA Intelligent Lights.xlsx")
Set ws2023 = wbLights.Worksheets("2023")

With ws2023
  rowCount = .Cells(.Rows.Count, "A").End(xlUp).Row ' <-- get last row in column "A"
  'the term workOrderDescription isn`t defined in this procedure???
  .Range("A" & rowCount + 1).Value = workOrderDescription
  .Range("A" & rowCount + 2).Value = "Yes"
 
  rowCount = .Cells(.Rows.Count, "B").End(xlUp).Row ' <-- get last row in column "B"
  .Range("B" & rowCount + 1).Value = workOrderDescription
  .Range("B" & rowCount + 2).Value = Format(Date, "DD/MM/YY")
 
  rowCount = .Cells(.Rows.Count, "C").End(xlUp).Row ' <-- get last row in column "C"
  .Range("C" & rowCount + 1).Value = workOrderDescription
  .Range("C" & rowCount + 2).Value = Application.UserName
  'I suggest you use Environ("UserName") which holds the name of teh user logged into the computer
  '.Range("C" & rowCount + 2)value = Environ("UserName")
 
  rowCount = .Cells(.Rows.Count, "D").End(xlUp).Row ' <-- get last row in column "D"
  .Range("D" & rowCount + 1).Value = workOrderDescription
  .Range("D" & rowCount + 2).Resize(1, 2).Value = wsSrc.Range("B143").Resize(1, 2).Value

  For lngCnt = LBound(varSrc) To UBound(varSrc)
    If WorksheetFunction.CountIf(.Range(varTarg(lngCnt) & 1).EntireColumn, wsSrc.Range(varSrc(lngCnt)).Value) = 0 Then
      rowCount = .Cells(.Rows.Count, varTarg(lngCnt)).End(xlUp).Row ' <-- get last row in column in question
      .Range(varTarg(lngCnt) & rowCount + 1).Value = workOrderDescription
      .Range(varTarg(lngCnt) & rowCount + 2).Value = wsSrc.Range(varSrc(lngCnt)).Value
    End If
  Next lngCnt

Set ws2023 = Nothing
Set wbLights = Nothing
Set wsSrc = Nothing
Set wbLights = Nothing

End Sub

Please have a look at How to Post Your VBA Code for posting codes in the future.


@sleek12:

a part of your code is missing.

Ciao,
Holger
Hey

Thank you. I tried this one and got the following:
1681739250987.png
 
Upvote 0
Try :-
VBA Code:
Sub EALightReport()
    ' ENTER YES, DATE AND NAME
    Dim ws As Worksheet
    Set ws = Workbooks("EA Intelligent Lights.xlsx").Worksheets("2023")

    With ws
        RowCount = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("A" & RowCount + 1) = workOrderDescription
        If Not ValueExistsInColumn("B", workOrderDescription, ws) Then
            .Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = "Yes"
        End If
        .Range("C" & RowCount + 1) = workOrderDescription
        If Not ValueExistsInColumn("D", workOrderDescription, ws) Then
            .Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Format(Date, "DD/MM/YY")
        End If
        .Range("E" & RowCount + 1) = workOrderDescription
        If Not ValueExistsInColumn("F", workOrderDescription, ws) Then
            .Range("F" & Rows.Count).End(xlUp).Offset(1, 0) = Application.UserName
        End If

        ' COPY DATA FROM qryHIRES TO EA INTEL LIGHT
        Windows("qryHires_onHire.xlsm").Activate
        Range("B143").Copy
        Windows("EA Intelligent Lights.xlsx").Activate
        If Not ValueExistsInColumn("D", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "D").End(xlUp).Row
            .Range("D" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("C143").Copy
        If Not ValueExistsInColumn("E", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "E").End(xlUp).Row
            .Range("E" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("E143").Copy
        If Not ValueExistsInColumn("F", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "F").End(xlUp).Row
            .Range("F" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("F143").Copy
        If Not ValueExistsInColumn("G", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "G").End(xlUp).Row
            .Range("G" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("I143").Copy
        If Not ValueExistsInColumn("H", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "H").End(xlUp).Row
            .Range("H" & RowCount + 1).PasteSpecial xlPasteValues
        End If

        Range("K143").Copy
        If Not ValueExistsInColumn("I", workOrderDescription, ws) Then
            RowCount = .Cells(.Rows.Count, "I").End(xlUp).Row
            .Range
Hi

Thank you. I tried this one and got the attached error message:
 

Attachments

  • Error Message.PNG
    Error Message.PNG
    239.1 KB · Views: 7
Upvote 0
For the error in post# you are missing the opening Sub line.
It's best to copy the code using the copy icon in the top right of the code window
1681739925512.png
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top