+ Reply to Thread
Results 1 to 2 of 2

please help with Loops

  1. #1
    jhahes
    Guest

    please help with Loops

    I am very new at vba programming, and I will try to make this simple to understand.


    I have 2 sheets in a workbook right now.

    One is a daily log and one is for invoicing.

    Right now what I am trying to do is this.

    Someone hits a command button in the invoicing page

    and it searches through Column D or a unique key field of the daily log page to find a match to the name on the invoicing page.

    Then when it finds a match the code is suppose to do this.

    Here is my current code

    Dim x as string

    x = Sheet2(Invoicing).range("Agent").value

    Sheet1.Range("D4").select

    If activecell = x then

    Sheet2.range("A19").value = activecell.offset(0,1)
    Sheet2.range("B19)").value = activecell.offset(0,2)


    My question is how do I get the code to the find the next time that name appears on the daily log sheet. Say the agent has 10 orders. Also, when the code runs it needs to put the next values 2 rows or lines down in the Invoicing page.

    So If agent abc is on line D4 of Sheet1 it would print the value in a19 of sheet 2 and the 2nd value in cell b19. Then the code would look for the next time that name would appear say D10 then put that value in a21 and b21, and so on a23 and b23 for next appearance.

    I would appreciate any help.

    Thanks

  2. #2
    Forum Contributor
    Join Date
    11-09-2004
    Posts
    451
    Sub Macro1()
    Dim init, i As Variant
    Dim r As Variant
    Columns("d:d").Select
    On Error GoTo a:
    Selection.Find(What:=Range("Agent").Value, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False).Activate

    init = ActiveCell.Address
    r = 19
    Range("a" & r).Value = ActiveCell.Offset(0, 1).Value
    Range("b" & r).Value = ActiveCell.Offset(0, 1).Value

    While i = 0

    Selection.FindNext(After:=ActiveCell).Activate

    If ActiveCell.Address = init Then
    End
    Else
    r = r + 1
    Range("a" & r).Value = ActiveCell.Offset(0, 1).Value
    Range("b" & r).Value = ActiveCell.Offset(0, 1).Value
    End If
    Wend
    a:
    End Sub

+ 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