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
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