Hello Abdul
I don’t think I really understand what help it is that you are asking for now?
Originally Posted by
Abdul
... I think my VBA code in this sheet is not properly aligned.
I don’t understand what are you trying to say?
Your coding is a bit untidy. But that is personal choice. You can choose how you want to write your code
This is yours
Code:
Sub TestPaper()
Dim FoundCell As Range
Dim FindWhat As String
FindWhat = Worksheets("Test").Range("D3").Value
Set FoundCell = Worksheets("Result").Range("C:C").Find(What:=FindWhat, LookAt:=xlWhole)
If Not FoundCell Is Nothing Then
MsgBox "Register Number " & FindWhat & " is already submitted Test at " & Sheets("Test").Range("J6").Value & ". It can't be edited or modified. You have already secured " & Sheets("Test").Range("J5").Value & " marks."
Exit Sub
End If
Dim x As Long
Dim y As Worksheet
Set y = Sheets("Result")
Dim Answer2 As VbMsgBoxResult
Answer2 = MsgBox("Do you want to save your Result, If once submitted can't be edited", vbYesNo + vbQuestion + vbDefaultButton2, "Save Result")
If Answer = vbYes Then
Else
x = Sheets("Result").Range("B" & Rows.Count).End(xlUp).Row
With y
Sheets("Test").Range("D2:D4").Copy
.Range("B" & x + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=True
.Cells(x + 1, 6).Value = Sheets("Test").Range("J2").Value
.Cells(x + 1, 5).Value = Sheets("Test").Range("J4").Value
Sheets("Test").Range("L4:AZ4").Copy
.Range("G" & x + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Sheets("Test").Range("L5:AZ5").Copy
.Range("Q" & x + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
With Sheets("Result").Range("A2:Z100" & x)
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
With Sheets("Result").Range("A3:A" & x + 1)
.Formula = "=Row() - 2"
.Value = .Value
With Sheets("Result").Range("A2:Z" & x + 1)
.BorderAround xlContinuous
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Borders(xlInsideVertical).LineStyle = xlContinuous
End With
End With
End With
MsgBox "Your Result is submitted successfully. You have secured Total Marks is " & Sheets("Test").Range("J4").Value
ActiveWorkbook.Save
'MsgBox "Your Data not saved, you can make correction if you want then submit again"
Exit Sub
End If
End Sub
This would be mine ( one small note: you had one obvious error Answer instead of Answer2 )
Code:
Option Explicit
Sub TestPaper()
Dim FoundCell As Range
Dim FindWhat As String
Let FindWhat = Worksheets("Test").Range("D3").Value
Set FoundCell = Worksheets("Result").Range("C:C").Find(What:=FindWhat, LookAt:=xlWhole)
If Not FoundCell Is Nothing Then
MsgBox "Register Number " & FindWhat & " is already submitted Test at " & Sheets("Test").Range("J6").Value & ". It can't be edited or modified. You have already secured " & Sheets("Test").Range("J5").Value & " marks."
Exit Sub
Else
End If
Dim x As Long
Dim y As Worksheet
Set y = Sheets("Result")
Dim Answer2 As VbMsgBoxResult
Let Answer2 = MsgBox("Do you want to save your Result, If once submitted can't be edited", vbYesNo + vbQuestion + vbDefaultButton2, "Save Result")
If Answer2 = vbYes Then
Else
Let x = Sheets("Result").Range("B" & Rows.Count).End(xlUp).Row
With y
Sheets("Test").Range("D2:D4").Copy
.Range("B" & x + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=True
.Cells(x + 1, 6).Value = Sheets("Test").Range("J2").Value
.Cells(x + 1, 5).Value = Sheets("Test").Range("J4").Value
Sheets("Test").Range("L4:AZ4").Copy
.Range("G" & x + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Sheets("Test").Range("L5:AZ5").Copy
.Range("Q" & x + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
End With
With Sheets("Result").Range("A2:Z100" & x)
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
With Sheets("Result").Range("A3:A" & x + 1)
.Formula = "=Row() - 2"
.Value = .Value
End With
With Sheets("Result").Range("A2:Z" & x + 1)
.BorderAround xlContinuous
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Borders(xlInsideVertical).LineStyle = xlContinuous
End With
MsgBox "Your Result is submitted successfully. You have secured Total Marks is " & Sheets("Test").Range("J4").Value
ActiveWorkbook.Save
'MsgBox "Your Data not saved, you can make correction if you want then submit again"
Exit Sub
End If
End Sub
Note: I have not full tested or checked your coding because I do not understand what you want.
The test data you gave does not fully test the coding because with your test data you are in situation …..3) If Reg. No. already saved in column C of the Result sheet is same as Reg. No. D3 of the Test sheet, then I want to alert message as "Register Number " & FindWhat & " is already submitted Test at " & Sheets("Test").Range("J6").Value & ". It can't be edited or modified. You have already secured " & Sheets("Test").Range("J5").Value & " marks."……
If you want any more help, then I think you will need to explain better what help you want.
Alan
Bookmarks