C
Coza
The below code i am using to try to update values that are input into a
form.
My object is to ONLY insert a NEW ROW if there is NO matching values already
in the sheet(sheet2-"Data")
If there is NO matching values then a New Row is inserted and the values
from the form are placed into row "A"
If not then they are placed into the ROW that the values DO match.
Matching values are located in Column A & B & C.
Sheet2 = "Data"
Column A = Name
Column B = Month
Column C = Year
Userform1
Combobox2 = name
Combobox1 = Month
Spinbutton1(Textbox2) = year
If a user selects a NAME(combobox2) and Month(Combobox1) and
year(Spinbutton1) and these VALUES are located in the same ROW already in
the 'Data" Sheet(Sheet2) then ANY values that are placed in the numerous
Textboxes on the form are placed into the matching ROW in Sheet2.
If there is NO matches from ALL 3 selections(Combobox2; Combobox1 &
Spinbutton1) then a NEW ROW is inserted into Sheet2 and all values entered
into the Userform are placed into the NEW ROW.
Note: There maybe and will be Matching names(Column 1) and Matching
Months(Column 2) in sheet2 but that have a different Year(Column 3), so as
there is NOT 3 Matches then a NEW Row is needed to tbe inserted.
A new row is ONLY required IF ALL 3 (Combobox2, Combobox1 & Spinbutton1)
match values in Sheet2(Column1,2 & 3).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CODE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub CommandButton1_Click()' Button on form
Dim rngFound As Range
On Error Resume Next
With Worksheets("Data").Range("A:A") ' Sheet2
Set rngFound = .Find(What:=Me.ComboBox2.Value, After:=.Cells(1),
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:=False, Matchbyte:=False)
If rngFound.Value <> "" Then
If ComboBox2.Value = rngFound.Value Then ' Name selected
If ComboBox1.Value = rngFound.Offset(0, 1).Value Then ' Month Selected
If SpinButton1.Value = rngFound.Offset(0, 2).Value Then ' Year Selected
' Input the data to the Data Sheet WITHOUT inserting a NEW ROW, into
the MATCHING Values ROW
With Sheets("Data") ' Sheet2
..Select
rngFound.Offset(0, 3) = TextBox27.Value
rngFound.Offset(0, 4) = TextBox39.Value
rngFound.Offset(0, 5) = TextBox51.Value
rngFound.Offset(0, 6) = TextBox63.Value
rngFound.Offset(0, 7) = TextBox75.Value
rngFound.Offset(0, 8) = TextBox87.Value
rngFound.Offset(0, 9) = TextBox88.Value
End With
End If
End If
End If
Unload Me
Else
Sheets("Data").Select ' Sheet2
Rows("1:1").Select
Selection.Insert Shift:=xlDown ' INSERT NEW ROW as NO Values MATCHING
are found
Range("A1").Select
With UserForm1
Sheets("Data").Select ' Sheet2
Range("A1").Value = ComboBox2.Value ' Place these values into the NEW ROW
Range("B1").Value = ComboBox1.Value ' Place these values into the NEW ROW
Range("C1").Value = TextBox2.Value ' Place these values into the NEW ROW
Range("D1").Value = TextBox27.Value ' Place these values into the NEW ROW
Range("E1").Value = TextBox39.Value ' Place these values into the NEW ROW
Range("F1").Value = TextBox51.Value ' Place these values into the NEW ROW
Range("G1").Value = TextBox63.Value ' Place these values into the NEW ROW
Range("H1").Value = TextBox75.Value ' Place these values into the NEW ROW
Range("I1").Value = TextBox87.Value ' Place these values into the NEW ROW
Range("J1").Value = TextBox88.Value ' Place these values into the NEW ROW
End With
Unload Me
End If
End With
Sheet1.Select
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Can anyone assist with this code ?
The above code does NOT seem to work as expected and explained above.
Corey....
I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
form.
My object is to ONLY insert a NEW ROW if there is NO matching values already
in the sheet(sheet2-"Data")
If there is NO matching values then a New Row is inserted and the values
from the form are placed into row "A"
If not then they are placed into the ROW that the values DO match.
Matching values are located in Column A & B & C.
Sheet2 = "Data"
Column A = Name
Column B = Month
Column C = Year
Userform1
Combobox2 = name
Combobox1 = Month
Spinbutton1(Textbox2) = year
If a user selects a NAME(combobox2) and Month(Combobox1) and
year(Spinbutton1) and these VALUES are located in the same ROW already in
the 'Data" Sheet(Sheet2) then ANY values that are placed in the numerous
Textboxes on the form are placed into the matching ROW in Sheet2.
If there is NO matches from ALL 3 selections(Combobox2; Combobox1 &
Spinbutton1) then a NEW ROW is inserted into Sheet2 and all values entered
into the Userform are placed into the NEW ROW.
Note: There maybe and will be Matching names(Column 1) and Matching
Months(Column 2) in sheet2 but that have a different Year(Column 3), so as
there is NOT 3 Matches then a NEW Row is needed to tbe inserted.
A new row is ONLY required IF ALL 3 (Combobox2, Combobox1 & Spinbutton1)
match values in Sheet2(Column1,2 & 3).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CODE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub CommandButton1_Click()' Button on form
Dim rngFound As Range
On Error Resume Next
With Worksheets("Data").Range("A:A") ' Sheet2
Set rngFound = .Find(What:=Me.ComboBox2.Value, After:=.Cells(1),
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:=False, Matchbyte:=False)
If rngFound.Value <> "" Then
If ComboBox2.Value = rngFound.Value Then ' Name selected
If ComboBox1.Value = rngFound.Offset(0, 1).Value Then ' Month Selected
If SpinButton1.Value = rngFound.Offset(0, 2).Value Then ' Year Selected
' Input the data to the Data Sheet WITHOUT inserting a NEW ROW, into
the MATCHING Values ROW
With Sheets("Data") ' Sheet2
..Select
rngFound.Offset(0, 3) = TextBox27.Value
rngFound.Offset(0, 4) = TextBox39.Value
rngFound.Offset(0, 5) = TextBox51.Value
rngFound.Offset(0, 6) = TextBox63.Value
rngFound.Offset(0, 7) = TextBox75.Value
rngFound.Offset(0, 8) = TextBox87.Value
rngFound.Offset(0, 9) = TextBox88.Value
End With
End If
End If
End If
Unload Me
Else
Sheets("Data").Select ' Sheet2
Rows("1:1").Select
Selection.Insert Shift:=xlDown ' INSERT NEW ROW as NO Values MATCHING
are found
Range("A1").Select
With UserForm1
Sheets("Data").Select ' Sheet2
Range("A1").Value = ComboBox2.Value ' Place these values into the NEW ROW
Range("B1").Value = ComboBox1.Value ' Place these values into the NEW ROW
Range("C1").Value = TextBox2.Value ' Place these values into the NEW ROW
Range("D1").Value = TextBox27.Value ' Place these values into the NEW ROW
Range("E1").Value = TextBox39.Value ' Place these values into the NEW ROW
Range("F1").Value = TextBox51.Value ' Place these values into the NEW ROW
Range("G1").Value = TextBox63.Value ' Place these values into the NEW ROW
Range("H1").Value = TextBox75.Value ' Place these values into the NEW ROW
Range("I1").Value = TextBox87.Value ' Place these values into the NEW ROW
Range("J1").Value = TextBox88.Value ' Place these values into the NEW ROW
End With
Unload Me
End If
End With
Sheet1.Select
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Can anyone assist with this code ?
The above code does NOT seem to work as expected and explained above.
Corey....
I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html