If current data exist in database then exit, else run code...

Xceller

Active Member
Joined
Aug 24, 2009
Messages
265
Hello,

I am new to vba, and run into some problems with the codes below. Want to copy some range of data from an Input sheet to a database sheet. Before running the copy and paste codes I need to check if the current data already exist in the database. If already exist then exit else copy and paste.

Sub copyNpaste()

Dim WSI As Worksheet
Dim WSD As Worksheet


Set WSI = Worksheets("Input")
Set WSD = Worksheets("dBASE")


FinalRow = Sheets("dBASE").Cells(Rows.Count, 1).End(xlUp).Row
dRange = Sheets("dBASE").Range("A2").Resize(FinalRow - 1, 1)
CurrentDate = Sheets("Input").Range("E5").Value

For Each cell In dRange
If cell.Value = CurrentDate Then
MsgBox ("Data already exist in database")
Exit Sub
End If
Next cell

WSD.Cells(FinalRow + 1, 1).Resize(7, 1).Value = WSI.Range("C14:C20").Value
WSD.Cells(FinalRow + 8, 1).Resize(7, 1).Value = WSI.Range("C24:C30").Value

WSD.Cells(FinalRow + 1, 2).Resize(7, 1).Value = WSI.Range("O14:O20").Value
WSD.Cells(FinalRow + 8, 2).Resize(7, 1).Value = WSI.Range("O24:O30").Value

WSD.Cells(FinalRow + 1, 3).Resize(7, 1).Value = WSI.Range("S14:S20").Value
WSD.Cells(FinalRow + 8, 3).Resize(7, 1).Value = WSI.Range("S24:S30").Value

WSD.Cells(FinalRow + 1, 4).Resize(7, 1).Value = WSI.Range("T14:T20").Value
WSD.Cells(FinalRow + 8, 4).Resize(7, 1).Value = WSI.Range("T24:T30").Value

WSD.Cells(FinalRow + 1, 5).Resize(7, 1).Value = WSI.Range("U14:U20").Value
WSD.Cells(FinalRow + 8, 5).Resize(7, 1).Value = WSI.Range("U24:U30").Value

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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