Do Until condition met

*shudder*

Well-known Member
Joined
Aug 20, 2009
Messages
510
Office Version
  1. 2016
Platform
  1. Windows
Suffering from a hang over and having a mental block.

I want to loop through column D until the word 'computer' is found, apply some code (I have this bit) then exit the loop.

Thanks in advance
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Why not use the Find method

Code:
Dim Found As Range
Set Found = Columns("D").Find(what:="computer", LookIn:=xlValues, lookat:=xlWhole)
 
Upvote 0
Thanks Peter.

How would I then use that to apply additional code?

I am using this in an email macro and dependant on the cell content in column D I need to add an attachment relating to that product.

Cheers
 
Upvote 0
Ah, maybe you are looking for a cell that contains the word computer with other text. In which case

Rich (BB code):
Dim Found As Range
Set Found = Columns("D").Find(what:="computer", LookIn:=xlValues, lookat:=xlPart)
If Found.Value = "computer shopper" Then
 
Upvote 0
Thanks again Peter that give me what I want, although I do not need the 'xlpart' element as the cells only contain the whole search subject.

Is there any way of ignoring case?
 
Upvote 0
I think it should be case-insensitive by default. Just to be sure

Code:
Dim Found As Range
Set Found = Columns("D").Find(what:="computer", LookIn:=xlValues, lookat:=xlPart, MatchCase:=False)
If Found.Value = "computer shopper" Then
 
Upvote 0
Thanks again, I did test case sensitivity prior to posting and your suggestion has corrected it.

My problem now is that when I incorporate this into the rest of my code it adds the attachment even if the word 'computer' is not present. This is an extract of my routine:

Code:
With OutMail
    .To = mailAddress
    .Subject = mailSubject
    .Attachments.Add NewWB.FullName
    Set Found = NewWB.Sheet1.Columns("D").Find(what:="Computer", LookIn:=xlValues, MatchCase:=False)
    If Found.Value = "Computer" Then
        .Attachments.Add ("XXX")
    End If
    .HTMLBody = "<font face='Arial' style='font-size:10pt'>" & mailSalutation & _
                "," & "<br><br>" & strbody & "<br>" & Signature
    If Range("A2").Value <> "" Then .Display
        Application.Wait (Now + TimeValue("0:00:02"))
        Application.SendKeys "%s"
End With
 
Upvote 0
I'm surprised that doesn't crash if the word isn't found.

Try like this

Rich (BB code):
With OutMail
    .To = mailAddress
    .Subject = mailSubject
    .Attachments.Add NewWB.FullName
    Set Found = NewWB.Sheet1.Columns("D").Find(what:="Computer", LookIn:=xlValues, MatchCase:=False)
    If Found Is Nothing Then Exit Sub
    If Found.Value = "Computer" Then
        .Attachments.Add ("XXX")
    End If
    .HTMLBody = "" & mailSalutation & _
                "," & "" & strbody & "" & Signature
    If Range("A2").Value <> "" Then .Display
        Application.Wait (Now + TimeValue("0:00:02"))
        Application.SendKeys "%s"
End With
 
Upvote 0
Sorry Peter, I cannot use that as I am sending large lists of data, its only when the 'computer' value is there that I need to add the attachment, if it is not I still need the email to send.
 
Upvote 0
Try

Code:
With OutMail
    .To = mailAddress
    .Subject = mailSubject
    .Attachments.Add NewWB.FullName
    Set Found = NewWB.Sheet1.Columns("D").Find(what:="Computer", LookIn:=xlValues, MatchCase:=False)
    If Not Found Is Nothing Then
        If Found.Value = "Computer" Then
            .Attachments.Add ("XXX")
        End If
    End If
    .HTMLBody = "" & mailSalutation & _
                "," & "" & strbody & "" & Signature
    If Range("A2").Value <> "" Then .Display
        Application.Wait (Now + TimeValue("0:00:02"))
        Application.SendKeys "%s"
End With
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top