我每天都会从外部来源输入数据。在一张纸上,我有一个股票代码列表(按字母顺序排列),并在该行中继续显示相应的数据。记录宏(Excel 2003)按行有条件地复制粘贴
在另一张纸上,我有股票按其相应的部门组织,而不是按字母顺序排列。
我试图开发一个宏,使第一张工作表中的信息自动粘贴到第二张工作表中,方法是识别代码并粘贴到相应的行中。
这里是到目前为止所使用的代码,但它没有工作打算的方式:
Dim LSymbol As String
Dim LRow As Integer
Dim LFound As Boolean
On Error GoTo Err_Execute
'Retrieve symbol value to search for
LSymbol = Sheets("Portfolio Update").Range("B4").Value
Sheets("Test").Select
'Start at row 2
LRow = 2
LFound = False
While LFound = False
'Encountered blank cell in column B, terminate search
If Len(Cells(2, LRow)) = 0 Then
MsgBox "No matching symbol was found."
Exit Sub
'Found match in column b
ElseIf Cells(2, LRow) = LSymbol Then
'Select values to copy from "Portfolio Update" sheet
Sheets("Portfolio Update").Select
Range("B5:V5").Select
Selection.Copy
'Paste onto "Test" sheet
Sheets("Test").Select
Cells(3, LRow).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
LFound = True
MsgBox "The data has been successfully copied."
'Continue searching
Else
LRow = LRow + 1
End If
Wend
On Error GoTo 0
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
感谢。
它做什么,而不是按照预期工作? – 2012-08-08 18:40:26
应该是'.Cells(row,col)'不是'.Cells(col,row)' – 2012-08-08 18:54:18
@TimWilliams:这是一个有效的答案;)(提示提示)@EBB:避免使用'.Select'看这个链接http ://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select – 2012-08-09 01:10:12