Solved

If, left function, loop, replace

Posted on 2012-03-26
4
229 Views
Last Modified: 2012-03-26
Hi,

I need a piece of code to count rows "A" then do do the following


IF Left(D2,2) = "BR" then W2 = "Paris" else W2 = "New York"

And loop till the last row.

Can anyone put that in structure for me!

Thanks
Seamus
0
Comment
Question by:Seamus2626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 42

Accepted Solution

by:
dlmille earned 250 total points
ID: 37766616
When you say "count rows A" do you mean for each row having data in Column A do this task?  That's what this code does:

Sub doTheWork()
Dim wks As Worksheet
Dim rng As Range
Dim r As Range

    Set wks = ActiveSheet
    
    Set rng = wks.Range("A1", wks.Range("A" & wks.Rows.Count).End(xlUp))
    
    For Each r In rng
        If Left(wks.Range("D" & r.Row).Value, 2) = "BR" Then
            wks.Range("W" & r.Row).Value = "Paris"
        Else
            wks.Range("W" & r.Row).Value = "New York"
        End If
    Next r
    
End Sub

Open in new window


Cheers,

Dave
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 37766620
What you are asking is a little confusing because it seems like you want to loop through the rows but your example uses cell D2. Is this waht you want


Dim r As Range
Dim i As Long

Set r = Range("A1").End(xlDown).Offset(0, 0)
For i = 1 To r.Row

    If Range("D" & 1).Value = "BR" Then
        Range("W" & i).Value = "Paris"
    Else
        Range("W" & 1).Value = "New York"
    End If

Next
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 250 total points
ID: 37766648
Or:
Sub testing()
    Dim lngLastrow As Long
    lngLastrow = Cells(Rows.Count, "A").End(xlUp).Row
    Range("W2:W" & lngLastrow) = Evaluate("IF(LEFT(A2:A" & lngLastrow & ",2)=""BR"",""Paris"", ""New York"")")
End Sub

Open in new window

0
 

Author Closing Comment

by:Seamus2626
ID: 37766856
Hey Martin, that solution didnt work for me.

Thanks guys

Seamus
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question