VBA Userform to check file for records, count them and then write to same file

bcassidy1986

New Member
Joined
Aug 20, 2014
Messages
6
Afternoon,

I'm currently building a VBA user form that will be used for ticking off friend referrels from current customers.

Existing customers can refer up to 5 friends to us - so my userform firstly needs to check the old policy number against the list that has already been compiled and if this old policy number has not appeared 5 or more times write the detials entered to the same file it just checked.

So far my code looks like;

Code:
Private Sub CheckButton_Click()
    'set dimensions
        Dim FVWorkbook As Workbook
        Dim FVWorksheet As Worksheet
        Dim OldPolicyRange As Range
        Dim SearchFor As Long
        Dim OldCount As Long
        
    'check username is entered
        If Trim(Me.UserName.Value) = "" Then
            Me.UserName.SetFocus
            message = MsgBox("Please enter your user name", vbCritical, "Missing User Name")
            Exit Sub
        End If
        
    'check new policy number entered and correct length
        If Len(Me.NewPolicy.Text) <> 9 Then
            Me.NewPolicy.SetFocus
            message = MsgBox("New Policy number missing or not correct length", vbCritical, "New Policy Number Error")
            Exit Sub
        End If
    
    'check old policy number entered and correct length
        If Len(Me.OldPolicy.Text) <> 9 Then
            Me.OldPolicy.SetFocus
            message = MsgBox("Old Policy number missing or not correct length", vbCritical, "Old Policy Number Error")
            Exit Sub
        End If
        
    'set variables
        Set FVWorkbook = Workbooks.Open("S:\Loyalty Scheme.xlsx")
        Set FVWorksheet = FVWorkbook.Worksheets(1)
        Set OldPolicyRange = FVWorksheet.Range("A:A")
        SearchFor = OldPolicy.Value
    'if count is greater than zero show messege
        If Application.WorksheetFunction.CountIf(OldPolicyRange, SearchFor) >= 5 Then
            OldCount = Application.WorksheetFunction.CountIf(OldPolicyRange, SearchFor)
            FVWorkbook.Close False
            messege = MsgBox("Old Policy has referred " & OldCount & " Times, customer can not refer again" & vbCr & vbCr & "Check detials and click 'Clear Details' if needed", vbCritical, "Customer Referred Too Many Times")
        Else
        If Application.WorksheetFunction.CountIf(OldPolicyRange, SearchFor) > 0 Then
            OldCount = Application.WorksheetFunction.CountIf(OldPolicyRange, SearchFor)
            FVWorkbook.Close True
            messege = MsgBox("Old Policy has referred " & OldCount & " time(s) already not including this one")
        Else
            FVWorkbook.Close False
            messege = MsgBox("Policy number not in list")
        End If
        End If
            
End Sub

So the user form asks for the old policy number and the new policy number as well as a user name - it already checks that the policy number is the correct 9 digits and can then opens the log of already submitted records, makes sure the old policy number does not appear 5 or more times. If it does it flashes up a warning messege, if it doesnt i would then like it to write to the same file it just read...

And thats when i get stuck; i've tried using the open to append and even then asking it to find the last row add one and write directly to cell references but it's not working.

Hope you can help with this.

Cheers
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,215,790
Messages
6,126,911
Members
449,348
Latest member
Rdeane

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