+ Reply to Thread
Results 1 to 5 of 5

Thread: Unable to get if statement to work

  1. #1
    fialko
    Guest

    Unable to get if statement to work

    Sorry folks i know this is a basic question and i've tried examples from other threads but still struggling

    I have a spreadsheet that takes info from a user populated form and writes to a spreadsheet which works fine. I now need it to write some data to a separate sheet dependant on a combo box as well and assume its an if statement but i cant get it to work

    Below is what i have written which works fine to both sheets without the if. Basically if a user selects sent to chennai in the form i need it to also transfer values to another spreadsheet, any other selection i need it to carry on and clear the form please


    'If Me.cboact = "Sent to Chennai" Then
    'With ws

    ' .Cells(lRow, 2).Value = Me.txtCdl.Value
    ' .Cells(lRow, 3).Value = Me.cboyn.Value
    '.Cells(lRow, 4).Value = Me.txtcom.Value
    'Else

    'clear form
    Me.txtname.Value = ""
    Me.txtdob.Value = ""
    Me.cboyn.Value = ""
    Me.txtrec.Value = ""
    Last edited by fialko; 07-16-2011 at 04:26 AM. Reason: quote coding

  2. #2
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Unable to get if statement to work

    Hi fialko, without seeing all of your procedure, I can only assume that you haven't appropriately ended your If statement or your With statement.
    Sub fialco()
        If Me.cboact = "Sent to Chennai" Then
            With ws
                .Cells(lRow, 2).Value = Me.txtCdl.Value
                .Cells(lRow, 3).Value = Me.cboyn.Value
                .Cells(lRow, 4).Value = Me.txtcom.Value
            End With
        Else
        '''The rest of your 'Else' code here
        End If
    End Sub
    Please leave a message after the beep!

  3. #3
    fialko
    Guest

    Re: Unable to get if statement to work

    Hi and thanks for helping, here is the rest of the code. Sorry to be a pain, not professing to really know what i am doing trying to learn. Here is rest of code to the end. I have tried what you said in your reply but i am obviously doing something wrong as i get compile error when i try to pass the data. Thanks again

    If Me.cboact = "Sent to Chennai" Then
    With ws
     
      .Cells(lRow, 2).Value = Me.txtcdl.Value
      .Cells(lRow, 3).Value = Me.cboyn.Value
      .Cells(lRow, 4).Value = Me.txtcom.Value
      End With
    Else
    
    'clear form
    Me.txtname.Value = ""
    Me.txtdob.Value = ""
    Me.cboyn.Value = ""
    Me.txtrec.Value = ""
    Me.cbovet.Value = ""
    Me.cboCdl.Value = ""
    Me.cboact.Value = ""
    Me.txtcom.Value = ""
    Me.txtcdl.Value = ""
    
    'save the workbook
    ActiveWorkbook.Save
    
    MsgBox "Data has been saved"
    Unload Me
    
    'End If
    
    End Sub

  4. #4
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Unable to get if statement to work

    HI fialco, you are not "being a pain" so no worries ! In the code you provided your 'End if' is commented out. Amended:
    If Me.cboact = "Sent to Chennai" Then
        With ws
        .Cells(lRow, 2).Value = Me.txtcdl.Value
        .Cells(lRow, 3).Value = Me.cboyn.Value
        .Cells(lRow, 4).Value = Me.txtcom.Value
        End With
    Else
        'clear form
        Me.txtname.Value = ""
        Me.txtdob.Value = ""
        Me.cboyn.Value = ""
        Me.txtrec.Value = ""
        Me.cbovet.Value = ""
        Me.cboCdl.Value = ""
        Me.cboact.Value = ""
        Me.txtcom.Value = ""
        Me.txtcdl.Value = ""
        'save the workbook
        ActiveWorkbook.Save
        MsgBox "Data has been saved"
        Unload Me
    End If
    End Sub
    This was no tested
    Please leave a message after the beep!

  5. #5
    fialko
    Guest

    Re: Unable to get if statement to work

    Hey Mordred

    Thank you so much for your help, really appreciated and it works great.

    Fialko

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0