Solved

Reducing an array

Posted on 2011-03-12
6
246 Views
Last Modified: 2012-05-11
I have an array of data read off a worksheet using the statement

maxrds = dta.Range("a" & xls.Application.Rows.Count).End(xlUp).Row - datstart + 1

All variables are variants.

How can I create a similar array which has only even numbers in the first column?

Saqib
0
Comment
Question by:Saqib Husain, Syed
  • 3
  • 3
6 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 35115494
Saqib,

I think you will need to create a second array reading in every second record from your initial array (faster than writing every second cell to a single array)

Cheers

Dave
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 35115524
Hi, Dave,

I have not declared the size of the original array. It sizes it based on the assignment statement above. I would like to know a way to assign this to the new array without defining the length. So the main list might have 100 entries and the new list might have 70

The numbers would have to be checked for evenness, they are not regularly incrementing.
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 35115662
Sorry I had given the wrong assignment statement in the question. That was for calculation the number of entries in the range. The range assignment is given here

rds = WorksheetFunction.Transpose(dta.Range(dta.Cells(datstart, 1), dta.Cells(maxrds + datstart - 1, 1)).Value)

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 50

Expert Comment

by:Dave Brett
ID: 35115921
Can you pls post a sample Saqib
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 35122777
Dave,

I have created this file with the given numbers in column A.

In column D I have shown what values I would like to see in the reduced array. This column is not normally present.

I have also created a macro which assigns column A values to rds and column D values to rds2.

I would like to have rds contain the contents of rds2 instead of having a new variable rds2.

Saqib
Reducearray.xls
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 35124561

Saqib,

This code uses a Mod test to dump only the even numbers to the second array

A Redim Preserve is used to trim the second array to the correct size

For future reference my array article is at  http://www.experts-exchange.com/A_2684.html

Cheers

Dave
Sub Recut()
    Dim X
    Dim Y
    Dim lngRow As Long
    Dim lngRow2 As Long
    X = Application.Transpose(Range([a5], Cells(Rows.Count, "A").End(xlUp)))
    Y = X
    For lngRow = 1 To UBound(X)
        If X(lngRow) Mod 2 = 0 Then
            lngRow2 = lngRow2 + 1
            Y(lngRow2) = X(lngRow)
        End If
    Next
    ReDim Preserve Y(lngRow2)
End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now