[英]Extracting and copying data in an excel file
I'm extracting data froman excel file that is inside the parameter of the dates provided. 我正在从提供的日期的参数内的Excel文件中提取数据。 But this code is not working.
但是此代码不起作用。 Anybody can help me figure this out?
有人可以帮我解决这个问题吗?
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
This returns an error saying : 这将返回一条错误消息:
Invalid procedure call or argument.
无效的过程调用或参数。
NOTE 注意
txtStartDate and txtEndDate are date Types. txtStartDate和txtEndDate是日期类型。
If I use OR in the If condition, all data were copied, but if I used And , no data is copied. 如果在If条件中使用OR ,则将复制所有数据,但是如果使用And ,则不会复制任何数据。 I don't know whats going on.
我不知道怎么回事。
VALUES VALUES
txtStartDate 05/13/2016 txtEndDate 05/18/2016 k2 05/14/2016 txtStartDate 05/13/2016 txtEndDate 05/18/2016 k2 05/14/2016
Im not sure with your txtStartDate and txtEndDate variables, but look at my code I declared your variables, but please specify date types, also i removed dot from txtEndDate
and changed cell references and now it works. 我不确定您的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
I think it's a date value issue 我认为这是日期值问题
Moreover I'm guessing your code is within some userform pane and activated at some button click after which it has to compare two textboxes values to some cells content and copy/paste values accordingly 此外,我猜您的代码在某些用户窗体窗格中,并且在单击某些按钮后被激活,之后它必须将两个文本框值与某些单元格内容进行比较,并相应地复制/粘贴值
should my guessing be right (finger crossed...) try this: 如果我的猜测正确(手指交叉...),请尝试以下操作:
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
should my guessing be wrong, still the code above can give you some suggestions as to the date value issue 如果我的猜测是错误的,上面的代码仍然可以为您提供有关日期值问题的一些建议
This code is working for me: 这段代码为我工作:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.