adjacent cell values added to string

mzza

Board Regular
Joined
Nov 8, 2006
Messages
55
Hi all,

been away from VBA coding for a VERY long time, so I'm sure there's something very obvious that I'm missing here - probably LOTS of things. Here's what I'm trying to do:

sheet "usage" has 8 columns. Column B has a list of countries, which may grow and shrink over time. Column E shows an "x" by those countries which the user has selected. I want the code to go through the entire of Column E looking for "x"s - where it finds an "x", I need it to build a text string based on the contents of columns B and H for the rows with "x"s in.

Here was my first stab, which isn't behaving at all but will hopefully help clarify...

Code:
Sub Countries()

Dim iString As String
Dim c As Range

For Each c In Range("E2", Range("E" & Rows.Count).End(xlUp))
    If c.Value = "x" Then
        iString = c.Offset(-3, 0).Value & " - " & c.Offset(2, 0).Value
    End If
Next c

MsgBox (iString)

End Sub

... the MsgBox is only there for testing purposes to demonstrate the contents of the string.

Any advice would be much appreciated!

TIA

mzza
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Something like this perhaps...

Code:
Sub Countries()
Dim Last_Row As Long
Dim i As Long
Dim iString As String

With Sheets("Usage")
    Last_Row = .Range("E" & Rows.Count).End(xlUp).Row
    
    For i = 2 To Last_Row
        If .Cells(i, 5) = "x" Then iString = iString & "," & Cells(i, 2).Value & "-" & Cells(i, 8).Value
    Next i
End With
    
    MsgBox iString
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,544
Members
452,925
Latest member
duyvmex

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