Search column, get value from other column same row, paste into new sheet

rakasab

New Member
Joined
May 4, 2018
Messages
3
Totally new to VBA, some python experience. All sheets are in the same workbook. I'm trying to search through a column in Sheet1 and if a condition is true, get the value from another column on the same row. I then want to check for duplicates. If duplicates send message error. If no duplicates, sort (data is integers) ascending and write the values to consecutive cells in Sheet2. Also, it'd be nice if the upper limit of the range was variable (i.e. I don't know how long the table in Sheet1 will be and I don't know how many unique values will be returned).

Pseudocode something like this (with example data below):

Sub doSomething()

In Sheet1:
for i in Range(A1:A7)
array() = []
if i = 1
get value from column B
array.append(value from column B)
If duplicates in array()
msgBox("Duplicates exist")
Exit Sub
else:
sort array elements ascending

In Sheet2:
write elements from array() consecutively into cells L5:R5

End Sub

Sheet1
AB
16
15
010
015
13
02

<tbody>
</tbody>

Sheet2
LMNOPQR
356

<tbody>
</tbody>
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about
Code:
Sub rakasab()
   Dim Cl As Range
   Dim Lst As Object
   
   Set Lst = CreateObject("System.Collections.ArrayList")
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
         If Cl.Offset(, -1).Value = 1 Then
            If Not .exists(Cl.Value) Then
               .Add Cl.Value, Nothing
               Lst.Add Cl.Value
            Else
               MsgBox "duplicates exist"
               Exit Sub
            End If
         End If
      Next Cl
   End With
   Lst.Sort
   Sheets("New").Range("A1").Resize(, Lst.Count).Value = Lst.toarray
      
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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