我提取的数据来自excel文件,该文件位于所提供日期的参数内。但是这段代码不起作用。任何人都可以帮我解决这个问题吗?
Set src = wb.Sheets("Request Log Extract")
Set dest = ThisWorkbook.Sheets("Resolution Time Performance")
srcRow = src.Cells(src.Rows.Count, "K").End(xlUp).Row
destRow = dest.Cells(dest.Rows.Count, "E").End(xlUp).Row + 1
wb.Activate
For i = 2 To srcRow
If src.Cells("K" & i) >= txtStartDate.Value Or src.Cells("K" & i) <= .txtEndDate.Value Then
src.Cells("K" & i).Copy
dest.Activate
dest.Cells("E" & i).Paste
src.Activate
End If
Next
这会返回错误说明:
无效的过程调用或参数。
注意
txtStartDate和txtEndDate是日期类型。
如果我在If条件中使用 OR ,则会复制所有数据,但如果我使用和,则不会复制任何数据。我不知道最近发生了什么。
VALUES
txtStartDate 05/13/2016 txtEndDate 05/18/2016 k2 05/14/2016
答案 0 :(得分:0)
我不确定你的txtStartDate和txtEndDate变量,但看看我的代码
我声明了你的变量,但请指定日期类型,我也从txtEndDate
删除了点并更改了单元格引用,现在它可以正常工作。
Sub extractData()
Dim src
Dim dest
Dim wb As Workbook
Set wb = ThisWorkbook
Dim txtStartDate
Dim txtEndDate
Set src = wb.Sheets("Request Log Extract")
Set dest = ThisWorkbook.Sheets("Resolution Time Performance")
srcRow = src.Cells(src.Rows.Count, "K").End(xlUp).Row
destRow = dest.Cells(dest.Rows.Count, "E").End(xlUp).Row + 1
txtStartDate = 0
txtEndDate = 100
For i = 2 To srcRow
If src.Cells(i, "K").Value > txtStartDate Or src.Cells(i, "K").Value < txtEndDate Then
src.Cells(i, "K").Copy
dest.Activate
dest.Cells(i, "E").PasteSpecial
src.Activate
End If
Next
End Sub
答案 1 :(得分:0)
我认为这是一个日期值问题
此外,我猜你的代码在某个用户窗格内,并在某个按钮点击时激活,之后它必须将两个文本框值与一些单元格内容进行比较并相应地复制/粘贴值
我的猜测应该是正确的(手指交叉......)试试这个:
Option Explicit
Private Sub CommandButton1_Click()
Dim src As Worksheet, dest As Worksheet
Dim srcRow As Long, destRow As Long, i As Long
Dim startDate As Date, endDate As Date, cellDate As Date
With Me
If Not ValidateDate("txtStartDate", .txtStartDate.Value, startDate) Then Exit Sub
If Not ValidateDate("txtEndDate", .txtEndDate.Value, endDate) Then Exit Sub
Set src = ActiveWorkbook.Sheets("Request Log Extract") '<~~ change workbook reference as per your need
Set dest = ThisWorkbook.Sheets("Resolution Time Performance")
srcRow = src.Cells(src.Rows.Count, "K").End(xlUp).Row
destRow = dest.Cells(dest.Rows.Count, "E").End(xlUp).Row + 1
For i = 2 To srcRow
If ValidateDate("src.Range(""K" & i & """)", src.Range("K" & i), cellDate) Then
If cellDate >= startDate And cellDate <= endDate Then src.Range("K" & i).Copy dest.Range("E" & i)
End If
Next
End With
End Sub
Function ValidateDate(textName As String, textValue As String, retDate As Date) As Boolean
ValidateDate = IsDate(textValue)
If ValidateDate Then
retDate = DateValue(textValue)
Else
MsgBox textValue & " is not a valid date" & vbCrLf & "please input a new value for " & textName
End If
End Function
如果我的猜测错了,上面的代码仍然会给你一些关于日期值问题的建议
答案 2 :(得分:0)
此代码对我有用:
Sub Demo()
Dim wb As Workbook
Dim txtStartDate As Date, txtEndDate As Date
Set wb = ActiveWorkbook
Set src = wb.Sheets("Request Log Extract")
Set dest = wb.Sheets("Resolution Time Performance")
srcRow = src.Cells(src.Rows.Count, "K").End(xlUp).Row
destRow = dest.Cells(dest.Rows.Count, "E").End(xlUp).Row + 1
txtStartDate = "05/13/2016"
txtEndDate = "05/18/2016"
For i = 2 To srcRow
If src.Range("K" & i).Value >= txtStartDate And src.Range("K" & i).Value <= txtEndDate Then
src.Range("K" & i).Copy Destination:=dest.Range("E" & i)
End If
Next
End Sub