Need help with userform vba

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
102
Hi, I have created a userform with 2 option buttons, 1 combobox and 2 textboxes, I also have 2 sheets Transfers and Master, if I select the add stock option button and fill out the rest of the form and click submit it adds stock in the master sheet column C which works fine the part I can not figure out is how to transfer stock with the other option button I need it to add the amount to column D then minus it from colum C. There is a link below with the file which will make more sense.

https://www.dropbox.com/s/8eln30oauh05p6i/Test_1.xlsm?dl=0

Regards
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,835
Office Version
365
Platform
Windows
How about
Code:
Private Sub CommandButton1_Click()
    Dim NewRow As Long
    Dim m As Variant
    
    If Not Me.opt_AddStock And Not Me.opt_TransferStock Then
      MsgBox "please select Add or Transfer"
      Exit Sub
   End If
    Application.DisplayAlerts = False
    
    With wsTransfers
        NewRow = .Range("A" & .Rows.count).End(xlUp).Row + 1
        .Range("A" & NewRow).Value = Me.ComboBox1.Text
        .Range("B" & NewRow).Value = Me.TextBox1.Text
        .Range("C" & NewRow).Value = Me.TextBox2.Text
        .Range("D" & NewRow).Value = IIf(Me.opt_AddStock.Value, "Added", "Transfered")
        .Range("E" & NewRow).Value = Now
    End With
'update master
    With wsMaster
        m = Application.Match(Val(Me.ComboBox1.Text), .Columns(1), 0)
        If Not IsError(m) Then
            With .Cells(CLng(m), "C")
               If Me.opt_AddStock Then
                  .Value = .Value + Val(Me.TextBox2.Value)
               ElseIf Me.opt_TransferStock Then
                  .Offset(, 1).Value = Val(Me.TextBox2.Value)
                  .Value = .Value - Val(Me.TextBox2.Value)
               End If
            End With
        End If
    End With
    Application.DisplayAlerts = True
    Unload Me
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,835
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
102
Hi,

I'm having a little trouble with the code in #2 when I select transfer stock it minuses from warehouse but does not add to the value that is already in holding area it replaces. So from the example below if I transferred 0050 it would minus that from warehouse
to show 0350 but would only show 0050 in holding area instead of 0100 any help would be great.

WarehouseHolding Area
PLUIngredients DescriptionTotal Weight kgTotal Weight kgTotal Stock kg
3011UNSALTED BUTTER ROASTED ONION040000500450

<colgroup><col><col><col span="2"><col></colgroup><tbody>
</tbody>
<strike></strike>

<colgroup><col><col><col span="2"><col></colgroup><tbody>
</tbody>
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,835
Office Version
365
Platform
Windows
How about
Code:
    With wsMaster
        m = Application.Match(Val(Me.ComboBox1.Text), .Columns(1), 0)
        If Not IsError(m) Then
            With .Cells(CLng(m), "C")
               If Me.opt_AddStock Then
                  .Value = .Value + Val(Me.TextBox2.Value)
               ElseIf Me.opt_TransferStock Then
                  .Offset(, 1).Value =[COLOR=#ff0000] .Offset(, 1).Value + [/COLOR]Val(Me.TextBox2.Value)
                  .Value = .Value - Val(Me.TextBox2.Value)
               End If
            End With
        End If
    End With
 

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
102
hi, i'm getting a run time error 424 object required on line: m =Application.Match(Val(Me.ComboBox1.Text), .Columns(1), 0)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,835
Office Version
365
Platform
Windows
Is that with your test file?
 

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
102
Hi, yes same file


Private Sub CommandButton1_Click()
Dim NewRow As Long
Dim m As Variant

If Not Me.opt_AddStock And Not Me.opt_TransferStock Then
MsgBox "please select Add or Transfer"
Exit Sub
End If
Application.DisplayAlerts = False

With wsTransfers
NewRow = .Range("A" & .Rows.count).End(xlUp).Row + 1
.Range("A" & NewRow).Value = Me.ComboBox1.Text
.Range("B" & NewRow).Value = Me.TextBox1.Text
.Range("C" & NewRow).Value = Me.TextBox2.Text
.Range("D" & NewRow).Value = IIf(Me.opt_AddStock.Value, "Added To Warehouse", "Transfered To Holding Area")
.Range("E" & NewRow).Value = Now
End With
'update Ingmaster
With wsIngMaster
m = Application.Match(Val(Me.ComboBox1.Text), .Columns(1), 0)
If Not IsError(m) Then
With .Cells(CLng(m), "C")
If Me.opt_AddStock Then
.Value = .Value + Val(Me.TextBox2.Value)
ElseIf Me.opt_TransferStock Then
.Offset(, 1).Value = Val(Me.TextBox2.Value)
.Value = .Value - Val(Me.TextBox2.Value)
Application.DisplayAlerts = True
End If
End With
End If
End With
Unload Me
frm_Stock.Show
 

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
102
Hi, I have just tried again and now it works not sure why it errored the first time but yet again thank you so much for your help.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,665
Messages
5,488,177
Members
407,630
Latest member
Mehezabin

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top