2017-06-01 9 views
0

私は、それぞれが異なるタスク、プロジェクト、予定/実際の開始/終了を表す日付の行列を持っています。実際の名前は、私の会社に機密ているとして、私は一般的にすべてのものを残しているマクロを使用してExcelで列範囲を変数として設定するにはどうすればよいですか?

Screenshot of data

:添付画像を参照してください。とにかく、すべての行と列は別の情報を伝えます。 2行に1つの日付が属するプロジェクトが示され、すべての行にその日付が開始日か終了日かが示されます。 2つの列ごとに、その日付がどのタスクに属しているかが示され、すべての列に日付が予測された日付か実際の日付かが示されます。

私がこのデータを使って何をしようとしているのは、ユーザーが設定した範囲内のこれらの日付をすべて検索し、前述の情報で各日付をリストするマクロを作成することです。これまでのところ、私は、コードを持っている個々の行については、このん:

Sub Sort_By_Date() 

Dim StartDate As Date 
Dim EndDate As Date 
Dim i As Integer 


StartDate = Range("F61").Value 
EndDate = Range("G61").Value 

'Clears out cells 
Range("Z74:AD200").Value = "" 

m = 74 

For i = 71 To 200 
If Range("C" & i).Value >= StartDate And Range("C" & i).Value <= EndDate Then 
Range("Z" & m).Value = Range("C" & i).Value 
Range("AA" & m).Value = Range("A" & i).Value 
Range("AB" & m).Value = Range("C69") 
Range("AC" & m).Value = Range("B" & i) 
Range("AD" & m).Value = Range("C70") 
m = m + 1 
End If 

If Range("D" & i).Value >= StartDate And Range("D" & i).Value <= EndDate Then 
Range("Z" & m).Value = Range("D" & i).Value 
Range("AA" & m).Value = Range("A" & i).Value 
Range("AB" & m).Value = Range("D69") 
Range("AC" & m).Value = Range("B" & i) 
Range("AD" & m).Value = Range("D70") 
m = m + 1 
End If 

If Range("E" & i).Value >= StartDate And Range("E" & i).Value <= EndDate Then 
Range("Z" & m).Value = Range("E" & i).Value 
Range("AA" & m).Value = Range("A" & i).Value 
Range("AB" & m).Value = Range("E69") 
Range("AC" & m).Value = Range("B" & i) 
Range("AD" & m).Value = Range("E70") 
m = m + 1 
End If 

... ... ... がうまくいけば、あなたはここでパターンを見ることができます。

すべての列に対してIf Then文をコピーして貼り付けておくことができますが、効率的な方法が必要です。私はかなりExcelマクロに新しい(他の誰かが実際にそのコードの基礎を書いた)ので、私はそれが私がそれをしたいと思うようにする方法がわからない。私はそれが列の文字を数字にし、その後変数にすることを含むと想像していますが、わかりません。私はそれを探してみましたが、特定のアプリケーションに見つかったものを適用するのに問題があります。

私の主な質問:何百万回もコピーして貼り付けなくても、データの各列に対してそのIf Thenステートメントを繰り返すにはどうすればよいですか?あなたは、単にループのネストされたが必要のよう

+0

したがって、他のすべての列に必要な日付がある場合、Forステートメントのステップを指定できます(For i = 2〜100ステップ2)。各Forステートメント内では、If/Thenステートメントを使用できます。合計など – Cyril

答えて

0

は思え:

Dim StartDate As Date 
Dim EndDate As Date 
Dim i As Integer, c as integer 


StartDate = Range("F61").Value 
EndDate = Range("G61").Value 

'Clears out cells 
Range("Z74:AD200").Value = "" 

m = 74 

'From column C to column AD 
For c = 3 TO 30 
    For i = 71 To 200 
    If Range("C" & i).Value >= StartDate And Range("C" & i).Value <= EndDate Then 
     Range("Z" & m).Value = cells(i,c).Value 
     Range("AA" & m).Value = Range("A" & i).Value 
     Range("AB" & m).Value = cells(69,c).value 
     Range("AC" & m).Value = Range("B" & i) 
     Range("AD" & m).Value = cells(70,c).value 
     m = m + 1 
    End If 
    Next 
Next 
0

は私が探していたものが判明し、このた:

Sub Sort_By_Date() 

Dim StartDate As Date 
Dim EndDate As Date 
Dim rwMin As Integer 
Dim colMin As Integer 
Dim rwMax As Integer 
Dim colMax As Integer 
Dim rwIndex As Integer 
Dim colIndex As Integer 

StartDate = Range("F61").Value 
EndDate = Range("G61").Value 

rwMin = 4 
colMin = 3 
rwMax = 37 
colMax = 68 

Range("L44:R2600").Value = "" 

m = 44 

For colIndex = colMin To colMax 

    For rwIndex = rwMin To rwMax 

    If Cells(rwIndex, colIndex).Value >= StartDate And Cells(rwIndex, colIndex).Value <= EndDate Then 
    Range("L" & m).Value = Cells(rwIndex, colIndex).Value 
    Range("M" & m).Value = Cells(rwIndex, 1).Value 
    Range("N" & m).Value = Cells(2, colIndex).Value 
    Range("O" & m).Value = Cells(1, colIndex).Value 
    Range("P" & m).Value = Cells(rwIndex, 2).Value 
    Range("Q" & m).Value = Cells(3, colIndex).Value 
    m = m + 1 
    End If 

    Next rwIndex 


Next colIndex 

End Sub 

基本的に、私はCells.ValueRange.Valueを使用してから切り替えるために必要とインデックスを使用します。しかし、私は今私がインクルードしたい別の機能でランタイムエラーを引き起こすという別の問題を抱えているようですが、それは別の投稿のトピックです。しかし、今はその機能がなくても動作します。あなたのご意見ありがとうございます!

関連する問題