Confused: Same code - different results

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
I’m attempting to assemble a ws with several columns of data.
Macro1 works just fine.
Except for the column & iRow cell references, Macro2 appears to me to be the same code. However, it replaces the existing data.
Any ideas on how can this be resolved?

Rich (BB code):
SubMacro1()
Dim FullName As String
    Dim iRow As Long
    Dim ws As Worksheet

Set ws =Worksheets("Teams")
ws.Select
ws.Unprotect

    FullName = UserFormPostScores.ComboBox1

    With ws
        If UserFormPostScores.TextBox85.Value =1 Then

    FullName =UserFormPostScores.ComboBox1.Value
    Set CLoc =ws.Columns("B:B").Find(What:=FullName, after:=ws.Cells(1, 2),LookIn:= _
                            xlFormulas,LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:= _
                            xlNext,MatchCase:=False, SearchFormat:=False)

    If CLoc Is Nothing Then
        iRow = ws.Cells(Rows.Count, 1) _
               .End(xlUp).Offset(1, 0).Row
    Else

        iRow = CLoc.Row
    End If

    ws.Cells(iRow, 1).Formula ="=Rand()"
    ws.Cells(iRow, 2).Value =UserFormPostScores.ComboBox1
    ws.Cells(iRow, 3).Value =UserFormPostScores.TextBox90
    ws.Cells(iRow, 4).Value =UserFormPostScores.TextBox36.Value
    ws.Cells(iRow, 5).Value =UserFormPostScores.TextBox24.Value - UserFormPostScores.TextBox3.Value
Else
 End If
  End With

CallMacroBuildTeams2
End Sub
__________________________________

Sub Macro2()

Dim FullNameAs String
    Dim iRow As Long
    Dim ws As Worksheet

Set ws =Worksheets("Teams")
ws.Select
ws.Unprotect

    FullName = UserFormPostScores.ComboBox1

    With ws
        If UserFormPostScores.TextBox85.Value =2 Then

    FullName =UserFormPostScores.ComboBox1.Value

    Set CLoc =ws.Columns("G:G").Find(What:=FullName, after:=ws.Cells(1, 7),LookIn:= _
                            xlFormulas, LookAt:=xlWhole,SearchOrder:=xlByColumns, SearchDirection:= _
                            xlNext,MatchCase:=False, SearchFormat:=False)

    If CLoc Is Nothing Then
        iRow = ws.Cells(Rows.Count, 1) _
               .End(xlUp).Offset(1, 0).Row
    Else

        iRow = CLoc.Row
    End If

    ws.Cells(iRow, 6).Formula ="=Rand()"
    ws.Cells(iRow, 7).Value =UserFormPostScores.ComboBox1
    ws.Cells(iRow, 8).Value =UserFormPostScores.TextBox90
    ws.Cells(iRow, 9).Value =UserFormPostScores.TextBox36.Value
    ws.Cells(iRow, 10).Value =UserFormPostScores.TextBox24.Value - UserFormPostScores.TextBox3.Value
Else
 End If
  End With
End Sub

 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
What is the value of CLoc.Row when it writes to the wrong row?
 
Upvote 0
CLoc.Row values are always correct.
Only Macro1 works correctly:
It starts at cell[A2] inputting the random number formula,
[B2]=
ComboBox1, [C2]=Textbox90, [D2]=Textbox36, etc.
Added values fall in place correctly, AND, duplicate ComboBox1 values are prevented.

Macro2 puts all data in the correct columns but only on Row3, AND, subsequent entries replace the existing values. It's a mystery...to me!
 
Upvote 0
Macro2 puts all data in the correct columns but only on Row3, AND, subsequent entries replace the existing values. It's a mystery...to me!
That's why I was asking what the value of CLoc.Row was. It sounds as though the "FullName" value is only being found on row3 & hence being overwritten
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top