有没有办法将整数值数组传递给interior.colorindex

时间:2015-06-26 19:19:28

标签: arrays excel vba

我正在撰写报告,删除我不需要的内容。它按区域分开。然后我试图重新格式化它。在重新格式化期间,而不是使用“完整”,“空”和“等待”字样。我需要将机架位置背景着色。绿色为全色,浅红色为空,另一种颜色为等待。我正在处理大量数据。

当我设置范围时,我可以将数组中的所有值传递到范围

Set RngRow = TargetSheet.Range(TargetSheet.Cells(LastRow, 1), TargetSheet.Cells(LastRow, MatCount + 1))

RngRow.Value = MyMaterial

但是当我尝试传递单元格背景颜色的整数值数组时,我得到类型不匹配

  RngRow.Interior.ColorIndex = MyFormat

我在这里遗漏了一些东西,我认为这是一个没有看到树木森林的情况。我会很感激任何指向我做错的指示

SupplyArea  Plnt        Material                   Number         Kanban Quantity       BUn
 ID number  No. Status        Actual quantity   Date    Time        MRP elemnt      Replen.elemt    

CVC015I10B  1170        2958910             2       10      EA
112827  1   WAIT        10  5/26/2015   7:39:19     JITCll      1345847 
112828  2   WAIT        10  5/26/2015   7:39:38     JITCll      1345848 

CVC015I10B  1170        3698163             2       8       EA
112829  1   WAIT        8   5/26/2015   7:40:37     JITCll      775674  
112830  2   WAIT            00/00/0000  0:00:00                 

我的代码就是这个

Sub PK18_EPID()
  Dim SourceSheet, TargetSheet As Worksheet
  Dim HeadingLoop, RngCount, LastRow, RowCount, MatCount As Long
  Dim DelCntr, ShtCnt As Integer
  Dim MyMaterial()  As String
  Dim MyFormat() As Integer
  Dim HeaderValues, SheetNames, SheetUsedRows As Variant
  Dim RngRow As Excel.Range

  Set SourceSheet = Sheets("pk18")
  With SourceSheet
    .Select
    For DelCntr = 1 To 4 Step 1
       Rows(1).EntireRow.Delete
    Next
    Columns(1).EntireColumn.Delete
    For DelCntr = 1 To 8 Step 1
      Columns(5).EntireColumn.Delete
    Next
  End With
  SheetNames = Array("SS", "NN", "LL", "CV", "Non_Std")
  SheetUsedRows = Array(1, 1, 1, 1, 1)
  HeaderValues = Array("Material", "Supply Area", "Kanban ID", "Status")
  RngCount = UBound(HeaderValues) + 1
  For HeadingLoop = 0 To UBound(SheetNames)
    Worksheets.Add(After:=ActiveSheet).Name = SheetNames(HeadingLoop)
    Set TargetSheet = Worksheets(SheetNames(HeadingLoop))
    Set RngRow = TargetSheet.Range(TargetSheet.Cells(1, 1),     TargetSheet.Cells(1, RngCount))
    RngRow.Value = HeaderValues
  Next HeadingLoop

  With SourceSheet
    Do While .UsedRange.Rows.Count > 1
      MatCount = 0
      Do
        MatCount = MatCount + 1
      Loop While SourceSheet.Cells(MatCount + 1, 1).Value <> ""

      ReDim MyMaterial(MatCount)
      ReDim MyFormat(MatCount)
      MyMaterial(0) = SourceSheet.Cells(1, 4).Value
      MyMaterial(1) = SourceSheet.Cells(1, 1).Value
      MyFormat(0) = 1
      MyFormat(1) = 1
      RowCount = 0
      For RowCount = 2 To (MatCount)
        MyMaterial(RowCount) = SourceSheet.Cells(RowCount, 1).Value
        Select Case True
          Case SourceSheet.Cells(RowCount, 3).Value = "FULL"
            MyFormat(RowCount) = 35
          Case SourceSheet.Cells(RowCount, 3).Value = "EMPTY"
            MyFormat(RowCount) = 2
          Case SourceSheet.Cells(RowCount, 3).Value = "WAIT"
            MyFormat(RowCount) = 15
        End Select
      Next
      Select Case True
        Case Left(SourceSheet.Cells(1, 1).Value, 2) = "SS"
          Set TargetSheet = Worksheets("SS")
          SheetUsedRows(0) = SheetUsedRows(0) + 1
          LastRow = SheetUsedRows(0)
        Case Left(SourceSheet.Cells(1, 1).Value, 2) = "NN"
          Set TargetSheet = Worksheets("NN")
          SheetUsedRows(1) = SheetUsedRows(1) + 1
          LastRow = SheetUsedRows(1)
        Case Left(SourceSheet.Cells(1, 1).Value, 2) = "LL"
          Set TargetSheet = Worksheets("LL")
          SheetUsedRows(2) = SheetUsedRows(2) + 1
          LastRow = SheetUsedRows(2)
        Case Left(SourceSheet.Cells(1, 1).Value, 2) = "CV"
          Set TargetSheet = Worksheets("CV")
          SheetUsedRows(3) = SheetUsedRows(3) + 1
          LastRow = SheetUsedRows(3)
        Case Else
          Set TargetSheet = Worksheets("Non_Std")
          SheetUsedRows(4) = SheetUsedRows(4) + 1
          LastRow = SheetUsedRows(4)
      End Select
      Set RngRow = TargetSheet.Range(TargetSheet.Cells(LastRow, 1),    TargetSheet.Cells(LastRow, MatCount + 1))
      RngRow.Value = MyMaterial
      RngRow.Interior.ColorIndex = MyFormat
      For DelCntr = 1 To MatCount + 1
        SourceSheet.Rows(1).EntireRow.Delete
      Next
    Loop
  End With
End Sub

1 个答案:

答案 0 :(得分:0)

如果单元格将包含预定义的文本值(&#34; FULL,&#34; EMPTY&#34;或&#34; WAIT&#34;),只需将conditional formatting应用于此列让Excel为你着色。