Excel 宏将数据从一个工作表复制并粘贴到另一工作表
我正在尝试在列中搜索值并从 Sheet1 复制行并创建新工作表作为 MySheet 并粘贴该特定行。但是在 MySheet 中粘贴数据时出现运行时错误。请提供任何建议。
我正在尝试的数据输入:
ID名称价格单位desc
1 ikura 10 4 Mail Box
2 test 11 14 xxxx
3 test 11 14 yyyy
4 test 11 14 Mail Box
Sub SearchForString()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
On Error GoTo Err_Execute
'Start search in row 4
LSearchRow = 4
'Start copying data to row 2 in Sheet2 (row counter variable)
LCopyToRow = 2
Worksheets.Add (After:=Worksheets(Worksheets.Count)).Name = "MySheet"
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column E = "Mail Box", copy entire row to Sheet2
If Range("E" & CStr(LSearchRow)).Value = "Mail Box" Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Sheet2 in next row
Sheets("MySheet").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Sheet1").Select
End If
LSearchRow = LSearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
问候,
Raju
I am trying to Search for a value in a column and copy row from Sheet1 and creating new sheet as MySheet and pasting that particular row .But I am getting run time error while pasting data in MySheet.Any suggestions please.
Data Input I am trying :
ID name price units desc
1 ikura 10 4 Mail Box
2 test 11 14 xxxx
3 test 11 14 yyyy
4 test 11 14 Mail Box
Sub SearchForString()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
On Error GoTo Err_Execute
'Start search in row 4
LSearchRow = 4
'Start copying data to row 2 in Sheet2 (row counter variable)
LCopyToRow = 2
Worksheets.Add (After:=Worksheets(Worksheets.Count)).Name = "MySheet"
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column E = "Mail Box", copy entire row to Sheet2
If Range("E" & CStr(LSearchRow)).Value = "Mail Box" Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Sheet2 in next row
Sheets("MySheet").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Sheet1").Select
End If
LSearchRow = LSearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
Regards,
Raju
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先要做的事情是:
魔鬼的手段。直接处理范围/工作表对象。
错误。描述。如果你从一开始就这么做了
可能不必发布这个问题。
以避免一些潜在的麻烦。
这是一些简化的代码,看看它是否有效:
First things first:
devil's methods. Deal with range/worksheet objects directly.
err.Description. If you'd done that from the beginning you
probably wouldn't have had to post this question.
to save some potential headaches.
Here's some simplified code, see if it works:
尝试这个简化版本:
Try this simplified version: