Map unique values to a specific Excel sheet - excel

I'm wondering if its possible to create a VBA that map a random "numerical codes" from an excel Spreadsheet 2 (let's say column A) to a column B (Spreadsheet 1).
Some of the values on the spreadsheet 2 are repeated, I would like to build a unique correspondence (no repeated values from column A / Spreadsheet 2 to my column B / Spreadsheet 1)
Spreadsheet1:
Spreadsheet2
Desired output, column filled from Spreadsheet2 (Unique)values :
Is this possible?? feasible??

The following VBA code uses for loops to iterate through the list of values in Spreadsheet2 and only copy each value to Spreadsheet1 if the value has not occurred already in the list.
Option Explicit
Sub ListUniqueCodes()
Dim code As Long
Dim codes As Range
Dim i As Integer
Dim j As Integer
Dim last_row As Integer
Dim output_cell As Range
Dim unique_codes As New Collection 'You could also use a Scripting.Dictionary here as suggested by JvdV
'See https://stackoverflow.com/questions/18799590/avoid-duplicate-values-in-collection
'Store the length of the list of codes that is in Spreadsheet2:
last_row = Workbooks("Spreadsheet2.xlsx").Sheets("Sheet1").Range("A1").End(xlDown).Row
'Store the list of codes that is in Spreadsheet2:
Set codes = Workbooks("Spreadsheet2.xlsx").Sheets("Sheet1").Range("A1:A" & last_row)
'For each code...
For i = 1 To codes.Rows.Count
code = codes.Cells(i).Value2
'...if it does not equal zero...
If code <> 0 Then
'...and if it is not already in the collection unique_codes...
For j = 1 To unique_codes.Count
If unique_codes(j) = code Then Exit For
Next j
'...then add it to the collection unique_codes:
If j = (unique_codes.Count + 1) Then
unique_codes.Add code
End If
End If
Next i
Set output_cell = Workbooks("Spreadsheet1.xlsm").Sheets("Sheet1").Range("B2")
'Write out the unique codes in Spreadsheet1:
For i = 1 To unique_codes.Count
output_cell.Offset(i - 1, 0).Value2 = unique_codes(i)
Next i
End Sub

Related

How to count the duplicate value in output in new worksheet using VBA Module

I need to count the duplicate value in new worksheet from the source worksheet "Rawdata" using VBA module. The Number of times the same values are repeated in particular column in front of ID in different sheet.
I have done by Pivot Table but I would like to do this by VBA module on button click event.
Original Source Page
Expected Output worksheet as below :
I have tried with the below code by updating the correct sheet name but I am getting the type mismatch error as below :
I am using the below code but I have manually copy the whole column data from position "Y" (25) to number One ("A") by inserting new column.
modified worksheet
Option Explicit
Sub CountDuplicates()
Dim a As Variant, b As Variant, e As Variant
Dim d As Object
Dim i As Long
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = 1
b = Sheets("Rawdata").Range("A3").CurrentRegion.Resize(, 1).Value
ReDim Preserve b(1 To UBound(b), 1 To 2)
a = Sheets("Rawdata").UsedRange.Value
For Each e In a
If Len(e) > 0 Then
d(e) = d(e) + 1
End If
Next e
For i = 1 To UBound(b)
b(i, 2) = d(b(i, 1))
Next i
Sheets("DuplicateCount").Range("A1:B1").Resize(UBound(b)).Value = b
End Sub
**Now my Output result worksheet have repeat count (not distinct). I mean ID column is not unique. The numeric value have duplicated with count in result in spite of change the format to text in source column **
This is how it accomplished. Change the sheet names according to yours.
Option Explicit
Sub CountDuplicates()
Dim a As Variant, b As Variant, e As Variant
Dim d As Object
Dim i As Long
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = 1
b = Sheets("Rawdata").Range("Y3:Y10000").Value
ReDim Preserve b(1 To UBound(b), 1 To 2)
a = Sheets("Rawdata").UsedRange.Value
For Each e In a
If Len(e) > 0 Then
d(e) = d(e) + 1
End If
Next e
For i = 1 To UBound(b)
b(i, 2) = d(b(i, 1))
Next i
Sheets("DuplicateCount").Range("A1:B1").Resize(UBound(b)).Value = b
End Sub

Loop through name list and if names exist in selection start after last name

I apologize, this is my first crack at Excel VBA so excuse my lack of knowledge!
So I have a list of (currently) 3 names to assign to the days in column A in a repeating order in Excel.
Currently my VBA code allows it to populate the selected cells with the names in a repeating pattern (this part is good), however there are two pieces I need help with.
1- with current code, once it reaches the bottom of the names it checks for the blank box that would end that list and starts over at the tops as directed but it puts a blank cell first (see screenshot). How can I have it put next name without adding blank cell first?
2- I want to be able to (once this gets going)select the entire D column through what dates need to be filled and:
-check the lowest non blank box
-match to list and set the
counter to name below that so
it continues the name order
from the last person who was
assigned
This is code I have now:
Sub EXAMPLE()
Dim count As Integer
count = 0
For Each c In Selection
c.Value = Range("X1").Offset(count, 0).Value
If c.Value = "" Then count = -1 And c.Value = Range("x1").Offset(count, 0).Value
count = count + 1
Next c
End Sub
Sorry I know that was long, I hope this makes sense.
I think it's worth reading about arrays, as this task is ideally suited to their use. Your best bet would be to read the names into an array and then build a recurring array whose dimension is equal to the number of rows in your dates column (or selection, or however you want to define the size of the output range).
Code would look a little like this:
Dim v As Variant
Dim people() As Variant, output() As Variant
Dim rowCount As Long, i As Long, j As Long
Dim endRange As Range
'Read the list of names into an array.
'This just takes all data in column "X" -> amend as desired
With Sheet1
Set endRange = .Cells(.Rows.Count, "X").End(xlUp)
v = .Range(.Cells(1, "X"), endRange).Value
End With
'Sense check on the names data.
If IsEmpty(v) Then
MsgBox "No names in Column ""X"""
Exit Sub
End If
If Not IsArray(v) Then
ReDim people(1 To 1, 1 To 1)
people(1, 1) = v
Else
people = v
End If
'Acquire the number of rows for repeating list of names.
'This just takes all data in column "A" -> amend as desired
With Sheet1
Set endRange = .Cells(.Rows.Count, "A").End(xlUp)
rowCount = .Range(.Cells(3, "A"), endRange).Rows.Count
End With
'Sense check date data.
If endRange.Row < 3 Then
MsgBox "No dates in Column ""A"""
Exit Sub
End If
'Make a recurring array.
ReDim output(1 To rowCount, 1 To 1)
i = 1
Do While i <= rowCount
For j = 1 To UBound(people, 1)
output(i, 1) = people(j, 1)
i = i + 1
If i > rowCount Then Exit Do
Next
Loop
'Write the output to column "D"
Sheet1.Range("D3").Resize(UBound(output, 1)).Value = output

Macros to sum unique values related to another column

I need to create macros to populate in "Metrics calculator" tab, the sum of "No of test steps"(Column G) for each "study" (Column D). The sum should consider unique values only. Please see the table below:
In this Study no '1111', total step no/patient no '20' repeated 3 times, '15' repeated 4 times and '30' 3 times- i need the macros just to take unique values and add,i.e just 20+15+30=65.
In Metrics calculator tab"- It should come with output as
Study no Total no of steps
1111 65
So if I understand the question, you want to sum the unique values in column B. The following will do the trick.
Note, you will need to adgust the range as necessary. Right now the range is set to B2:B20 as in your sample table.
Sub unique()
Dim arr() As Variant
Dim arrElem As Variant
Dim Rng As Range
Dim elem As Range
Dim i As Long
Dim stepSum As Long
Dim match As Boolean
Set Rng = Range("B2:B20") 'Edit this so that it fits your array
'this sets up an array to store unique variables
ReDim Preserve arr(1 To 1) As Variant
arr(1) = 0
'this loops through each number in the identified range
For Each elem In Rng
'this is a boolean, false means the current number is unique (so far) true means the number has been seen previously.
match = False
'this checks the current number against all the unique values stored in the array
For Each arrElem In arr
'If it finds a match, it changes the boolean to true
If arrElem = elem Then match = True
Next arrElem
'If not match was found, we store the current number as a new unique value in the array
If match = False Then
'this adds another row to the array
ReDim Preserve arr(1 To UBound(arr) + 1) As Variant
'this adds the unique value
arr(UBound(arr)) = elem.Value
End If
Next elem
'this sums the unique numbers we stored in the array
For i = 1 To UBound(arr)
stepSum = stepSum + arr(i)
Next i
'this reports the sum of unique elements
MsgBox stepSum
End Sub
Review this example :
In our sample ,cells(vendor names) in "column B" are listed as unique values into "column K" :
Columns("B:B").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Columns( _
"B:B"), CopyToRange:=Range("'Sheet1'!K1"), Unique:=True
Later , to sum these items(amounts in column I) in column "L" ,we entered SUMIF formula to cells in column L using a loop :
For i = 2 To Cells(Rows.Count, 11).End(xlUp).Row
Cells(i, "L").FormulaR1C1 = "=SUMIF(C[-10],RC[-1],C[-3])"
Next i
Source : Excel vba sum unique items

Loop through sheets and add values to Dropdown list - VBA

Goal
I would like to loop through five sheets and populate a dropdown list in a sixth sheet based on some requirements.
Problem
I don't know how to populate the dropdown list, make them dynamic and loop through the five sheets.
Description
In the sixth sheet (Dropdown Sheet), column E contains names which I would like to compare to the first row (E1:GG1) in the five sheets. Each column has a bunch of 1's.
If there is a match between a name in column E (Dropdown Sheet) and a name in a row (one of the five sheets) AND there's a '1' in the column for that row, the dropdown list should be populated with the ID in column A.
Desired output
Code
Private Sub ValuesInDropdownList()
Dim TeamSource As Range, PersonSource As Range, r As Range, csString As String
Dim PersonCell As Range, TeamCell As Range
Dim Dropdown As Collection
Dim i As Integer
Dim lastRow As Integer
Set TeamSource = Sheets("Dropdown Sheet").Range("E10:E100")
Set PersonSource = Sheets("Sheet1").Range("E1:GG1")
Set Dropdown = New Collection
On Error Resume Next
For Each PersonCell In PersonSource
v = PersonCell.Value
Debug.Print (v)
With PersonSource
lastRow = .Cells(.Rows.Count, PersonCell.Columns.Count).End(xlUp).Row
Debug.Print (lastRow)
End With
If v <> "" Then
For Each TeamCell In TeamSource
Debug.Print (TeamCell)
If PersonCell = TeamCell Then
intValueToFind = 1
For i = 1 To lastRow
If PersonCell.Offset(i, 0) = intValueToFind Then
Debug.Print (PersonCell.Offset(i, -4))
Dropdown.Add v, CStr(v)
End If
Next i
End If
Next TeamCell
End If
Next PersonCell
End Sub

Excel VBA script to populate another sheet conditionally

I would like to write a VBA macro for excel through which i want data from a master sheet to populated to another sheets conditionally.
for example, my master sheet ("Sheet1) has multiple rows and column. The condition for data population from Sheet1 to Sheet2 should be based on these condition
(1) Only rows which has a particular string in a column (say "keyword" string in column D)
(2) Only few columns to be copied from Sheet1 to Sheet2 (say column A,B,E & G)
I have a code that copies a column when the heading of the column is a certain string, would that help?
Edit1:
Here is what I have come up with. The code should be flexible enough to adapt to any type of spreadsheet you've got
Dim keyColumn As Integer
Dim i As Integer
Dim keyWord As Variant 'I've used variant, so you can choose your own data type for the keyword
Dim dataSh As String 'I'm using sheet names for sheet referencing
Dim populateSh As String
Dim rowNum As Integer
Dim dataRow() As Variant
Sub Populate()
'set the column number, which contains the keywords, the keyword itself,
'name of the sheet to populate and the row offset you'd like to start populating
populateSh = "populate"
keyColumn = 4
keyWord = "yes"
rowNum = 0
'assuming you run the macro in the sheet you get the data from, get its name to return to it after copying the row
dataSh = ActiveSheet.Name
'loop through all the used cells in the column
For i = 1 To ActiveSheet.UsedRange.Rows.Count
If Cells(i, keyColumn) = keyWord Then
'starting in row 1 in the sheet you populate, you'll have to set the rowNum variable to desired offset few lines above
rowNum = rowNum + 1
Call copyRow(i, rowNum)
End If
Next i
End Sub
Sub copyRow(ByVal cRow As Integer, ByVal pRow As Integer)
Dim colNum As Integer
'set the number of columns you'd like to copy
colNum = 3
'redimension the array to carry the data to other sheet
'this can be done any way you,d like, but I'm using array for flexibility
ReDim dataRow(1 To colNum)
'put the data into the array, as an example I'm using columns 1, 2 and 3, while skipping the keyword column.
dataRow(1) = Cells(cRow, 1)
dataRow(2) = Cells(cRow, 2)
dataRow(3) = Cells(cRow, 3)
Sheets(populateSh).Select
For p = 1 To UBound(dataRow)
Cells(pRow, p) = dataRow(p)
Next p
Sheets(dataSh).Select
End Sub
Hope that helps. Sorry for any style errors in advance

Resources