# Build a string

#### Leandroarb

##### Board Regular
Hi
I have a range in which the same code is repeated, but this code has another code called Priority. I need to go through the codes and build a string with the Priorities.

Exampe:

Code Priority String
10.......1........12
10 ......2........12

I try with this For Each:

Code:
``````For Each celP In rngProt
If celP = Protocolo Then
End If
Next``````

But the result is:

Code Priority String
10.......1.........11
10.......2.........22

Last edited:

### Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

#### Rick Rothstein

##### MrExcel MVP
Hi
I have a range in which the same code is repeated, but this code has another code called Priority. I need to go through the codes and build a string with the Priorities.

Exampe:

Code Priority String
10.......1........12
10 ......2........12

I try with this For Each:

Code:
``````For Each celP In rngProt
If celP = Protocolo Then
End If
Next``````

But the result is:

Code Priority String
10.......1.........11
10.......2.........22

I think it might be useful for you to show us all of your code so we can see how you declare your variables (assuming you do so specifically) and what is assigned to each of them leading up to the code snippet you did post.

#### Leandroarb

##### Board Regular
Thanks!
In the moment this all code.
celP and rngProt are variables type Range.
Thanks for the help.

#### Leandroarb

##### Board Regular
I think it might be useful for you to show us all of your code so we can see how you declare your variables (assuming you do so specifically) and what is assigned to each of them leading up to the code snippet you did post.

Good morning!
This is the all code:

Code:
``````Public Function TIPOPROTOCOLO(IntervaloProt As Range, Protocolo As String, Prioridade As Integer, Qtde As Integer, Cod As Long) As String
On Error Resume Next
Dim rngProt As Range
Dim celP As Range
Dim str
Dim strPrio As String

Set rngProt = IntervaloProt
cont = 0

If Qtde = 1 Then
TIPOPROTOCOLO = "Único"
Else
For Each celP In rngProt
If celP = Protocolo Then
End If
Next
TIPOPROTOCOLO = str
End If
End Function``````

#### Leandroarb

##### Board Regular
Hi,
I've come up with a solution.
I know it is not more elegant, but it is. If anyone has a more functional and logical idea, please share.
Thanks!

Code:
``````Global iCol1         As Integer
Global iCol2         As Integer
Global iLin1         As Integer
Global iLin2         As Integer

Public Function TIPOPROTOCOLO(IntervaloProt As Range, Protocolo As String, Prioridade As Range, Qtde As Integer, Cod As Long)
On Error Resume Next
Dim rngProt As Range
Dim celP As Range
Dim cont As Integer
Set rngProt = IntervaloProt
cont = 0
iCol1 = 0
iCol2 = 0
iLin1 = 0
iLin2 = 0
If Qtde = 1 Then
TIPOPROTOCOLO = "Único"
Else
For Each celP In rngProt
If Protocolo = celP Then
cont = cont + 1
If cont = 1 Then
iLin1 = celP.Row
Else
If cont = Qtde Then
iLin2 = celP.Row
End If
End If
End If
Next
End If
TIPOPROTOCOLO = Left(sPrio, Qtde)
End Function

Public Function sPrio() As String
Dim rngS As Range
Dim celS As Range

Set rngS = Range(Cells(iLin1, iCol1), Cells(iLin2, iCol2))
sPrio = ""

For Each celS In rngS
Next celS

End Function``````

Last edited:

Replies
3
Views
105
Replies
14
Views
283
Replies
2
Views
232
Replies
1
Views
314
Replies
1
Views
106

1,136,260
Messages
5,674,681
Members
419,520
Latest member
Jennifer4Dillon

### 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.

### Which adblocker are you using?

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

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