+ Reply to Thread
Results 1 to 5 of 5

Solution:Looping

  1. #1
    Baffee
    Guest

    Solution: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!

  2. #2
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Maybe I'm being dense, but I'm not really sure what you're trying to do here - could you give some detail?

  3. #3
    Ivan Raiminius
    Guest

    re: Solution: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


  4. #4
    Don Guillett
    Guest

    re: Solution: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
    [email protected]
    "Baffee" <[email protected]> wrote in message
    news:[email protected]...
    >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!




  5. #5
    Baffee
    Guest

    re: Solution: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
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1