Nishant94
Well-known Member
- Joined
- May 8, 2015
- Messages
- 514
- Office Version
- 365
- Platform
- Windows
- MacOS
I have written a VBA in which I want the user to input the number of bills
And then the user will be asked to input the amount of each bill. and the sum of all the bill will be displayed in in C8
For example, if the user inputs that the number of bills is 4. Then, the user will be asked to input
Bill No. 1: 25000
Bill No. 2: 20000
Bill No. 3: 25000
Bill No. 4: 30000
The sum of all the bill 100000 will be entered in the cell C8.
Please help me.
I have written the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim num1 As Double
Dim num2 As Double
Dim num3 As Double
'Dim k As Integer
'Dim n As Integer
If [C5] = "94C" Then
ActiveSheet.Unprotect ("PASSWORD")
[C8].Locked = True
ActiveSheet.Protect ("PASSWORD")
'To Assign the sum of bill in C8
num1 = InputBox("Please enter the number of bill:")
'k = 17
'n = 3
num3 = 0
'looping
For i = 1 To num1
num2 = InputBox("Enter the amount of bill no. " & (i) & ":")
num3 = num3 + num2
Next i
'[C8].Value = num3
'ActiveSheet.Range("C8") = ActiveSheet.Range("C41")
'Remove locked property if C5's value is anything else or is deleted.
Else
ActiveSheet.Unprotect ("PASSWORD")
[C8].Locked = False
'To protect the sheet again
'ActiveSheet.Protect ("PASSWORD")
End If
Cells(8, 3).Value = num3
End Sub
And then the user will be asked to input the amount of each bill. and the sum of all the bill will be displayed in in C8
For example, if the user inputs that the number of bills is 4. Then, the user will be asked to input
Bill No. 1: 25000
Bill No. 2: 20000
Bill No. 3: 25000
Bill No. 4: 30000
The sum of all the bill 100000 will be entered in the cell C8.
Please help me.
I have written the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim num1 As Double
Dim num2 As Double
Dim num3 As Double
'Dim k As Integer
'Dim n As Integer
If [C5] = "94C" Then
ActiveSheet.Unprotect ("PASSWORD")
[C8].Locked = True
ActiveSheet.Protect ("PASSWORD")
'To Assign the sum of bill in C8
num1 = InputBox("Please enter the number of bill:")
'k = 17
'n = 3
num3 = 0
'looping
For i = 1 To num1
num2 = InputBox("Enter the amount of bill no. " & (i) & ":")
num3 = num3 + num2
Next i
'[C8].Value = num3
'ActiveSheet.Range("C8") = ActiveSheet.Range("C41")
'Remove locked property if C5's value is anything else or is deleted.
Else
ActiveSheet.Unprotect ("PASSWORD")
[C8].Locked = False
'To protect the sheet again
'ActiveSheet.Protect ("PASSWORD")
End If
Cells(8, 3).Value = num3
End Sub