in Education by
This question already has answers here: Extracting Unique values from a list (2 answers) Closed 7 years ago. Imagining I have the following column: 2008 2008 2009 2010 2009 I want to build a code in VBA that returns first of all, the total of unique values, in this example: 3 (2008, 2009 and 2010), I also want to store these single values in an array (that's what I believe is best). I've tried building a cycle that checks the last cell and compares results, but it's not enough obviously... JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
If your unique data was in column A (say A1 to A5 in your example) then you can use a variant array with a dictionary to extract the uniques The code below creates a variant array X with your 5 values in column A tests each item to see if it exists in a dictionary object objDic if not it is added to the dictionary, and to a second variant array Y the final variant array Y is dumped to B1 extending as far as necessary (this array contains the uniques plus blanks at the end in place of dupes, it can be resized if necessary) (Updated: added test to ignore blanks*) Sub GetUniques() Dim X Dim Y Dim objDic As Object Dim lngRow As Long Dim lngCnt As Long Set objDic = CreateObject("Scripting.Dictionary") X = Range([a1], Cells(Rows.Count, "A").End(xlUp)).Value2 ReDim Y(1 To UBound(X, 1), 1 To 1) For lngRow = 1 To UBound(X, 1) If Len(X(lngRow, 1)) > 0 Then If objDic.exists(X(lngRow, 1)) = False Then lngCnt = lngCnt + 1 Y(lngCnt, 1) = X(lngRow, 1) objDic.Add X(lngRow, 1), 1 End If End If Next lngRow [b1].Resize(UBound(Y, 1), 1) = Y End Sub version 2 Uses Join as per Simple VBA array join not working Sub GetUniques2() Dim X Dim Y Dim objDic As Object Dim lngRow As Long Dim lngCnt As Long Set objDic = CreateObject("Scripting.Dictionary") X = Range([a1], Cells(Rows.Count, "A").End(xlUp)).Value2 ReDim Y(1 To UBound(X, 1)) For lngRow = 1 To UBound(X, 1) If Len(X(lngRow, 1)) > 0 Then If objDic.exists(X(lngRow, 1)) = False Then lngCnt = lngCnt + 1 Y(lngCnt) = X(lngRow, 1) objDic.Add X(lngRow, 1), 1 End If End If Next lngRow ReDim Preserve Y(1 To lngCnt) MsgBox Join(Y, ", ") End Sub

Related questions

0 votes
    I have a table: Using vba I need to add a column after UsedRange, add a header and fill cells ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a table: Using vba I need to add a column after UsedRange, add a header and fill cells ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have encountered a problem during my work. There are over one hundred worksheets in my excel, and I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to adjust the row height based on the cell value. The operation have to run through ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I am trying to export p-values to Excel from the Stata community-contributed command reghdfe: // get data ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have two sheets sheet1, sheet2. Sheet2 having the with records and duplicate number. like sheet1 1--- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I have data like this: id a b c d 1 y y z z 2 y z y y 3 y y y y I want to count the value of "y" ... 1 2 2 1 3 4 Can anyone help me? Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have 10 worksheets. I want to create a table for each. every table has a different amount of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have 10 worksheets. I want to create a table for each. every table has a different amount of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    How do I transfer data from a worksheet to another based on a specific cell value? I tried several ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Below, I have code that sends a personalized SMS message and includes the name. I got that part to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I am trying to do some validation on the file name before it's saved. Here are my three file- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    Below, I have code that sends a personalized SMS message and includes the name. I got that part to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    We have over 10 computers on our network and everyone's computer opens up the file with no issue. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    How do I transfer data from a worksheet to another based on a specific cell value? I tried several ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
...