VBA Code Writing to Excel Box Name

M

Mike

Hi everyone,

Say you have a VBA code with FOR loop that solves and writes a
solution each time to a single row in a Name Box. So, after each
single solve you increase the line counter by 1 (INCREMENT=1) using
Aline=Aline+1. So if we have 5 solves, then Name Box has the first 5
rows full.

But, say a solution might take variable number of rows after each
solve to write without leaving blanks in-between inside the Name Box.
How do you make the INCREMENT variable then?

Thanks,
Mike
 
C

Cimjet

Hi Mike
I'm not sure what you want, maybe post your code and explain what you want to
change.
This will find the last row and do what you want.
Sub something()
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To finalrow
If Cells(i, 1).Value = "Something" Then
do something
End If
Next i
End Sub
HTH
Cimjet
 
M

Mike

Hi Mike
I'm not sure what you want, maybe post your code and explain what you want to
change.
This will find the last row and do what you want.
Sub something()
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To finalrow
If Cells(i, 1).Value = "Something" Then
do something
End If
Next i
End Sub
HTH








- Show quoted text -

Say I have a table of 10 lines with a Name Box "ABC" as follows:

1 .1 .3 .7
2 .2 .3 .5
3
4
5
6
7
8
9
10

Now I have a WHILE Loop in which a model is solved everytime and a row
or more get filled after each run. Because how many rows get filled
each time is unknown, I need to find away to automate this so always
the 1st blank row in "ABC" and that follow get filled.
 
A

Andrew

Say I have a table of 10 lines with a Name Box "ABC" as follows:

1   .1  .3  .7
2   .2  .3  .5
3
4
5
6
7
8
9
10

Now I have a WHILE Loop in which a model is solved everytime and a row
or more get filled after each run. Because how many rows get filled
each time is unknown, I need to find away to automate this so always
the 1st blank row in "ABC" and that follow get filled.

You can use a FOR loop for a known number of iterations. You can use
a While loop for an unknown number of iterations.

Example. I am trying to find the answer to X^3-x =5


Sub xxxxx()

x = -1000 ' starting value for X
E = 1000 ' starting value for the error in each guess
e_tol = 0.1 ' acceptable amount of error
leasterr = 10000

While Abs(E) > e_tol
x = x + 0.1
E = x ^ 3 - x - 5
If Abs(E) < leasterr Then
bestx = x
Ebest = E
End If
If x > 1000 Then GoTo errmessage
Wend
MsgBox ("Answer = " & x)
Exit Sub
errmessage: MsgBox ("No solution found. Best solution: x= " & bestx
& " error of " & Ebest)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top