繁体   English   中英

如何使用宏在Excel中将列范围设置为变量?

[英]How do I set column range as variable in Excel using a macro?

我有一个日期矩阵,每个日期代表一个不同的任务,项目,计划的/实际的开始/完成。 请看附图:

数据截图

由于实际名称是我公司的机密信息,因此我保留了所有内容。 无论如何,每一行和每一列都告诉不同的信息。 每两行告诉一个日期所属的项目,每行告诉该日期是开始日期还是结束日期。 每两列显示日期所属的任务,每列说明日期是预测日期还是实际日期。

顺便说一句,我正在尝试使用此数据创建一个宏,该宏将在用户设置的范围内搜索所有这些日期,并使用上述信息列出每个日期。 到目前为止,我有一个代码针对单个行执行此操作:

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语句以对我拥有的每一列数据重复,而不必复制和粘贴一百万次?

似乎您只需要一个嵌套的for循环即可:

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

原来我想要的是这样的:

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 ,然后使用索引。 但是,我现在似乎有一个单独的问题,那就是我要包含的另一个函数引起运行时错误,但这是另一篇文章的主题。 不过,它现在可以在没有该功能的情况下运行。 感谢您的输入!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM