My VBA not working

Nishant94

Well-known Member
Joined
May 8, 2015
Messages
514
Office Version
  1. 365
Platform
  1. Windows
  2. 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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I find it strange that you put the code in the Worksheet Event area? Since you are protecting the entire sheet (except for cell C8) it is not that a person can change anything on the sheet anyways and trigger the change event.

He is the code that you can put on a standard module (or back in the change event)...up to you.
I have assumed what you want to do with the macro and made some changes to the macro and the order of the code.

Code:
Sub GetBills()
Dim num1 As String
Dim num2 As Double
Dim num3 As Double
Dim i As Integer
    
    If [C5] = "94C" Then
        ActiveSheet.Unprotect ("PASSWORD")
        [C8].Locked = True
        'To Assign the sum of bill in C8
        num1 = InputBox("Please enter the number of bill:")
        If num1 = "" Then Exit Sub
        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.Protect ("PASSWORD")
    '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
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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