如何修改此代码以搜索60000行并在相邻列中粘贴1700个已找到的值

时间:2012-01-23 01:32:01

标签: excel-vba vba excel

我是excel Visual Basic宏编程的新手,试图创建一个查找/复制/粘贴宏,它将在A列中搜索“$”(其中有60000行(具有间歇性空白))。我已经发现在A列中有1700个“$”出现,我必须将其粘贴到columnA中找到数据的每个Cell的相邻列B中。

这是我到目前为止所做的:

Sub FindAValue()
'
'This macro finds the value and paste to a single cell
'

'
Selection.Find(What:="$", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart
, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A2").Select
Selection.Copy
Range("B2").Select
ActiveSheet.Paste
Application.CutCopyMode=False
End Sub

我已经查看了各种博客的大量帖子,我仍然无法理解如何扩展此宏,以便它搜索所有ColumnA,找到每个“$”,并将其粘贴到ColumnA的每一行旁边的ColumnB中,直到找到一个空白。 1700“$”值中的每一个都是唯一的,后续单元格的数量也可以变化到60000行(包括空格。感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

我认为你正在寻找像这样的东西

Public Const DATACOL = 1
Public Const DESTCOL = 2

Sub exp()


'set initial row value
Row = 1
'iterate until a blank is found
Do While Cells(Row, DATACOL) <> ""
  'look is each datacell for $
  'instr returns the location of the search string as a number
  'and will return 0 if not found
  If InStr(1, Cells(Row, DATACOL), "$") > 0 Then
  'copy data
    Cells(Row, DESTCOL) = Cells(Row, DATACOL)
  End If

  'next row
  Row = Row + 1
Loop

End Sub