Link to home
Start Free TrialLog in
Avatar of ca1358
ca1358

asked on

Excel Loop VBA

How would I make a loop in Excel Vba.
The data is
        Col              A           B                  C                D
 Row15             4.5       100          10000000      
 Row 16
 Row 17           5.5        100         100000000

Then to say if C16 is not null then
D16 = CommitNumber.TextBox1.Value + 1 but if
D16 is null then it would check
D17 = CommitNumber.TextBox1.Value + 1

Any help would greatly be apprecatied>


'////////////////////////////////////////////
Private Sub CommandButton1_Click()

If Not IsNull(ActiveSheet.Range("C15")) Then
    ActiveSheet.Range("D15").Select
         ActiveSheet.Range("D15") = CommitNumber.TextBox1.Value
Else
End If

End Sub
Avatar of Badotz
Badotz
Flag of United States of America image

If Range("C16") <> Null Then
    If Range("D16") <> Null Then
        Range("D16") = CommitNumber.TextBox1.Value + 1
    Else
        Range("D17") = CommitNumber.TextBox1.Value + 1
    End If
End If
What is CommitNumber?
Avatar of ca1358
ca1358

ASKER

CommitNumber is this module which is the source for the text box on the User Form.

Is there not a way to loop this?  This will have to go throught Column C about 30 rows checking to see if it is not null add 1 to CommitNumber.  The example of the commitNumber would be 5000 then the next one would be 5001 if Column C is not null.

Thank you both for looking.
'//////////////////////////////////////////////////
Public Conn As ADODB.Connection
Public rsCommit As ADODB.Recordset
Public rs2NewLastCommitNumber As ADODB.Recordset

Public Sub OpenADOCommitNumber()
    Dim dbpath As String
   
    'With Password
   dbpath = "Data Source=\\Volume.mdb;Jet OLEDB:Database Password=mandatory;"

     
   
    Set Conn = New ADODB.Connection
    With Conn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Open dbpath
    End With
   
        Set rsCommit = New ADODB.Recordset
    With rsCommit
        .Open UCase(fOSUserName) & "Commit", Conn, adOpenDynamic, adLockOptimistic, adCmdTable
        CommitNumber.TextBox1.Value = .Fields(0).Value + 1
       
    End With
   
    Set rs2NewLastCommitNumber = New ADODB.Recordset
    With rs2NewLastCommitNumber
        .Open UCase(fOSUserName) & "NewLastCommitNumber", Conn, adOpenDynamic, adLockOptimistic, adCmdTable
        CommitNumber.TextBox2.Value = .Fields(0).Value

   
     
    End With
   
End Sub
What does the ADO code shown above have to do with the loop you are wanting? Are we to loop through the rows in the Recordset and update the spreadsheet?

I need a bit more info in order to proceed, sorry.
Avatar of ca1358

ASKER

The commitNumber is only populated once.  Then I need the code to just add one to that numbe(5000)r if Column C is not null(5001), then go to the next, if Column C is not null add 1(5002) but if not skip that number and go to the next.  

     Col              A           B                  C                D
 Row15             4.5       100          10000000       5000
 Row 16
 Row 17           5.5        100         100000000     5001
 Row 18           5.5        100         100000000     5002
 Row 19
 Row 20           5.5        100         100000000     5003
etc...





ASKER CERTIFIED SOLUTION
Avatar of Badotz
Badotz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No worries - glad to help.