The following VB code is applied to my worksheet named “NewMemoâ€.
The purpose if this code is for the simultaneous movement of data that is entered into the fields mentioned in the code, to the sheets named “OrderData†& “Memosâ€.
If one of the fields from the range ("D3,D8,H8,H9,H12,H13,J9,J12,J13,J26,J8,F8,J7") is not filled, a message box appears saying that “please fill all the fieldsâ€.
As a consequence of this, none of the data in the fields ("D3,D8,H8,H9,H12,H13,J9,J12,J13,J26,J8,F8,J7") gets copied or moved into the sheet “Memosâ€.
But the data within the range A16:A25 & I16:I25 gets copied into the sheet “OrderDataâ€, Thus preventing the simultaneous movement of the data from ("D3,D8,H8,H9,H12,H13,J9,J12,J13,J26,J8,F8,J7") & (A16:A25 & I16:I25).
Since all the fields in the range ("D3,D8,H8,H9,H12,H13,J9,J12,J13,J26,J8,F8,J7") is mandatory to be filled, what I want from you is a modification of the existing code so that if one field from the range ("D3,D8,H8,H9,H12,H13,J9,J12,J13,J26,J8,F8,J7") is not filled, the data in the range (A16:A25 & I16:I25) should stay in the “NewMemo†sheet until the empty field from the data range ("D3,D8,H8,H9,H12,H13,J9,J12,J13,J26,J8,F8,J7") is filled.
Note:
It is not mandatory to fill the fields from the rangeA16:A25 & I16:I25. Data should be moved even if one field is filled from the range A16:A25 & I16:I25.
Your help in this would be greatly appreciated.
VB Code as mention above;
Code: Select all
Sub CopyToDATA()
On Error Resume Next
Application.ScreenUpdating = False
Dim r As Long
Dim m As Long
Dim n As Long
Dim MemosWks As Worksheet
Dim NewMemoWks As Worksheet
Dim OrderWks As Worksheet
Dim nextRow As Long
Dim oCol As Long
Dim myRng As Range
Dim myCopy As String
Dim myCell As Range
'cells to copy from NewMemo sheet - some contain formulas
myCopy = "D3,D8,H8,H9,H12,H13,J9,J12,J13,J26,J8,F8,J7"
Set NewMemoWks = Worksheets("NewMemo")
Set MemosWks = Worksheets("Memos")
Set OrderWks = Worksheets("OrderData")
' Use column C because column A contains "Total" (and B is empty)
m = NewMemoWks.Range("I" & NewMemoWks.Rows.Count).End(xlUp).Row
' Headers are now in row 4
If m = 15 Then
MsgBox "No data", vbExclamation
Exit Sub
End If
r = OrderWks.Range("C" & OrderWks.Rows.Count).End(xlUp).Row + 1
' Copy Code
NewMemoWks.Range("A16:A" & m).Copy Destination:=OrderWks.Range("C" & r)
' Copy Quantity
NewMemoWks.Range("I16:I" & m).Copy Destination:=OrderWks.Range("G" & r)
' Copy Category as values
NewMemoWks.Range("C16:C" & m).Copy
OrderWks.Range("D" & r & ":D" & (r + m - 16)).PasteSpecial Paste:=xlPasteValues
' Copy Description as values
NewMemoWks.Range("F16:F" & m).Copy
OrderWks.Range("E" & r & ":E" & (r + m - 16)).PasteSpecial Paste:=xlPasteValues
' Copy Rate as values
NewMemoWks.Range("H16:H" & m).Copy
OrderWks.Range("F" & r & ":F" & (r + m - 16)).PasteSpecial Paste:=xlPasteValues
' Copy Value as values
NewMemoWks.Range("J16:J" & m).Copy
OrderWks.Range("H" & r & ":H" & (r + m - 16)).PasteSpecial Paste:=xlPasteValues
' Copy Serial number
OrderWks.Range("B" & r & ":B" & (r + m - 16)) = NewMemoWks.Range("D3")
' Copy Date
NewMemoWks.Range("H12").Copy Destination:=OrderWks.Range("A" & r & ":A" & (r + m - 16))
OrderWks.Range("A5:H5").Copy
OrderWks.Range("A" & r & ":H" & (r + m - 16)).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
With MemosWks
nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With
With NewMemoWks
Set myRng = .Range(myCopy)
If Application.CountA(myRng) <> myRng.Cells.Count Then
MsgBox "Please fill in all the fields!"
Exit Sub
End If
End With
With MemosWks
With .Cells(nextRow, "A")
.Value = Now
.NumberFormat = "hh:mm:ss"
End With
oCol = 2
For Each myCell In myRng.Cells
MemosWks.Cells(nextRow, oCol).Value = myCell.Value
oCol = oCol + 1
Next myCell
End With
With NewMemoWks.Range("D3")
.Value = .Value + 1
End With
NewMemoWks.Range("A16:A" & m & ",I16:I" & m & ",H12").ClearContents
'clear input cells that contain constants
With NewMemoWks
On Error Resume Next
With .Range("D8,H8,H9,H12,H13,J9,J12,J13,J26,J8,F8,J7").Cells.SpecialCells(xlCellTypeConstants)
.ClearContents
Application.GoTo .Cells(1) ', Scroll:=True
End With
On Error GoTo 0
End With
Application.ScreenUpdating = True
End Sub
Adam