Discussion Locked

Algorithm Correction in VBA Excel

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Pls have a look at my algorithm. I'm trying to merge two columns' data into one and place the result in another column.

Sub Combine_Ports()
Dim CurSheet, PortCode As String
Dim i As Long

CurSheet = ActiveSheet.Name
Worksheets("Port").Activate

For i = 2 To 11
PortCode = Range("A" & i).Value
If Range("D" & i).Value = PortCode Then
Range("B" & i).Value = Range("C" & i).Value & Range("D" & i).Value

Exit For
End If
Next i
Worksheets(CurSheet).Activate
End Sub

Here is what the excel sheet look like:

ColA ColB ColC ColD

SING SG SIN
BRV DE BRV
RTM NL RTM
ANR BE ANR
HAM DE HAM
SOU GB SOU
LEH FR LEH
GNK IT GOA
BCN ES BCN
FOS FR FOS

I made a mistake by referencing to ColumnA. There are exceptions. I don't want to make reference to ColumnA anymore. I just want to combine ColumnC and ColumnD, and place result in Column B.

Pls correct my algorithm. Thank you.

4 replies

by firstpeter

If you remove the conditional check in the following three lines:

> If Range("D" & i).value = PortCode Then
> Exit For
> End If

It should take the values in C & D 2-11 and put the combined result of each row into B. You can even remove the PortCode= statement, since there is no other use for it anymore.

This discussion is closed to new replies.