code copy data from user form to last columns into sheet not fill values

Hasson

Active Member
Joined
Apr 8, 2021
Messages
392
Office Version
  1. 2016
Platform
  1. Windows
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
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
this is my picture to help understand
1.JPG
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,215,947
Messages
6,127,867
Members
449,410
Latest member
adunn_23

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