第一次使用VBA。
摘要问题伪代码:我有一个包含3行(日期,代码,现金)的输出工作表。我在单元格A1中输入了日期。
我想遍历日期行并找到与A1输入日期匹配的日期。如果匹配,我想检查它下面的行中是否有“代码”匹配项(“代码”为1)。如果日期和代码都匹配,则我想要一个变量cash来将值存储在该匹配列的第三行的单元格中。总是只有1个匹配项,并且20列足以检查。
我在“ Set curCell”行上收到“需要对象”错误。
********将行更改为“ xx” .value2,将3个变量设置为范围。仍然收到错误?**
Sub GetCash()
Dim theDate As String: theDate = Worksheets("Sheet1").Range("A1").Value
Dim curCell As Range, perCell As Range, cashCell As Range, Counter As Integer, Cash As Integer
For Counter = 1 To 20
Set curCell = Worksheets("Sheet1").Cells(5, Counter).Value2
Set perCell = Worksheets("Sheet1").Cells(6, Counter).Value2
Set cashCell = Worksheets("Sheet1").Cells(7, Counter).Value2
If curCell = theDate And perCell = 1 Then Cash = cashCell
Next Counter
MsgBox Cash
End Sub