• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Input form that disallows duplicates with matching fields

wamaral

New Member
Hello, just wanted to apologize if I don't seem clear as I am truely lost with VBA and this is actually my first post on these forums. Basically I have a workbook with two main spreadsheets, first as an input spreadsheet where a VBA form is used to input a record while the second serves as the actual database. Currently I have the form in working order with an error message that displays when all fields of the form aren't filled. I would like to add an error message for adding a duplicate entry but only when 3 of the fields match.


The form consists of an input for the following fields: Fund, Return, Date, Type


Fund- General Format (Or list, see rest of post)

Return- %

Date- mm/dd/yyyy format (Changing to list)

Type- List (Either "M" or "Q" can be selected)


I want an error message to occur when Fund, Date, and Type fields are already stored in the database even if the return field may be different when the form is used.


I made a forumla that brings a return only if the fund, date, and type fields match.


Code:
=INDEX(ReturnsData!D2:D9999,MATCH(1,(ReturnsData!C2:C9999=D21)*(ReturnsData!E2:E9999=Input!D23)*(ReturnsData!F2:F9999=Input!D25),0))


If there is a duplicate record with only the return field different, then there is no way to know which one it is pulling from. 


Another spreadsheet will be added that serves as the list of possible funds available for selection for the input user form. I tried to use data validation, but it is cumbersome because it does not autosuggest possible funds when you begin to type and the list will be long. Is there a way to implement this as well? A way to begin to type a fund and giving you a suggestion of funds that match the "possible funds" list? Similar to autofill in a google search. While updating the possible funds dropdown if another fund is added to the "possible funds" list?


Here is the code as it is currently written:

[pre]Sub UpdateLogWorksheet()

Dim historyWks As Worksheet
Dim inputWks 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 Input sheet - some contain formulas
myCopy = "D5,D7,D9, D11"

Set inputWks = Worksheets("Input")
Set historyWks = Worksheets("ReturnsData")

With historyWks
nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With

With inputWks
Set myRng = .Range(myCopy)

If Application.CountA(myRng) <> myRng.Cells.Count Then
MsgBox "Please fill in all the cells!"
Exit Sub
End If
End With

With historyWks
With .Cells(nextRow, "A")
.Value = Now
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
.Cells(nextRow, "B").Value = Application.UserName
oCol = 3
For Each myCell In myRng.Cells
historyWks.Cells(nextRow, oCol).Value = myCell.Value
oCol = oCol + 1
Next myCell
End With

'clear input cells that contain constants
With inputWks
On Error Resume Next
With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
.ClearContents
Application.GoTo .Cells(1) ', Scroll:=True
End With
On Error GoTo 0
End With
End Sub
[/pre]

Thank you all in advance for your help!


*Updated with excel file


http://www.mediafire.com/view/?mn8lrz2o4onvwum
 
Hi, wamaral!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the three first green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about your question...


Give a look at this file:

https://dl.dropbox.com/u/60558749/Input%20form%20that%20disallows%20duplicates%20with%20matching%20fields%20-%20Return%20%28for%20wamaral%20at%20chandoo.org%29.xlsm


I added_

a) 3 dynamic named ranges for columns C, E and F in ReturnsData worksheet

b) a formula for existing error at D16 cell of Input worksheet

=NO(ESERROR(COINCIDIR(txt.Fund&txt.Date&txt.Type;FundData&DateData&TypeData;0))) -----> in english: =NOT(ISERROR(MATCH(txt.Fund&txt.Date&txt.Type,FundData&DateData&TypeData,0)))

c) a validation within the procedure UpdateLogWorksheet at modData module


Just advise if any issue.


Regards!


PS: code updated as:

-----

[pre]
Code:
Option Explicit

Sub UpdateLogWorksheet()

Dim historyWks As Worksheet
Dim inputWks 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 Input sheet - some contain formulas
myCopy = "D5,D7,D9, D11"

Set inputWks = Worksheets("Input")
Set historyWks = Worksheets("ReturnsData")

'validation
If Worksheets("Input").Range("txt.Error").Value Then
MsgBox "Error adding to DB. Fund/Date/Type already existing!", _
vbApplicationModal + vbCritical + vbOKOnly, "Error"
GoTo UpdateLogWorksheet_Exit
End If

With historyWks
nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With

With inputWks
Set myRng = .Range(myCopy)

If Application.CountA(myRng) <> myRng.Cells.Count Then
MsgBox "Please fill in all the cells!"
Exit Sub
End If
End With

With historyWks
With .Cells(nextRow, "A")
.Value = Now
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
.Cells(nextRow, "B").Value = Application.UserName
oCol = 3
For Each myCell In myRng.Cells
historyWks.Cells(nextRow, oCol).Value = myCell.Value
oCol = oCol + 1
Next myCell
End With

'clear input cells that contain constants
With inputWks
On Error Resume Next
With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
.ClearContents
Application.Goto .Cells(1) ', Scroll:=True
End With
On Error GoTo 0
End With

UpdateLogWorksheet_Exit:
Set historyWks = Nothing
Set inputWks = Nothing
Set myRng = Nothing
Set myCell = Nothing

End Sub
[/pre]
-----
 
That validation works perfectly thank you! I can use that dynamic range for my other formula as well instead of bringing it out to the 9999 row heh. I could also use that for my spreadsheet with the Fund List if only there was a way to incorporate an "autofill" for a dropdown list in the input sheet for the fund. You think it will be possible?
 
Hi, wamaral!

Glad you solved it. Thanks for your feedback and for your kind words too.

Download again the updated file from same previous link and check the 2 drop down list boxes generated by data validation for Fund and Type.

Regards!
 
Back
Top