+ Reply to Thread
Results 1 to 4 of 4

if then else statement not working

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-01-2012
    Location
    england
    MS-Off Ver
    Excel 2016 pro
    Posts
    772

    Post if then else statement not working

    hi


    i have done this statement it copies a range to 3 sheets ,i have put a if then else statement , if yes is on row q3 then only copy rows = to yes
    but now the first statement will not work now,could someone give me some guidance please,have read and downloaded lots of samples,just stuck now
    cheers colin











    Private Sub CommandButton1_Click()
    Dim x As Long, NR As Long, CR As Long, SC As Long
        Dim c As Range
    For Each c In Worksheets("mfr_list").Range("Q3:Q100")
        If c.Value = "Yes" Then
        
    NR = 46
    CR = 3
    SC = 3
      For x = 3 To Sheets("MFR_List").UsedRange.Rows.Count
     If Application.CountBlank(Range(Cells(x, 1), Cells(x, 6))) = 0 Then
     Range(Cells(x, 1), Cells(x, 6)).Copy Sheets("Completed").Cells(CR, "A")
     Range(Cells(x, 1), Cells(x, 6)).Copy Sheets("invoice").Cells(NR, "A")
     Range(Cells(x, 1), Cells(x, 6)).Copy Sheets("stored contracts").Cells(SC, "A")
     Range(Cells(x, 1), Cells(x, 6)).Copy Sheets("Completed").Cells(CR, "A")
    NR = NR + 1
    CR = CR + 1
    SC = SC + 1
    
    
    Else: MsgBox "no copy"
     
    
     End If
     
     Next x
     End If
     Next c
     End Sub
    Last edited by cfinch100; 02-22-2013 at 05:46 PM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: if then else statement not working

    Hi, colin,

    maybe just set a With-Statement with the sheet name and refer to that? If nothing is indicated ranges refer to the active sheet.

    Private Sub CommandButton1_Click()
    Dim x As Long, NR As Long, CR As Long, SC As Long
    Dim c As Range
    With Sheets("mfr_list")
      For Each c In .Range("Q3:Q100")
        If c.Value = "Yes" Then
          NR = 46
          CR = 3
          SC = 3
          For x = 3 To .UsedRange.Rows.Count
            If Application.CountBlank(.Range(.Cells(x, 1), .Cells(x, 6))) = 0 Then
              .Range(.Cells(x, 1), .Cells(x, 6)).Copy Sheets("Completed").Cells(CR, "A")
              .Range(.Cells(x, 1), .Cells(x, 6)).Copy Sheets("invoice").Cells(NR, "A")
              .Range(.Cells(x, 1), .Cells(x, 6)).Copy Sheets("stored contracts").Cells(SC, "A")
              .Range(.Cells(x, 1), .Cells(x, 6)).Copy Sheets("Completed").Cells(CR, "A")
              NR = NR + 1
              CR = CR + 1
              SC = SC + 1
            Else
              MsgBox "no copy"
            End If
          Next x
        End If
      Next c
    End With
    End Sub
    Why not apply an Autofilter to narrow down the number of cells to be checked?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    10-01-2012
    Location
    england
    MS-Off Ver
    Excel 2016 pro
    Posts
    772

    Re: if then else statement not working

    hi
    thanks for the code but it did not do anything im afraid, have you any other ideas please
    cheers colin

  4. #4
    Forum Contributor
    Join Date
    10-01-2012
    Location
    england
    MS-Off Ver
    Excel 2016 pro
    Posts
    772

    Re: if then else statement not working

    hi
    have just found this code works well, just screen flickers alot is there any way to stop this
    cheers colin

    Dim r As Long, endRow As Long, pasteRowIndex As Long
    
    endRow = 100 ' of course it's best to retrieve the last used row number via a function
    pasteRowIndex = 1
    
    For r = 1 To endRow 'Loop through sheet1 and search for your criteria
    
        If Cells(r, Columns("q").Column).Value = "yes" Then 'Found
    
                'Copy the current row
                Rows(r).Select
                Selection.Copy
    
                'Switch to the sheet where you want to paste it & paste
                Sheets("completed").Select
                Rows(pasteRowIndex).Select
                ActiveSheet.Paste
    
                'Next time you find a match, it will be pasted in a new row
                pasteRowIndex = pasteRowIndex + 1
    
    
               'Switch back to your table & continue to search for your criteria
                Sheets("mfr_list").Select
        End If
    Next r
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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