• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Need to understand this coding in details

Set rg = Cells(10, 3)
rsReason.MoveFirst
Do Until rsReason.EOF
rg.Value = rsReason("reason")
Set rg = rg.Offset(1, 0)
rsReason.MoveNext
Loop
rsReason.MoveFirst
Set rg = Cells(reason_count + 24, 3)
Do Until rsReason.EOF
rg.Value = rsReason("reason")
rg.Font.Bold = False
Set rg = rg.Offset(1, 0)
rsReason.MoveNext
Loop
 
Set rg = Cells(10, 3) 'Defines the rg range as being the cell at row 10, column 3 (aka C10)
rsReason.MoveFirst 'Go to the first record of dataset
Do Until rsReason.EOF 'Loop until we reach the end of the dataset
rg.Value = rsReason("reason") 'Set the value of rg (which is C10) to this value
Set rg = rg.Offset(1, 0) 'Redefine rg to now be the cell 1 below (aka, C11)
rsReason.MoveNext 'go to next record in dataset
Loop
rsReason.MoveFirst 'Same as above
Set rg = Cells(reason_count + 24, 3) 'Same as above, but with a variable
Do Until rsReason.EOF 'Same as above
rg.Value = rsReason("reason") 'same as above
rg.Font.Bold = False 'Make sure the font is not bold
Set rg = rg.Offset(1, 0) 'Same as above
rsReason.MoveNext 'Same as above
Loop
 
as a side note, this is not the best written code as it performs a loop twice. A more efficient way to write those two loops would have been:
Code:
i = 0 'Setup a counter
 
rsReason.MoveFirst
Do Until rsReason.EOF
    'First cell that needs changed. Our counter helps us move
    'to each new cell as we loop
    Cells(10 + i, 3).Value = rsReason("reason")
    
    'Second cell that we change. Again, using a counter
    With Cells(reason_count + 24 + i, 3)
        'Need to make 2 changes to this cell
        .Value = rsReason("reason")
        .Font.Bold = False
    End With
    
    'Increment the counter
    i = i + 1
    rsReason.MoveNext
Loop
 
as a side note, this is not the best written code as it performs a loop twice. A more efficient way to write those two loops would have been:
Code:
i = 0 'Setup a counter
 
rsReason.MoveFirst
Do Until rsReason.EOF
    'First cell that needs changed. Our counter helps us move
    'to each new cell as we loop
    Cells(10 + i, 3).Value = rsReason("reason")
   
    'Second cell that we change. Again, using a counter
    With Cells(reason_count + 24 + i, 3)
        'Need to make 2 changes to this cell
        .Value = rsReason("reason")
        .Font.Bold = False
    End With
   
    'Increment the counter
    i = i + 1
    rsReason.MoveNext
Loop

Thanks
 
Back
Top