1

I have a macro that allows me to open multiple files based on their names and copy sheets based on a criteria (if there's a value in column "X" then copy the row but only some colums "F,G,P,Q,W,X,Y) to another unique workbook. the problem is in column F i have a color and i want to retrieve the color index but the macro leaves it blank

 [1] Get data from A1:Z{n}

  n = ws.Range("A" & Rows.Count).End(xlUp).Row   ' find last row number n
  v = ws.Range("A10:Y" & n).Value2 ' get data cols A:Y and omit header row

 [2] build array containing found rows

  a = buildAr2(v, 24) ' search in column X = 24

' [3a] Row Filter based on criteria

  v = Application.Transpose(Application.Index(v, _
  a, _
Application.Evaluate("row(1:" & 26 & ")"))) ' all columns from A to Z

[3b] Column Filter F,G,P,Q,W,X,Y

  v = Application.Transpose(Application.Transpose(Application.Index(v, _
      Application.Evaluate("row(1:" & UBound(a) - LBound(a) + 1 & ")"), _
      Array(6, 7, 16, 17, 23, 24, 25))))          ' only cols F,G,P,Q,W,X,Y

Function buildAr2(v, ByVal vColumn&, Optional criteria) As Variant
' Purpose: Helper function to check in Column X
' Note:    called by main function MultiCriteria in section [2]
Dim found&, found2&, i&, j&, n&, ar: ReDim ar(0 To UBound(v) - 1)
howMany = 0      ' reset boolean value to default
  For i = LBound(v) To UBound(v)
    If Len(Trim(v(i, vColumn))) > 0 Then
       ar(n) = i
       n = n + 1
    End If
  Next i
  If n < 2 Then
     howMany = n: n = 2
  Else
     howMany = n
  End If
  ReDim Preserve ar(0 To n - 1)
  buildAr2 = ar
End Function
Ibrahim
  • 79
  • 10
  • 2
    *"i want to retrieve the color index but the macro leaves it blank"* I don't see in your code where you try to retrieve the color index. Please have a look at how to provide a [mcve]. – Pᴇʜ Aug 02 '18 at 13:11
  • @Pᴇʜ I'm only copying the values in the array here, i dont know how to say for this specific column copy the colorindex not it's value – Ibrahim Aug 02 '18 at 13:26
  • Please add a [mcve] with example data for input. – Pᴇʜ Aug 02 '18 at 13:28
  • 2
    `v = ws.Range("A10:Y" & n).Value2` This method will only copy the values. An alternative would be to copy the entire data to the array and if you have specific cells which will have the color then you can direclty replace the relevant values in the array. – Siddharth Rout Aug 02 '18 at 13:41
  • Hint to source code: The somewhat shortened code is based on my answer to [Multi criteria selection with VBA](https://stackoverflow.com/questions/51519145/multi-criteria-selection-with-vba/51524230#51524230) where I commented that (range) values assigned to a (data field) array don't contain formatting information and referred expressly to MCVE. – T.M. Aug 02 '18 at 19:13
  • @T.M. I read it but I didn’t understand well, I’m new to Vba so I’m searching on google but I understood that the method only copies value. What is MCVE? – Ibrahim Aug 02 '18 at 21:06
  • @SiddharthRout You mean not filtering on column and copy all? – Ibrahim Aug 02 '18 at 21:08
  • @T.M.[picture of the file](https://drive.google.com/open?id=13X5W0jMZqNz1sn3Bet8KIqJCyw2fSbV3) the layout in **"F"** is like this, it could have got me the string **"Tranche 1"** for each line but since it gave me a blank box **(didn't copy the string in the merged column)** I thought to copy the color code and apply **"Tranche 1,2,..."** depending on the color **(tranche 1 is blue, tranche 2 is red etc)**. I can't think of a way of putting it in a macro with your method. – Ibrahim Aug 03 '18 at 07:49
  • MCVE is the abbreviation for a **M**inimal, **C**omplete, and **V**erifiable **e**xample - [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). As this is no code writing service, SO expects a new question not only to include the desired behaviour, but that you describe the **core problem** or error you are confronted with by showing typically the *shortest* (M) code with *all* necessary parts (C) to reproduce the issue in a *verifiable* and tested way (V). Such a *MCVE* saves time and would be helpful for other readers - @Ibrahimatto – T.M. Aug 03 '18 at 10:15
  • **As this is no code writing service** that’s a little bit harsh @T.M. I’m new to this forum and new to coding so I might be all over the place, asking buntch of things not knowing all the rules but I’m trying to understand. I don’t just paste a code without knowing what it’s doing or trying to understand the method I’m the first to one that it won’t help me just to copy and past. I came to an English forum (using translator and my English skills) because that’s where I’ll get the most help so please be patient with me. Thank you for understanding. – Ibrahim Aug 03 '18 at 11:06
  • A hint as you can't find the logic to procede: The `a` array values already refers to the "filtered" items (zero based). If you add your title row offsets, you can deduct the source row numbers and get the the `Interior.ColorIndex` of the source cell. The filtered column `F` becomes the first column in the target sheet, so you reformat the target rows one by one using e.g. the `.Interior.ColorIndex`. property as proposed by @dv3. Try your own code looping through the arrays or ranges, test it, search at SO and consider MCVE posing a new question... *Bonne Chance* - @Ibrahimatto – T.M. Aug 03 '18 at 11:08
  • I didn't want to offend you and I do support your shown interest. Nevertheless it's expected to show some code by your own, so please try my hint in the comment above and learn from other SO questions :-) – T.M. Aug 03 '18 at 11:15

3 Answers3

1

In addition to the comments above by @Pᴇʜ, the fact that you are mainly dealing with v, a variant array of strings, is going to be a limiting factor. You are going to have to deal with a Range if you want the .Interior.ColorIndex property of the cell (Range).

Also, if you want to be precise about the color, use color instead of ColorIndex.
ColorIndex will return the closest indexed color.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Profex
  • 1,370
  • 8
  • 20
1

I have no idea where the problem is, but you asked:

the problem is in column F i have a color and i want to retrieve the color index but the macro leaves it blank

Here's how you retrieve the colorindex from Cell A1:

col = Range("A1").Interior.ColorIndex

I would suggest you try retrieving it and if you run into a problem: open a question with your example, as Pᴇʜ suggested.

dv3
  • 4,369
  • 5
  • 28
  • 50
1

How to copy filtered array values together with color format (column F)

  • You got the solution to filter a data field Array v by row AND column using the Application.Index property and write these data to a target sheet - c.f. Multi criteria selection with VBA
  • Your issue was to find a way to write not only data, but also the source color formatting of column F to the target cells, as an array per se contains values and no color info.

Write the filtered information to a defined STARTROW (e.g. 10), then you can use the item numbers of array a adding a headline offset headerIncrement) to reconstruct the source row numbers by a simple loop in order to get/write the color formats, too:

Code addition

' [4a] Copy results array to target sheet, e.g. start row at A10
  Const STARTROW& = 10
  ws2.Cells(STARTROW, 1).Offset(0, 0).Resize(UBound(v), UBound(v, 2)) = v
' **************************************************************************
' [4b] Copy color formats using available item number information in array a
' **************************************************************************
  Dim sourceColumn&: sourceColumn = 6   ' <<~~ source column F = 6
  Dim targetColumn&: targetColumn = 1   ' <<~~ becomes first target column
  Dim headerIncrement&: headerIncrement = STARTROW - 1
  For i = 0 To UBound(a)
    ws2.Cells(i + headerIncrement, targetColumn).Offset(1, 26).Interior.Color = _
    ws.Cells(a(i) + headerIncrement, sourceColumn).Interior.Color
  Next i

Side Note Don't forget to set Option Explicit to force declaration of variables and to declare the variable howMany (used in both procedures) in the declaration head of your code module.

T.M.
  • 9,436
  • 3
  • 33
  • 57
  • 1
    Merci pour ton aide @T.M i just did a little modifcation and it's working as expected – Ibrahim Aug 06 '18 at 06:52
  • *Avec plaisir* /You are welcome and ... eventually I liked your persistance and enthusiasm in asking as it led me to think over both a simple and clear structured solution *within* the current code's logic. Nevertheless it's good use at SO (Stack Overflow) to point out your *own* coding efforts or planned logical steps (pseudo code) so that other readers understand *where precisely* you are at your wit's end :-) – T.M. Aug 06 '18 at 11:28