Question: I am trying to set up a bit of code to automate a task I have to do on a weekly report I run. The task is to go into all the 'data' sheets and do several find and replaces so that other parts of the report that refer to the data sheets look a bit cleaner.

Currently my code looks similar to this:

```
sub FindReplaceSheets()
  dim nameofsheet as string
  nameofsheet = "ABC Data"
  call FindReplace (nameofsheet)
  nameofsheet = "DEF Data"
  call FindReplace (nameofsheet)
End Sub

sub FindReplace (x)
  Sheets.(x).select
  Cells.Replace What:="qwerty", Replacement:="asdfgh"
  Cells.Replace What:="zxcvb", Replacement:="mnbvc"
  Cells.Replace What:="poiuy", Replacement:="lkjhg"
End Sub

```

Whilst this works fine, I am sure it can be done more neatly. There are more sheets and more find/replace than listed above but not so much that the speed of the code is vital, I am wanting to just make it look neater and be easier to edit.

I have tried to edit this in two ways: firstly to have the sheet selection done via a For each statement but I cannot get it to work with something like

```
For each ws
 If right(ws.name, 4) = "Data"

```

Secondly, I have tried to edit the find to use an array where I define each find and replace string but cannot seem to get the right syntax.

I think a string array and a for each loop would fit what I am trying to achieve here but please advise on other methods if more appropriate.

Thanks in advance for any help.

Answer: 
> 
> will the select wait until all the inserts are done?
> 
> 
> 

Nope. Why would it? If the insert queries were issued before the SELECT query and it's a MyISAM table, then yes, you'll get the id of the last insert. That such queries are serialized are the reason that most people use InnoDB for OLTP.

> 
> Is There some difference if I use MAX or last\_insert\_id?
> 
> 
> 

Yes. last\_insert\_id wil return the last id generated by *your* session, while MAX(id) will return the *greatest* integer assigned by any session before your query was run - auto\_increment integers do not alwayts increase monotonically.

Unless you know exactly what you are doing, and are deliberately doing something very odd, last\_insert\_id will give you the answer you are looking for and will be more efficient.
If you use MAX, you will always get the greatest number in the database. That is like use a AUTO\_INCREMENT.

When you use "last\_insert\_id" you get the last insert id, so if you insert something in the id 4, but your max id is 10, you have to check if is something between 4 and 10.

I recommend you use "MAX" or configure it correctly the AUTO\_INCREMENT in the database