hi
I need help from experts to update this code . the code insert a new row by combobox when match with col A . so I add two textboxes PUR,SALE should fill the values into the last three columns contain header PUR, SELL.every month I add three columns contain (PUR,SELL,NET) . so if I add anew data not are existed in COL B,C,D then insert a new row and fill IN COL B,C,D and fill the values in last three columns contain PUR,SALE and if i fill data on userform are existed in COL B,C,D then it shouldn't insert a new row just update the data in last three columns contain PUR,SELL
I hope this clear despite of it's complicated
this is my picture to help understand
I need help from experts to update this code . the code insert a new row by combobox when match with col A . so I add two textboxes PUR,SALE should fill the values into the last three columns contain header PUR, SELL.every month I add three columns contain (PUR,SELL,NET) . so if I add anew data not are existed in COL B,C,D then insert a new row and fill IN COL B,C,D and fill the values in last three columns contain PUR,SALE and if i fill data on userform are existed in COL B,C,D then it shouldn't insert a new row just update the data in last three columns contain PUR,SELL
I hope this clear despite of it's complicated
VBA Code:
Private Sub CmdOK_Click()
Me.Tag = 1
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = msoTrue
Me.Hide
End Sub
Private Sub CmdAddItem_Click()
Dim MyForm As UserForm1
Dim Rt As Long
Set MyForm = New UserForm1
With MyForm
SetCbxCat .ComboBox1, ActiveSheet
.Tag = ""
.Show
If Val(.Tag) = 1 Then InsertCategory MyForm, ActiveSheet
End With
Unload MyForm
Set MyForm = Nothing
End Sub
Option Explicit
Sub SetCbxCat(Cbx As MSForms.ComboBox, _
Ws As Worksheet)
Dim SelRow As Long
Dim n As Integer
Dim i As Integer
Dim Arr As Variant
Dim R As Long
With Cbx
.RowSource = ""
.ColumnCount = 3
.ColumnWidths = .Width & " pt;0 pt;0 pt"
.ListWidth = .Width
.BoundColumn = 1
End With
SelRow = ActiveCell.Row
With Ws
Arr = .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A")).Value
For R = 3 To UBound(Arr)
If Len(Arr(R, 1)) Then
With Cbx
.AddItem Arr(R, 1)
.List(n, 1) = R
If n Then .List(n - 1, 2) = R - 1
End With
If R < SelRow Then i = n
n = n + 1
End If
Next R
Cbx.List(n - 1, 2) = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
With Cbx
.ListIndex = i
.MatchRequired = True
End With
End Sub
Sub InsertCategory(MyForm As Object, _
Ws As Worksheet)
Dim Rng As Range
Dim Rf As Long
Dim Rt As Long
Dim Cl As Long
With MyForm.ComboBox1
Rf = .List(.ListIndex, 1)
Rt = .List(.ListIndex, 2)
End With
With Ws
Cl = .Cells(Rt, .Columns.Count).End(xlToLeft).Column
.Rows(Rt - 1).Copy
.Rows(Rt).Insert Shift:=xlDown
On Error Resume Next
.Rows(Rt).SpecialCells(xlCellTypeConstants).ClearContents
On Error GoTo 0
.Cells(Rt, "B").Value = MyForm.TextBox1.Value
.Cells(Rt, "C").Value = MyForm.TextBox2.Value
.Cells(Rt, "D").Value = MyForm.TextBox3.Value
Set Rng = .Range(.Cells(Rt + 1, "E"), .Cells(Rt + 1, Cl))
End With
Application.CutCopyMode = False
With Rng
.Cells(1).Formula = "=SUM(E" & Rf & ":E" & Rt & ")"
.FillRight
End With
End Sub