ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping (https://www.excelbanter.com/excel-programming/362436-looping.html)

Baffee

Looping
 
I have the following code in a macro:

For Emp = 8 To 63
Application.Goto Reference:="Name"
ActiveCell.Replace What:="Emp", Replacement:="Emp+1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Find(What:="Emp", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate
Application.Goto Reference:="Wages"
Selection.Replace What:="Emp", Replacement:="Emp+1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.Goto Reference:="Benefits"
Selection.Replace What:="Emp", Replacement:="Emp=+", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.Goto Reference:="Hours"
Selection.Replace What:="Emp", Replacement:="Emp+1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.Goto Reference:="Rate"
Selection.Replace What:="Emp", Replacement:="Emp+1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Next Emp

The value of Emp changes when I do a step through the macro, but hte value
of Emp and Emp + 1 never changes to the next incremental value. How can I
get this to loop through correctly?
Thanks!

Ivan Raiminius

Looping
 
Hi,

if you want to replace value Emp, then remove quotation marks (so "Emp"
should be Emp in your code). Emp is variable, "Emp" is string.

Regards,
Ivan


colofnature[_32_]

Looping
 

Maybe I'm being dense, but I'm not really sure what you're trying to do
here - could you give some detail?


--
colofnature
------------------------------------------------------------------------
colofnature's Profile: http://www.excelforum.com/member.php...o&userid=34356
View this thread: http://www.excelforum.com/showthread...hreadid=545545


Don Guillett

Looping
 
I'm not quite sure what you want to do but try this idea. It goes to each
named cell and changes emp1 to emp2

Sub loopemp()
myarray = Array("name", "benefits", "wages")
On Error Resume Next

For Each i In myarray
Application.Goto i
ActiveCell.Replace "emp1", "emp2"
Next i

End Sub

if you are trying to find all cells with emp then try using FINDNEXT. there
is a good example in vba help index.

--
Don Guillett
SalesAid Software

"Baffee" wrote in message
...
I have the following code in a macro:

For Emp = 8 To 63
Application.Goto Reference:="Name"
ActiveCell.Replace What:="Emp", Replacement:="Emp+1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Find(What:="Emp", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=
_
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate
Application.Goto Reference:="Wages"
Selection.Replace What:="Emp", Replacement:="Emp+1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.Goto Reference:="Benefits"
Selection.Replace What:="Emp", Replacement:="Emp=+", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.Goto Reference:="Hours"
Selection.Replace What:="Emp", Replacement:="Emp+1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.Goto Reference:="Rate"
Selection.Replace What:="Emp", Replacement:="Emp+1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Next Emp

The value of Emp changes when I do a step through the macro, but hte value
of Emp and Emp + 1 never changes to the next incremental value. How can
I
get this to loop through correctly?
Thanks!




Baffee

Looping
 
I knew I was close to getting the results I wanted. I thought maybe I had to
declate the variables or something. What an easy solution. This worked
PERFECTLY.
Thanks!!

"Ivan Raiminius" wrote:

Hi,

if you want to replace value Emp, then remove quotation marks (so "Emp"
should be Emp in your code). Emp is variable, "Emp" is string.

Regards,
Ivan




All times are GMT +1. The time now is 03:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com