+ Reply to Thread
Results 1 to 4 of 4

Using For or Do Until loops

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2013
    Location
    Jersey City, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    14

    Using For or Do Until loops

    Created command button running the macro below. I need help with two things:

    1 - How do compare to cells in column 2 until the end of list - which is blank
    2 - how and where to I insert a message box if no matches to the value in F91 is found after going thru the FOR loop - 'MsgBox "Please check AppID and try uploading again"


    Private Sub CommandButton1_Click()
    
        Dim wksFrom As Worksheet
        Dim wksTo As Worksheet
        Dim wksFrom2 As Worksheet
        Dim wkbfrom As Workbook
        Dim wkbto As Workbook
        Dim irow As Long
                    
        Set wkbfrom = Workbooks("VBA testing.xlsm")
        Set wkbto = Workbooks("Import test.xlsx")
        Set wksFrom = wkbfrom.Worksheets("Sheet1")    ' or perhaps "Sheeet1" no space
        Set wksFrom2 = wkbfrom.Worksheets("Sheet2")
        Set wksTo = wkbto.Worksheets("2012 Schedule")
                
        With wksFrom
            For irow = 1 To 100
            'Do Until IsEmpty(Cells(irow, 2))
                If wksFrom.Range("F91") = wksTo.Cells(irow, 2) Then
                    wksTo.Cells(irow, 16).Value = wksFrom2.Range("B3")
                    wksTo.Cells(irow, 17).Value = wksFrom2.Range("B4")
                    wksTo.Cells(irow, 18).Value = wksFrom2.Range("B5")
                    wksTo.Cells(irow, 19).Value = wksFrom2.Range("B6")
                    wksTo.Cells(irow, 21).Value = wksFrom2.Range("B7")
                    Exit For
                'Else
                    'MsgBox "Please check AppID and try uploading again"
                End If
                'irow = irow + 1
            'Loop
            Next irow
        End With
        MsgBox "Update Sent"
        wksFrom2.Range("B10").Value = Date
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Using For or Do Until loops

    Maybe something like this? A bit of warning here, it's a while loop and I haven't tested it.
    How many rows are we talking about? If many then I suspect WorksheetFunction.Match is MUCH faster.

    Option Explicit
        Dim wksFrom As Worksheet
        Dim wksTo As Worksheet
        Dim wksFrom2 As Worksheet
        Dim wkbfrom As Workbook
        Dim wkbto As Workbook
        Dim irow As Long
        Dim Valuefound As Boolean
        
        Set wkbfrom = Workbooks("VBA testing.xlsm")
        Set wkbto = Workbooks("Import test.xlsx")
        Set wksFrom = wkbfrom.Worksheets("Sheet1")    ' or perhaps "Sheeet1" no space
        Set wksFrom2 = wkbfrom.Worksheets("Sheet2")
        Set wksTo = wkbto.Worksheets("2012 Schedule")
                
        Valuefound = False
        irow = 1
        While wksTo.Cells(irow, 2) <> "" And Valuefound = False
            If wksFrom.Range("F91") = wksTo.Cells(irow, 2) Then
                wksTo.Cells(irow, 16).Value = wksFrom2.Range("B3")
                wksTo.Cells(irow, 17).Value = wksFrom2.Range("B4")
                wksTo.Cells(irow, 18).Value = wksFrom2.Range("B5")
                wksTo.Cells(irow, 19).Value = wksFrom2.Range("B6")
                wksTo.Cells(irow, 21).Value = wksFrom2.Range("B7")
                Valuefound = True
            End If
            irow = irow + 1
        Wend
        If Valuefound = False Then
            MsgBox "Please check AppID and try uploading again"
        End If
        
        MsgBox "Update Sent"
        wksFrom2.Range("B10").Value = Date
    End Sub
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

  3. #3
    Registered User
    Join Date
    01-07-2013
    Location
    Jersey City, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Using For or Do Until loops

    Thanks Jacc. I will try it and let you know. To answer your question on how many rows: it actually fluctuates so there is no set number of rows.

  4. #4
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Using For or Do Until loops

    Here's the MATCH version. 100% untested.
    Option Explicit
    Sub test()
        Dim wksFrom As Worksheet
        Dim wksTo As Worksheet
        Dim wksFrom2 As Worksheet
        Dim wkbfrom As Workbook
        Dim wkbto As Workbook
        Dim irow As Long
        Dim s As Variant
                    
        Set wkbfrom = Workbooks("VBA testing.xlsm")
        Set wkbto = Workbooks("Import test.xlsx")
        Set wksFrom = wkbfrom.Worksheets("Sheet1")    ' or perhaps "Sheet1" no space
        Set wksFrom2 = wkbfrom.Worksheets("Sheet2")
        Set wksTo = wkbto.Worksheets("2012 Schedule")
                
        On Error Resume Next
        irow = WorksheetFunction.Match(wksFrom.Range("F91"), wksTo.Range("B1:B10000"), 0)
        If Err = 0 Then
            wksTo.Cells(irow, 16).Value = wksFrom2.Range("B3")
            wksTo.Cells(irow, 17).Value = wksFrom2.Range("B4")
            wksTo.Cells(irow, 18).Value = wksFrom2.Range("B5")
            wksTo.Cells(irow, 19).Value = wksFrom2.Range("B6")
            wksTo.Cells(irow, 21).Value = wksFrom2.Range("B7")
        Else
            MsgBox "Please check AppID and try uploading again"
        End If
        
        MsgBox "Update Sent"
        wksFrom2.Range("B10").Value = Date
        
    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