PDA

View Full Version : [SOLVED:] Where is the error in this short code??



Cinema
02-29-2016, 02:31 AM
Hi,

I want to write a macro that searches a word in specific Sheets and then copy the cells right next to it. But I have a Problem with the "Next" command. Where is the error? Can somebody help me please?

Sub Makro1()
Dim ws As Worksheet
Dim rng As Range
Dim sAddress As String
Dim sFind As String
sFind = InputBox("Please enter word to find:")

For Each ws In Worksheets
If ws.Name Like "Table*" Then
Set rng = ws.Cells.Find( _
what:=sFind, _
lookat:=xlWhole, _
LookIn:=xlFormulas)
If Not rng Is Nothing Then
Set rng = ws.Range(rng.Offset(0, 1), rng.End(xlToRight))

Next ws

End If

End Sub

GTO
02-29-2016, 02:40 AM
You are missing an 'End If' after the first 'If'. After the second 'If', you have the 'End If' after the 'Next ws'.

A guess, but should it be?



Option Explicit
'
Sub Makro1()
Dim ws As Worksheet
Dim rng As Range
Dim sAddress As String
Dim sFind As String
'
sFind = InputBox("Please enter word to find:")
'
For Each ws In Worksheets
If ws.Name Like "Table*" Then
Set rng = ws.Cells.Find(what:=sFind, lookat:=xlWhole, LookIn:=xlFormulas)

If Not rng Is Nothing Then
Set rng = ws.Range(rng.Offset(0, 1), rng.End(xlToRight))
End If
End If
Next ws
'
End Sub



Hope that helps,

Mark

Cinema
02-29-2016, 02:44 AM
Hi GTO thank you so much :)

GTO
02-29-2016, 05:58 AM
Hi Cinema,

You are most welcome and glad that helped.

Mark