View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Per Jessen[_2_] Per Jessen[_2_] is offline
external usenet poster
 
Posts: 703
Default Loop Overwriting

Hi

It should work if C-National sheet is the active sheet when you run
the macro , as you do not have set the sheet reference:

If c.Value < "" And Worksheets("C-National").Cells(c.Row, "Y").Value
= "" Then

or use a With statement to set the reference (note the leading dot):


dt = Application.InputBox _
("Please enter details (mmm-yyyy) for the data imported ")
With Worksheets("C-National")
lastRow = .Cells(Rows.Count, "B").End(xlUp).Row

For Each c In .Range("B3:B" & lastRow)
If c.Value < "" And .Cells(c.Row, "Y").Value = "" Then
.Cells(c.Row, "Y").Value = dt
End If
Next c
End With

Regards,
Per

On 4 Mar., 16:09, BoRed79 wrote:
Hi.

I have a piece of code which asks the user to enter details of the data that
is being imported and which then enters this date in the column Y of the
spreadsheet - next to those new data entries (see below):

dt = Application.InputBox("Please enter details (mmm-yyyy) for the data
imported")

lastRow = Worksheets("C-National").Cells(Rows.Count, "B").End(xlUp).Row

For Each c In Worksheets("C-National").Range("B3:B" & lastRow)
If c.Value < "" And Cells(c.Row, "Y").Value = "" Then
Worksheets("C-National").Cells(c.Row, "Y").Value = dt
End If
Next c

This works fine and enters the dates in a treat. *However, when the next
batch of data is imported (the next month) it not only putting in the date
for that new data, but is overwriting the dates for the previous month's
data. *

I thought that I had got round this by asking the macro to look in the cells
in Column Y and put in the data only if the cell was blank - but this does
not seem to be having the desired effect.

Could anyone advise how I might modify this code to get round this issue.

Many thanks in advance.

Liz.