2017-06-01 494 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和然后使用索引。然而,我似乎现在有一个单独的问题,导致与我想包含的另一个函数的运行时错误,但这是另一篇文章的主题。尽管如此,它现在没有这个功能。感谢您的输入!