VBA

Moderators: PavlinII
Number of threads: 1673
Number of posts: 3078

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Macro to read list and fill binary matrix Posted by chrismblinn on 22 Mar 2011 at 8:37 AM
Hello,

I am trying to tackle an idea I had for a macro. I'm very new to macros and have only created a few of them.

I have a list of dates from a 60 year time span. I have created a table, columns being years, rows being months and days. I want to read the list of dates I have then find that date in the table. Example, the list date is 1/3/1950; I need to find the 1/3 row and 1950 column and put a value of 1 in the cell. For all other dates not in the list I need values of 0.

My idea is to have the macro loop through the list. Each date it will read from the left to the second "/", and someone find the row corresponding to that date. Then it will read the first four digits from the right, and find the corresponding column. Where these two intersect, a value of 1 will be placed.

Is this possible, or am I getting way over my head?
Report
Re: Macro to read list and fill binary matrix Posted by chrismblinn on 22 Mar 2011 at 10:40 AM
I have formatted the list of dates into 60 columns, each column corresponding to each year in the time span. These columns range from B to BI. There are a different number of dates for each year, ranging from 1 to 22 records. These records are listed in rows 370 to 392. The rows for the table I need to fill in with 0 and 1 values are indexed in Column BK from rows 2 to 366. Columns BI to DS are delegated for each year in the 60 year time span.

Report
Re: Macro to read list and fill binary matrix Posted by chrismblinn on 23 Mar 2011 at 12:10 PM
Dim obCurCell As Object, obCurCell2 As Object
Dim loX As Long

For Each obCurCell In Range("D371:D372")
    For Each obCurCell2 In Range("C3:C367")
        If obCurCell.Value = obCurCell2.Value Then
            loX = obCurCell2.Row
            Range("D" & loX) = 1
        End If
    Next
Next

End Sub


This code works for the first column. I need my obCurCell to loop from Column D to Column BK, and I need my obCurCell Range to loop from row 371 to 392.
Report
Re: Macro to read list and fill binary matrix Posted by DaiMitnick on 24 Mar 2011 at 8:33 AM
Hi Chris, I think you may find it easier to simply use countif formulae and use the macro to apply the formula and copy-paste the values. However, as you already have done some work, you could just edit that sub, all you really need is to move away from using column letters and start using cell references. For instance the following code:

For Each obCurCell In Range("D371:D372")
    For Each obCurCell2 In Range("C3:C367")
        If obCurCell.Value = obCurCell2.Value Then
            loX = obCurCell2.Row
            Range("D" & loX) = 1
        End If
    Next
Next


Could be changed to:

Dim i As Integer, j As Integer
For i = 371 To 372
    For j = 3 To 367
        If Cells(i, 4).Value = Cells(j, 3).Value Then
            Cells(j, 4).Value = 1
        End If
    Next
Next


Then it's a simple case of changing those hard-coded numbers to variables too, i.e. instead of that 4 there, change it to a variable called k (or whatever you like) and put in another for loop of "for k = 4 to 63" (columns D to BK). HTH, if you have any questions, please ask. Regards, Dai


------------------------------------------
Do or do not, there is no try. |
------------------------------------------



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.