Excel VBA Extracting Data from Table to an Array

tharrington

New Member
Joined
Jul 19, 2018
Messages
12
Im trying to extract the color index of cells in a table in a spreadsheet into an array using a VBA macro. The indexing I have for my for loop is offset as in the count starts at column 2 and row 4, but the array begins at 0,0 or 1,1? Is there a way to enter this info using such a for loop?

Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
does this do what you want?? Note I didn't know how many columns or rows you wanted so I assume up to row and column 10

Code:
Sub test2()
Dim inarr(1 To 9, 1 To 7) As Variant


For irow = 2 To 10
  For icol = 4 To 10
    inarr(irow - 1, icol - 3) = Cells(irow, icol).Interior.ColorIndex
  Next icol
Next irow
End Sub
 
Last edited:
Upvote 0
Code:
Dim j As Integer
Dim i As Integer
For i = 4 To numrows + 3
    For j = 2 To 12 'columns
    If ActiveSheet.Cells(i, j).Interior.ColorIndex = 6 Then
    Range("M" & i).Value = Range("M" & i).Value + 1
    Cells(i, j).Interior.ColorIndex = cArray(i - 3, j - 1)
    End If
    Next j
    Next i

Here is my code,

That indexing did not work I believe
 
Last edited:
Upvote 0
Code:
Dim j As Integer
Dim i As Integer
For i = 4 To numrows + 3
    For j = 2 To 12 'columns
    If ActiveSheet.Cells(i, j).Interior.ColorIndex = 6 Then
    Range("M" & i).Value = Range("M" & i).Value + 1
    Cells(i, j).Interior.ColorIndex = cArray(i - 3, j - 1)
    End If
    Next j
    Next i

Here is my code,

That indexing did not work I believe

:confused: The code you posted above has nothing to do with the question you asked in Message #1 . In Message #1 , you said you wanted to fill an array with the color index from the cells in part of a table... the code above is filtering only yellow filled cells and changing that yellow color to a value stored in an array named cArray. What exactly do you want?
 
Upvote 0
Apologies for that. I have existing code that counts the number of yellow cells. I figured that I would add in this array part in the same for loop to save code. I want a final count of yellows, and an array of the color index. Is that possible?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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