Consulting

Results 1 to 6 of 6

Thread: Solved: Loop to Get and put values

  1. #1

    Solved: Loop to Get and put values

    I am using the following code to take the value in a variable and put information by looping.
    But there is certain some problems in looping so I am not getting the desired result.
    Can someone please help me to rectify the code.
    Also please note that I cant use formula as this is a part of my project where code has to do the job.

    [VBA]
    Sub nameandackn()
    Dim Fname As Boolean
    Fname = False
    qtr = 1
    For i = 1 To 9
    Range("a" & i).Select
    'Do
    ename = Range("A" & (10 + qtr)).Value
    If ActiveCell.Value = ename And ActiveCell.Offset(0, 2).Value = qtr Then
    ackn = ActiveCell.Offset(0, 3).Value
    Range("C" & (10 + qtr)).Value = ackn
    Else
    Do
    ActiveCell.Offset(1, 0).Select
    Loop Until (ActiveCell.Offset(1, 0).Value = ename And ActiveCell.Offset(0, 2).Value) Or ActiveCell.Offset(1, 0).Value = ""
    ackn = ActiveCell.Offset(0, 3).Value
    Range("C" & (10 + qtr)).Value = ackn
    End If
    qtr = qtr + 1
    'Loop Until ActiveCell.Offset(1, 0).Value = "" Or Fname = True
    Next i
    End Sub
    [/VBA]
    Edited by Simon Lloyd: Replaced Code tags with VBA tags
    A sample file is attached for ready reference.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Which sheet is supposed to be the sheet to work on?

    And have you people never heard of indenting your code, it makes life so much easier.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    Also please note that I cant use formula as this is a part of my project where code has to do the job.
    Be that as it may, that should not be a huge negitive issue here. In this case posting formulas may help you clarify your problem and help other readers solve your issue.

    For example, you can say, "I need to replicate this formula in code, etc."

  4. #4
    Thanks for the replies. In the mean time I solved the same with this code.

    Sub nameandackn1()
    Range("A1").Select
    'Range("C13:C16").Select
    Selection.ClearContents
    Srow_I = 13
    For i = Srow_I To 16
    ename = Range("a" & i).Value
    q = Range("B" & i).Value
        For j = 1 To 10
            Range("A" & j).Select
            ActiveCell.Offset(1, 0).Select
            If ActiveCell.Value = ename And ActiveCell.Offset(0, 2).Value = q Then
                akn = ActiveCell.Offset(0, 3).Value
               Range("a" & i).Offset(0, 2).Value = akn
            End If
        Next j
    Next i
    End Sub

  5. #5
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    [vba]
    Sub nameandackn1()
    Dim i, j As Integer, ename, q As Variant

    For i = 13 To 16
    ename = Cells(i, "A").Value
    q = Cells(i, "B").Value
    For j = 1 To 10
    If Range("A" & j) = ename And Range("C" & j) = q Then
    Range("C" & i).Value = Range("D" & j).Value
    End If
    Next j
    Next i
    End Sub
    [/vba]

  6. #6

Posting Permissions

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