循环两列

时间:2015-10-16 07:49:36

标签: excel vba date datetime

我尝试获取两列U和S之间的日期之间的差异。当我手动用实际数字替换PRow(例如:1)时,列AD按预期给出结果。 但是当我尝试使用增量PRow和行位置循环它们时,它会出现

Run-time error '1004':
Apllication-defined or object-defined error

这是我的编码:

Dim FRow As Long
Dim lrow As Long
Dim PRow As Long

Range("AD1").Value = "Response Time"

Dim CurrentSheet As Worksheet
Set CurrentSheet = Excel.ActiveSheet

Dim Date1 As Date
Dim Date2 As Date

For PRow = lrow To FRow Step -1

FRow = CurrentSheet.UsedRange.Cells(1).Row
lrow = CurrentSheet.UsedRange.Rows(CurrentSheet.UsedRange.Rows.count).Row

Date1 = DateValue(CurrentSheet.Cells(PRow, "U").Value)
Date2 = DateValue(CurrentSheet.Cells(PRow, "S").Value)
CurrentSheet.Cells(PRow, "AD").Value = Day(Date1) - Day(Date2)

Next PRow

那么,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

也许您应该在之前分配变量,并将其用作循环范围:-) 否则FROW和lrow将为0。

FRow = CurrentSheet.UsedRange.Cells(1).Row 
lrow = CurrentSheet.UsedRange.Rows(CurrentSheet.UsedRange.Rows.count).Row

For PRow = lrow To FRow Step -1
   ....