title: If function to write to either sheet1 or both sheet1 and sheet2
body: <p>I'm trying to write an if function into a save button on a user-form so that if the data entered into the user-form is already on sheet 2 then it only gets written to sheet 1. But if it does not exist on sheet 2 then the data from the user-form gets written to both sheet 1 and sheet 2. This is because I want the data on sheet 2 to act like a sort of database and obviously do not want duplicates. I've made the write procedures into two separate modules (I figured this would make it easier to differentiate). Here is my code (Be gentle I'm still learning)</p>
  
  <pre><code>Sub writetosheet1()
  Dim i As Integer
  
  i = 1
  While ThisWorkbook.Worksheets("Sheet1").Range("A" &amp; i).Value &lt;&gt; ""
  i = i + 1
  Wend
  
  ThisWorkbook.Worksheets("Sheet1").Range("a" &amp; i).Value = UserForm1.txt1.Value
  ThisWorkbook.Worksheets("Sheet1").Range("b" &amp; i).Value = UserForm1.txt2.Value
  ThisWorkbook.Worksheets("Sheet1").Range("c" &amp; i).Value = UserForm1.txt3.Value
  ThisWorkbook.Worksheets("Sheet1").Range("d" &amp; i).Value = UserForm1.txt4.Value
  ThisWorkbook.Worksheets("Sheet1").Range("e" &amp; i).Value = UserForm1.txt5.Value
  End Sub
  
  Sub writetosheet2()
  
  Dim i As Integer
  i = 1
  While ThisWorkbook.Worksheets("Sheet1").Range("A" &amp; i).Value &lt;&gt; ""
  i = i + 1
  Wend
  
  
  ThisWorkbook.Worksheets("Sheet2").Range("a" &amp; i).Value = UserForm1.txt1.Value
  ThisWorkbook.Worksheets("Sheet2").Range("b" &amp; i).Value = UserForm1.txt2.Value
  ThisWorkbook.Worksheets("Sheet2").Range("c" &amp; i).Value = UserForm1.txt4.Value
  ThisWorkbook.Worksheets("Sheet2").Range("d" &amp; i).Value = UserForm1.txt5.Value
  End Sub
  
  Private Sub CMDSAVE_Click()
  Dim id As Long
  id = txt1.Value
  If id &lt;&gt; Sheets("Sheet2").Range("a:a").Value Then
  Call writetosheet1
  Call writetosheet2
  Else
  Call writetosheet1
  
  End If
  End Sub
  </code></pre>
  
  <p>Any help on this would be fantastic! Thanks.</p>