2016-07-19 5 views
1

Excelでvba関数を作成して、セルの色で列を並べ替えて別の場所に結果を出力しようとしています。私が現在持っているものは、現在私にエラーを与えています色で並べ替えて別の場所に出力

Function SortColor(colorSearchRange As Range, colorToSearch As Range, outputRange As Range) 
Dim colorValue As Integer 
Dim coloredItems(150) As String 
Dim index As Integer 

colorValue = colorToSearch.Interior.ColorIndex 
index = 0 
Set cell = colorSearchRange 
For Each cell In colorSearchRange 
    If cell.Interior.ColorIndex = colorValue Then 
     coloredItems(index) = cell.Value 
     index = index + 1 
    End If 
Next cell 

Range(outputRange & UBound(coloredItems) + 1) = WorksheetFunction.Transpose(coloredItems) 

End Function 

私はビジュアルベーシックで初めてです。どのようなタイプの助けでも大歓迎です。 cellFor Each cell In colorSearchRangeループ

最終的にあなたのFunction ISN」は各繰り返しで設定されようとしていることから、あなたは、Set cell = colorSearchRangeを必要としないも

outputRange.Resize(index) = WorksheetFunction.Transpose(coloredItems) 

+2

*現在、エラーが表示されます* - エラーとは何ですか(何行目)ですか? ...一見すると、この行 'Range(outputRange&UBound(coloredItems)+ 1)'は範囲引数のsytnaxが正しくありません –

+0

@ScottHoltzmanエラーが発生しました。「式に使用されている値が間違ったデータですタイプ"。私はデバッガを通過して、配列colourItemsが正しく構築されているように見えますが、outputRangeに正しく出力されていません。 –

+0

これはScottの話です。 Rangeメソッドが間違ったタイプの引数で呼び出されているため、最終行を書き直す必要があります。 私は 'RangeRange(outputRange、outputRange.Offset(UBound(colourItems)))'を提案します。これは、あなたが探していたと思っているoutputRangeで始まる列のすべてのデータを提供します。 – Mikegrann

答えて

1

は、あなたのようなものを使用する必要があります何も返さないので、あなたはそれを作ることができますSub

上記の結果は次のようになります:

Sub SortColor(colorSearchRange As Range, colorToSearch As Range, outputRange As Range) 
    Dim colorValue As Long, index As Long 
    Dim coloredItems() As String 
    Dim cell As Range 

    ReDim coloredItems(1 To colorSearchRange.Rows.Count) As String 'dim your array to the maxiumum possible for the passed range 
    colorValue = colorToSearch.Interior.ColorIndex 
    For Each cell In colorSearchRange 
     If cell.Interior.ColorIndex = colorValue Then 
      index = index + 1 
      coloredItems(index) = cell.Value 
     End If 
    Next cell 
    outputRange.Resize(index) = WorksheetFunction.Transpose(coloredItems) 
End Sub 
関連する問題