Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Function to loop through a column and return a value based on an x

See the below code. What we want to do is instead of using VBA to use
formula (Join & Trim) if possible. Don't want to have an input box
just named ranges, loop through named range "A" for and cell
containing x and if x then place results from named range "B" into on
cell Concatenating and commas separating each result.

Anybody help? Thanks!

Sub JoinCells()
Dim rCells As Range
Dim rRange As Range
Dim rStart As Range
Dim strStart As String
Dim iReply As Integer
On Error Resume Next

'Change Below to a named range instead of an input box

Set rCells = Application.InputBox _
(Prompt:="Select the cells to join," _
& "use Ctrl for non-contiguous cells.", _
Title:="CONCATENATION OF CELLS", Type:=8)

If rCells Is Nothing Then 'Cancelled or mistake
iReply = MsgBox("Invalid selection!", _
vbQuestion + vbRetryCancel)
If iReply = vbCancel Then
On Error GoTo 0
Exit Sub
Else
Run "JoinCells" 'Try again
End If
End If

'Set range variable to first cell
Set rStart = rCells(1, 1)

'Loop through cells chosen
For Each rRange In rCells
strStart = rRange 'parse cell content to a String
rRange.Clear 'Clear contents of cell
'Replace the original contents of first cell with "", then _
join the text Need to put results in a specific cell address
rStart = Trim(Replace(rStart, rStart, "") & " " _
& rStart & " " & strStart)
Next rRange
On Error GoTo 0

End Sub

Debbie Worst
Small Applications Team
513.345.6462

-----Original Message-----
From: Vessey, David
Sent: Thursday, July 08, 2004 8:27 AM
To: Worst, Debbie
Subject: Code for Looping

Sub JoinCells()
Dim rCells As Range
Dim rRange As Range
Dim rStart As Range
Dim strStart As String
Dim iReply As Integer
On Error Resume Next

'Allow user to nominate cells to join Change Below to a named rang
instead of an input box
Set rCells = Application.InputBox _
(Prompt:="Select the cells to join," _
& "use Ctrl for non-contiguous cells.", _
Title:="CONCATENATION OF CELLS", Type:=8)

If rCells Is Nothing Then 'Cancelled or mistake
iReply = MsgBox("Invalid selection!", _
vbQuestion + vbRetryCancel)
If iReply = vbCancel Then
On Error GoTo 0
Exit Sub
Else
Run "JoinCells" 'Try again
End If
End If

'Set range variable to first cell
Set rStart = rCells(1, 1)

'Loop through cells chosen
For Each rRange In rCells
strStart = rRange 'parse cell content to a String
rRange.Clear 'Clear contents of cell
'Replace the original contents of first cell with "", then _
join the text Need to put results in a specific cell address
rStart = Trim(Replace(rStart, rStart, "") & " " _
& rStart & " " & strStart)
Next rRange
On Error GoTo 0

End Su

--
Message posted from http://www.ExcelForum.com

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return column header based on last value in row C. Excel Worksheet Functions 3 April 12th 10 08:53 AM
Return Column Heading based on value in row Lee New Users to Excel 2 May 21st 09 06:06 PM
Return sum of values based on lookup-function Braute Excel Worksheet Functions 1 February 25th 09 09:21 AM
Return value in 3rd column based on 2 other columns Joe M. Excel Discussion (Misc queries) 2 May 29th 08 04:52 PM
Return Range of Numerical Values in Single Column based on Frequency Percentage Sam via OfficeKB.com Excel Worksheet Functions 9 October 28th 05 11:01 PM


All times are GMT +1. The time now is 06:46 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"