simplifying code- loop??

akforsyt

New Member
Joined
Apr 9, 2009
Messages
18
I have the following code. I would like to do this same operation for several more rows. I'm sure there has got to be a way to simplify this using some type of loop or variables, but I can't figure it out. What I am doing is pasting copied text into the cell when I click on it, then looking up a cell in the same row that I copied from and pasting this value next to it.

Code:
If Target.Address = "$E$3" Then
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

FndStr = Cells(3, 5)
     Set FndVal = Columns("C").Find(What:=FndStr, LookAt:=xlWhole)
        If FndVal Is Nothing Then
           MsgBox "No End Time Found"
        Else
            FndVal.Offset(0, 4).Copy
        End If
        Cells(3, 6).Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If


If Target.Address = "$E$4" Then
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

FndStr = Cells(4, 5)
     Set FndVal = Columns("C").Find(What:=FndStr, LookAt:=xlWhole)
        If FndVal Is Nothing Then
           MsgBox "No End Time Found"
        Else
            FndVal.Offset(0, 4).Copy
        End If
        Cells(4, 6).Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If
The parts that change I have replaced with X below (x is all the same #)...

Code:
If Target.Address = "$E$X" Then
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

FndStr = Cells(X, 5)
     Set FndVal = Columns("C").Find(What:=FndStr, LookAt:=xlWhole)
        If FndVal Is Nothing Then
           MsgBox "No End Time Found"
        Else
            'MsgBox FndVal.Offset(0, 6).Value
            FndVal.Offset(0, 4).Copy
        End If
        Cells(X, 6).Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If

Help please!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
you could use
Code:
if target.column = 5 then 
select case target.row
  case 4 'address "e4"
         'your code
  case 5 'address "e5"
         'your code
    'etc
 
Upvote 0
It looks like you may have taken your sample code from a larger macro, but perhaps something like this?

Code:
Dim RowStart As Long, RowEnd As Long, i As Long
Dim FndStr As Range, FndVal As Range

Do
    On Error Resume Next
    RowStart = Application.InputBox("Enter the start row:", Type:=1)
    On Error GoTo 0
    If RowStart < 1 Or RowStart >= Rows.Count Then MsgBox "You must enter a number between 1 and " & Rows.Count & "."
Loop While RowStart < 1 Or RowStart >= Rows.Count
    
Do
    On Error Resume Next
    RowEnd = Application.InputBox("Enter the end row:", Type:=1)
    On Error GoTo 0
    If RowEnd < RowStart Then
        MsgBox "You must enter a row number greater than the start row of " & RowStart & "."
    ElseIf RowEnd < 1 Or RowEnd > Rows.Count Then
        MsgBox "You must enter a number between 1 and " & Rows.Count & "."
    End If
Loop While RowEnd < 1 Or RowEnd > Rows.Count Or RowEnd < RowStart

For i = RowStart To RowEnd
    Set FndStr = Cells(i, 5)
    Set FndVal = Columns("C").Find(What:=FndStr.Value, LookAt:=xlWhole)
    If FndVal Is Nothing Then
        MsgBox "No End Time Found"
    Else
        FndVal.Offset(0, 4).Copy
    End If
    FndStr.Offset(0, 1).PasteSpecial xlPasteFormulas
Next i
 
Upvote 0
westconn... I think that would work, but I would still have to write in the code for every row. i am looking for a simpler way to put this.

kristy- yes, this code is part of a much, much larger project...
I will try the code you have posted when I get back to work on Monday.

I will post a follow-up later.

Thanks everyone.
 
Upvote 0
try
Code:
With Target.Cells(1)
    If Not Intersect(.Cells, Range("e3:e100")) Is Nothing Then"
        .PasteSpecial Paste:=xlPasteFormulas
        FndStr = .Value
        Set FndVal = Columns("C").Find(What:=FndStr, LookAt:=xlWhole)
        If FndVal Is Nothing Then
           MsgBox "No End Time Found"
        Else
            FndVal.Offset(0, 4).Copy
        End If
        .Offset(, 1).PasteSpecial Paste:=xlPasteFormulas
    End Select
End With
Application.CutCopyMode = False
 
Upvote 0
only for any rows that require different code

Code:
if target.column = 5 then 
select case target.row
  case 1 to 4 
         'your code
  case 5, 9, 11, 15
         'your code
  
    'etc
 
Upvote 0
Perhaps I'm missing something but don't you already know the row you are copying from.

If you do why do you need to do any sort of lookup/find/loop?
 
Upvote 0
thanks everyone. I've actually managed to work it out using a combination of some of the above. I used the select case and target.column stuff. I've posted the solution below. Again, thanks for all of your contributions.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim FndStr As String
Dim FndVal As Range
On Error GoTo ErrorEnd:

Application.ScreenUpdating = False
If Target.Column = 2 Then
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Select Case Target.Row
  Case 3 To 12
FndStr = Worksheets("ExtraRooms").Cells(Target.Row, Target.Column)
     Set FndVal = Worksheets("TestingSchedule").Columns("C").Find(What:=FndStr, LookAt:=xlWhole)
        If FndVal Is Nothing Then
           MsgBox "No End Time Found"
        Else
            FndVal.Offset(0, 4).Copy
        End If
       Worksheets("ExtraRooms").Cells(Target.Row, Target.Column + 1).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Select
End If

ErrorEnd:
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,527
Messages
6,160,342
Members
451,638
Latest member
MyFlower

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top