2017-06-01 500 views
0

我有一個日期矩陣,每個代表不同的任務,項目,計劃/實際開始/結束。請參閱所附圖片:如何在Excel中使用宏將列範圍設置爲變量?

Screenshot of data

我曾概括爲實際名稱左側的一切都是保密的,以我的公司。無論如何,每行和每列都會告訴不同的信息。每兩行都會告訴日期屬於哪個項目,並且每一行都會告訴您該日期是開始日期還是結束日期。每兩列說明日期屬於哪個任務,每列指出日期是預測日期還是實際日期。

撇開,我試圖用這些數據做的是創建一個宏,它將在用戶設置的範圍內搜索所有這些日期,並用上述信息列出每個日期。到目前爲止,我有一個代碼,做這行各行:

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 If語句爲每列,但必須有一個更有效的方式來做到這一點。我對Excel宏很陌生(其他人實際上編寫了該代碼的基礎),所以我不知道如何使它做到我想要的。我想它會涉及到把字母變成數字,然後變成變量,但我只是不知道。我試圖查找它,但我在應用我發現的特定應用程序時遇到了麻煩。

所以我的主要問題是:我如何得到If If語句重複每一列數據,而不必複製和粘貼一百萬次?

+0

因此,如果每個其他列都有您想要的日期,您可以指定For語句的步驟,例如For i = 2到100 Step 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 

基本上,我需要使用Range.Value切換到Cells.Value和然後使用索引。然而,我似乎現在有一個單獨的問題,導致與我想包含的另一個函數的運行時錯誤,但這是另一篇文章的主題。儘管如此,它現在沒有這個功能。感謝您的輸入!