Question regarding Offset

getshorty

New Member
Joined
Jan 18, 2009
Messages
8
Hello folks,

im a VB novice but have taken on a project at work to put some VB code behind an excel spreadsheet. I hacked some code that I found on the web to try and do what I need.

I need the code to create a find box into which a search string is input. The code should find the string at a particular cell, then copies this cell and four cells to the right to a location in sheet 2 of the same workbook

The code runs but comes back with a runtime 424 error. I believe the issue is being caused by "nextCell" but I cant see why.

Any support would be appreciated

Sub FindStrings()
Dim firstCell, nextCell, stringToFind As String
' Show an input box and return the entry to a variable.
stringToFind = _
Application.InputBox("Enter J Number and Stage Number. For Example J1234 ST1", "Search String")
' Set an object variable to evaluate the Find command.
Set firstCell = Cells.Find(what:=stringToFind, lookat:=xlWhole, _
searchdirection:=xlPrevious)
' If the string is not found, show this message box.
If firstCell Is Nothing Then
MsgBox "Search Value Not Found.", vbExclamation
Else
' Otherwise, find the next occurrence of the search text.
nextCell = _
Cells.FindNext(after:=Range(firstCell.Address)).Address
' Show its address in a message box.
Range(nextCell, nextCell.Offset(0, 4)).Copy
Sheets("Sheet2").Select
Range("B3").Select
ActiveSheet.Paste

End If
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If you're going to do it like this, which I don't recommend, you need to treat your Offset as a String. E.g.,

MsgBox Range(Range("A1").Address).Offset(0, 4).Address

*Untested*

At a glance, you can't treat Strings like they're Objects.
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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