简体   繁体   中英

Write an Excel macro to find and value, copy that value and paste into a Cell

I'm looking to write an excel VBA macro to

  • find a value "New Member" in a table (named "array1")
  • look to and copy the cell 5 cells to the left of the found item
  • paste into Cell G5
  • if the value "New Member" isn't found write "not found" in cell G5

I've tried various versions of


        Cells.Find("New Member").Activate
        ActiveCell.Offset(0, -5).Range("array1").Select
        Selection.Copy
     Range("????").Select
         ActiveSheet.Paste
        If aaa = "" Then
        MsgBox "nope"
        Else

        Range("G5").Select
        ActiveSheet.Paste

My vba excel macro skills are very new. Thanks!

You can do it without all the Select/Activate stuff.

Dim rngFound As Range

Set rngFound = Range("Array1").Cells.Find("New Member")
  
If rngFound Is Nothing Then
    Range("G5").Value = "Not Found"
Else
    rngFound.Offset(,-5).Resize(,5).Copy Range("G5")
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM