PC Review


Reply
Thread Tools Rate Thread

Add number to a list

 
 
Steve
Guest
Posts: n/a
 
      11th Dec 2009
Hi,

I am trying to write a macro that when I run it what it does is it looks at
Cell A1 and see's if it is blank. If it is blank it then inputs "-3" and
stops. Then next time I run it, it will look at A1 see it is not blank and
move to A2 input "-3" and stop. And it will continue to add a "-3" each time
I run the macro.

The code I currently have written is below. The problem is however, that it
does not stop. It keeps inputing "-3" down the entire Col A. Please help!

Thanks,

Steve

Sub LastRow()
Dim Rng As Range
For Each Rng In Range("A:a")
Range("A1").Select
If Not Rng.Value = "-3" Then
Rng.Select
ActiveCell.FormulaR1C1 = "-3"
End If
Next Rng
End Sub

 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      11th Dec 2009
Steve,

Don't loop - just find the last cell and check its value:

Sub LastRow()
Dim Rng As Range
Set Rng = Cells(Rows.Count, 1).End(xlUp)
If Rng.Value = "-3" Then Rng.Offset(1, 0).Value = "-3"
If Rng.Value = "" Then Rng.Value = "-3"
End Sub


HTH,
Bernie
MS Excel MVP


"Steve" <(E-Mail Removed)> wrote in message
news:317572C8-D29C-41E9-B595-(E-Mail Removed)...
> Hi,
>
> I am trying to write a macro that when I run it what it does is it looks at
> Cell A1 and see's if it is blank. If it is blank it then inputs "-3" and
> stops. Then next time I run it, it will look at A1 see it is not blank and
> move to A2 input "-3" and stop. And it will continue to add a "-3" each time
> I run the macro.
>
> The code I currently have written is below. The problem is however, that it
> does not stop. It keeps inputing "-3" down the entire Col A. Please help!
>
> Thanks,
>
> Steve
>
> Sub LastRow()
> Dim Rng As Range
> For Each Rng In Range("A:a")
> Range("A1").Select
> If Not Rng.Value = "-3" Then
> Rng.Select
> ActiveCell.FormulaR1C1 = "-3"
> End If
> Next Rng
> End Sub
>



 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      11th Dec 2009
Steve,

Try this

Sub LastRow()
If Range("A1").Value = "" Then
Range("A1").Value = -3
Else
Cells(Cells(Cells.Rows.Count, "A") _
.End(xlUp).Row + 1, 1).Value = -3
End If

End Sub

Mike

"Steve" wrote:

> Hi,
>
> I am trying to write a macro that when I run it what it does is it looks at
> Cell A1 and see's if it is blank. If it is blank it then inputs "-3" and
> stops. Then next time I run it, it will look at A1 see it is not blank and
> move to A2 input "-3" and stop. And it will continue to add a "-3" each time
> I run the macro.
>
> The code I currently have written is below. The problem is however, that it
> does not stop. It keeps inputing "-3" down the entire Col A. Please help!
>
> Thanks,
>
> Steve
>
> Sub LastRow()
> Dim Rng As Range
> For Each Rng In Range("A:a")
> Range("A1").Select
> If Not Rng.Value = "-3" Then
> Rng.Select
> ActiveCell.FormulaR1C1 = "-3"
> End If
> Next Rng
> End Sub
>

 
Reply With Quote
 
john
Guest
Posts: n/a
 
      11th Dec 2009
see if this does what you want:


Sub LastRow()
Dim Rng As Range

For Each Rng In Range("A:A")

If Not Rng.Value = -3 Then

Rng.Value = -3

Exit For

End If

Next Rng

End Sub
--
jb


"Steve" wrote:

> Hi,
>
> I am trying to write a macro that when I run it what it does is it looks at
> Cell A1 and see's if it is blank. If it is blank it then inputs "-3" and
> stops. Then next time I run it, it will look at A1 see it is not blank and
> move to A2 input "-3" and stop. And it will continue to add a "-3" each time
> I run the macro.
>
> The code I currently have written is below. The problem is however, that it
> does not stop. It keeps inputing "-3" down the entire Col A. Please help!
>
> Thanks,
>
> Steve
>
> Sub LastRow()
> Dim Rng As Range
> For Each Rng In Range("A:a")
> Range("A1").Select
> If Not Rng.Value = "-3" Then
> Rng.Select
> ActiveCell.FormulaR1C1 = "-3"
> End If
> Next Rng
> End Sub
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word 2003 TOC list chapter name, number, and page number ANATAON Microsoft Word Document Management 1 4th Feb 2009 08:32 PM
First Number in Cross Referenced Number List Won't Update =?Utf-8?B?VG9kZCBNLiBUYXlsb3I=?= Microsoft Word Document Management 3 28th Sep 2007 06:08 PM
setting number of lines in List-box -->LookUp-->Table/Query drop-down list aa Microsoft Access 2 21st Jan 2007 01:22 PM
How do i set up a list that sorts as Number/letter/number in orde =?Utf-8?B?eG9yZXg=?= Microsoft Excel Misc 3 8th Sep 2006 12:59 AM
command excel macros to convert a list 5 number number combinati. =?Utf-8?B?ZXhjYWxpYnVy?= Microsoft Access Macros 0 7th Dec 2004 04:55 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:54 AM.