+ Reply to Thread
Results 1 to 3 of 3

Combine two codes into one

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-08-2012
    Location
    englang
    MS-Off Ver
    Excel 2010
    Posts
    152

    Combine two codes into one

    Hi guys,

    Can somebody help me to combine the code in red with the code in blue.

    This is what i would like achieve:
    If the textbox "txtQty" is blank exit sub, but if "txtQty" textbox has a value and "sw1" combobox is blank then "sw1.Value = CommandButton5.Caption" but if "txtQty" textbox has a value and "sw1" combobox has a value then sw2.Value = CommandButton5.Caption. But if the combobox "cmbOnline".Value = "Standard" Then execute code in blue.

    I'm so stuck, I would appreciate any help.
    Thanks in advance

    Private Sub CommandButton5_Click()
    If txtQty = "" Then
    Exit Sub
    ElseIf sw1 = "" Then
    sw1.Value = CommandButton5.Caption
    Else: sw2.Value = CommandButton5.Caption
    End If
    
    Otherwise execute code below:
    
    If Trim(Me.cmbOnline.Value) = "Standard" Then
      Dim irow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("DataBase")
    'find first row in database
    irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
    .Range("A" & irow) = cmbDate.Value
    .Range("B" & irow) = txtTime.Value
    .Range("C" & irow) = cmbOperator
    .Range("D" & irow) = cmbLine.Value
    .Range("E" & irow) = txtOrder.Value
    .Range("F" & irow) = txtPart.Value
    .Range("G" & irow) = txtDescription
    .Range("H" & irow) = txtQty.Value
    .Range("I" & irow) = txtUom
    .Range("K" & irow) = txtComments
    .Range("J" & irow) = CommandButton4.Caption
    If sw1 = "" Then
    sw1.Value = CommandButton5.Caption
    Else: sw2.Value = CommandButton5.Caption
    Me.txtOrder.SetFocus
        Me.txtOrder.SelStart = 0
       Me.txtOrder.SelLength = Len(Me.txtOrder.Text)
    End If
    End With
    End Sub
    Last edited by intex; 10-14-2014 at 03:19 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Combine two codes into one

    Since the red code is part of the blue code, just split them, so perhaps:

    Dim irow As Long
    Dim ws As Worksheet
    
    If Trim(Me.cmbOnline.Value) = "Standard" Then
        Set ws = Worksheets("DataBase")
        'find first row in database
        irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        With ws
            .Range("A" & irow) = cmbDate.Value
            .Range("B" & irow) = txtTime.Value
            .Range("C" & irow) = cmbOperator
            .Range("D" & irow) = cmbLine.Value
            .Range("E" & irow) = txtOrder.Value
            .Range("F" & irow) = txtPart.Value
            .Range("G" & irow) = txtDescription
            .Range("H" & irow) = txtQty.Value
            .Range("I" & irow) = txtUom
            .Range("K" & irow) = txtComments
            .Range("J" & irow) = CommandButton4.Caption
        End With
    End If
    
    If sw1 = "" Then
        sw1.Value = CommandButton5.Caption
    Else
        sw2.Value = CommandButton5.Caption
    End If
    
    Me.txtOrder.SetFocus
    Me.txtOrder.SelStart = 0
    Me.txtOrder.SelLength = Len(Me.txtOrder.Text)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    08-08-2012
    Location
    englang
    MS-Off Ver
    Excel 2010
    Posts
    152

    Re: Combine two codes into one

    Thanks a lot it does exactly what i need,perfect!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How do i combine two codes to run together
    By Sam2149 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-25-2014, 05:42 PM
  2. Combine 2 codes
    By Minhaz.iubat in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-06-2014, 05:46 AM
  3. [SOLVED] How to combine these two set's of codes?
    By aadeshgandhi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2013, 07:26 PM
  4. combine codes
    By Oeysbrei in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-30-2012, 03:38 AM
  5. Combine vba codes
    By mmwltd in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-22-2010, 08:32 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1