Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Copy value until next value

Hi,

I am trying to write a code that will copy the contents of a cell - a month
- into all subsequent empty cells, unless another month is encountered. I
then want this new month to be copied into subsequent empty cells, until
another month is encountered. And so on. This will occur in one column only.

Reason: I have a spreadsheet that varies in size and format apart from one
column. The column contains one reference only per month's worth of line
items that follow. The line items do not contain any reference to the month
that they relate to. I am trying to write a code that will copy the only
reference of the month into every row that contains a line item for that
month, and repeat this for however many months there may be.

This is what I have so far, but it does not work:

Sub Month_copy

Dim CURRPERIOD As String
Dim CELL As Range, RNG As Range
With ActiveSheet
Set RNG = .Range(.Range("A1:a10000"), .Range("A1:A10000").End(xlDown))
End With

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
End sub

What I am trying to say is if the cell is blank, paste in the current month
value and move onto the next. If it is not blank, it must be a month
reference, therefore copy this into the subsequent blank cells.

Thanks in advance for any help received. (Hope this makes sense)

Ewan.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Copy value until next value

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
This isn't working because you're setting CURRPERIOD to active cell and
then you check active cell try this:

'assuming the first active cell has a value
CURRPERIOD = ActiveCell.Value
range(next cell).select
For each CELL in RNG
If ActiveCell = CURRPER1 then
range(next cell).select
Elseif ActiveCell = "" then
Activecell = CURRPERIOD
range(next cell).select
Else
CURRPERIOD = ActiveCell.Value
range(next cell).select
End If
Next

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Copy value until next value

Glen,

Sorry, but this does not compile. Is "range(next cell).select" literal?

"Glen" wrote:

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
This isn't working because you're setting CURRPERIOD to active cell and
then you check active cell try this:

'assuming the first active cell has a value
CURRPERIOD = ActiveCell.Value
range(next cell).select
For each CELL in RNG
If ActiveCell = CURRPER1 then
range(next cell).select
Elseif ActiveCell = "" then
Activecell = CURRPERIOD
range(next cell).select
Else
CURRPERIOD = ActiveCell.Value
range(next cell).select
End If
Next


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Copy value until next value

no - you have to name whatever the next cell is. I am sorry for that.
The format should be Range("A1").select if A! is the cell you want.
You can also name it dynamically like this
dim cellnum as integer
cellnum = 1
Range("A" & cellnum & "").select

HTH

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Copy value until next value

Sorry Glen, I don't understand.

I'm trying to make each empty cell in the range A1:A10000 the value of the
variable CURRPERIOD. I thought the For Each loop would cycle through all
cells in the range and change their value to CURRPERIOD, with CURRPERIOD only
changing if a non-blank cell is activated i.e. if the cell contains a month?

"Glen" wrote:

no - you have to name whatever the next cell is. I am sorry for that.
The format should be Range("A1").select if A! is the cell you want.
You can also name it dynamically like this
dim cellnum as integer
cellnum = 1
Range("A" & cellnum & "").select

HTH




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy value until next value


I don understand how you are setting RNG. But once that is done yo
could just as well use good old fassion Offset(-1, 0).Value

For Each CELL In RNG
If CELL.Value = "" Then CELL.Value = CELL.Offset(-1, 0).Value
Next

Or something like:

Sub Month_copy()

Dim CURRPERIOD As String
Dim CELL As Range, StartCell As Range, RNG As Range
With ActiveSheet
If IsEmpty(.Range("A1")) Then
Set StartCell = .Range("A1").End(xlDown)
Else
Set StartCell = .Range("A1")
End If
Set RNG = .Range(StartCell, .Range("A10000").End(xlUp))
End With
'MsgBox (RNG.Address)

For Each CELL In RNG
If CELL.Value = "" Then CELL.Value = CELL.Offset(-1, 0).Value
Next

End Su

--
Pointles
-----------------------------------------------------------------------
Pointless's Profile: http://www.excelforum.com/member.php...fo&userid=3086
View this thread: http://www.excelforum.com/showthread.php?threadid=50679

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Copy value until next value

Very good point "pointless" - isn't that ironic.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Copy value until next value

Ewan,

Don't loop:

Sub Month_copy2()

With Range("A:A")
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Copy
.PasteSpecial xlPasteValues
End With

End sub

HTH,
Bernie
MS Excel MVP


"ewan7279" wrote in message
...
Hi,

I am trying to write a code that will copy the contents of a cell - a month
- into all subsequent empty cells, unless another month is encountered. I
then want this new month to be copied into subsequent empty cells, until
another month is encountered. And so on. This will occur in one column only.

Reason: I have a spreadsheet that varies in size and format apart from one
column. The column contains one reference only per month's worth of line
items that follow. The line items do not contain any reference to the month
that they relate to. I am trying to write a code that will copy the only
reference of the month into every row that contains a line item for that
month, and repeat this for however many months there may be.

This is what I have so far, but it does not work:

Sub Month_copy

Dim CURRPERIOD As String
Dim CELL As Range, RNG As Range
With ActiveSheet
Set RNG = .Range(.Range("A1:a10000"), .Range("A1:A10000").End(xlDown))
End With

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
End sub

What I am trying to say is if the cell is blank, paste in the current month
value and move onto the next. If it is not blank, it must be a month
reference, therefore copy this into the subsequent blank cells.

Thanks in advance for any help received. (Hope this makes sense)

Ewan.



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Copy value until next value

Thanks Bernie,

The code only copies up to the next month, but not the last month itself
i.e. April copies up to May, May up to June, but June does not copy any
further. I need the month to copy down for every subsequent line item. Can
you help please?

"Bernie Deitrick" wrote:

Ewan,

Don't loop:

Sub Month_copy2()

With Range("A:A")
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Copy
.PasteSpecial xlPasteValues
End With

End sub

HTH,
Bernie
MS Excel MVP


"ewan7279" wrote in message
...
Hi,

I am trying to write a code that will copy the contents of a cell - a month
- into all subsequent empty cells, unless another month is encountered. I
then want this new month to be copied into subsequent empty cells, until
another month is encountered. And so on. This will occur in one column only.

Reason: I have a spreadsheet that varies in size and format apart from one
column. The column contains one reference only per month's worth of line
items that follow. The line items do not contain any reference to the month
that they relate to. I am trying to write a code that will copy the only
reference of the month into every row that contains a line item for that
month, and repeat this for however many months there may be.

This is what I have so far, but it does not work:

Sub Month_copy

Dim CURRPERIOD As String
Dim CELL As Range, RNG As Range
With ActiveSheet
Set RNG = .Range(.Range("A1:a10000"), .Range("A1:A10000").End(xlDown))
End With

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
End sub

What I am trying to say is if the cell is blank, paste in the current month
value and move onto the next. If it is not blank, it must be a month
reference, therefore copy this into the subsequent blank cells.

Thanks in advance for any help received. (Hope this makes sense)

Ewan.




  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Copy value until next value

If it needs to match the data in column B, then you could use

With Range("A2:A" &Range("B65536").End(xlUp).Row)
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Copy
.PasteSpecial xlPasteValues
End With

HTH,
Bernie
MS Excel MVP


"ewan7279" wrote in message
...
Thanks Bernie,

The code only copies up to the next month, but not the last month itself
i.e. April copies up to May, May up to June, but June does not copy any
further. I need the month to copy down for every subsequent line item. Can
you help please?

"Bernie Deitrick" wrote:

Ewan,

Don't loop:

Sub Month_copy2()

With Range("A:A")
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Copy
.PasteSpecial xlPasteValues
End With

End sub

HTH,
Bernie
MS Excel MVP


"ewan7279" wrote in message
...
Hi,

I am trying to write a code that will copy the contents of a cell - a month
- into all subsequent empty cells, unless another month is encountered. I
then want this new month to be copied into subsequent empty cells, until
another month is encountered. And so on. This will occur in one column only.

Reason: I have a spreadsheet that varies in size and format apart from one
column. The column contains one reference only per month's worth of line
items that follow. The line items do not contain any reference to the month
that they relate to. I am trying to write a code that will copy the only
reference of the month into every row that contains a line item for that
month, and repeat this for however many months there may be.

This is what I have so far, but it does not work:

Sub Month_copy

Dim CURRPERIOD As String
Dim CELL As Range, RNG As Range
With ActiveSheet
Set RNG = .Range(.Range("A1:a10000"), .Range("A1:A10000").End(xlDown))
End With

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
End sub

What I am trying to say is if the cell is blank, paste in the current month
value and move onto the next. If it is not blank, it must be a month
reference, therefore copy this into the subsequent blank cells.

Thanks in advance for any help received. (Hope this makes sense)

Ewan.








  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Copy value until next value

The only reason it would do what you describe is if you are testing it on a
dummy sheet you set up for testing and there is no data beyond the row that
contains June. If you put data in any other column that extends beyond the
row with June, then this will fill down to that row.

--
Regards,
Tom Ogilvy


"ewan7279" wrote in message
...
Thanks Bernie,

The code only copies up to the next month, but not the last month itself
i.e. April copies up to May, May up to June, but June does not copy any
further. I need the month to copy down for every subsequent line item.

Can
you help please?

"Bernie Deitrick" wrote:

Ewan,

Don't loop:

Sub Month_copy2()

With Range("A:A")
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Copy
.PasteSpecial xlPasteValues
End With

End sub

HTH,
Bernie
MS Excel MVP


"ewan7279" wrote in message
...
Hi,

I am trying to write a code that will copy the contents of a cell - a

month
- into all subsequent empty cells, unless another month is

encountered. I
then want this new month to be copied into subsequent empty cells,

until
another month is encountered. And so on. This will occur in one

column only.

Reason: I have a spreadsheet that varies in size and format apart from

one
column. The column contains one reference only per month's worth of

line
items that follow. The line items do not contain any reference to the

month
that they relate to. I am trying to write a code that will copy the

only
reference of the month into every row that contains a line item for

that
month, and repeat this for however many months there may be.

This is what I have so far, but it does not work:

Sub Month_copy

Dim CURRPERIOD As String
Dim CELL As Range, RNG As Range
With ActiveSheet
Set RNG = .Range(.Range("A1:a10000"),

..Range("A1:A10000").End(xlDown))
End With

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
End sub

What I am trying to say is if the cell is blank, paste in the current

month
value and move onto the next. If it is not blank, it must be a month
reference, therefore copy this into the subsequent blank cells.

Thanks in advance for any help received. (Hope this makes sense)

Ewan.






  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Copy value until next value

Hi Tom - you were right about me using a test sheet.

I'm not really sure what the macro that Bernie described does i.e. how it
works, but I've just tried it on 'live' data and it doesn't work at all, even
to the same level as on the test sheet. I'm really stuck...

"Tom Ogilvy" wrote:

The only reason it would do what you describe is if you are testing it on a
dummy sheet you set up for testing and there is no data beyond the row that
contains June. If you put data in any other column that extends beyond the
row with June, then this will fill down to that row.

--
Regards,
Tom Ogilvy


"ewan7279" wrote in message
...
Thanks Bernie,

The code only copies up to the next month, but not the last month itself
i.e. April copies up to May, May up to June, but June does not copy any
further. I need the month to copy down for every subsequent line item.

Can
you help please?

"Bernie Deitrick" wrote:

Ewan,

Don't loop:

Sub Month_copy2()

With Range("A:A")
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Copy
.PasteSpecial xlPasteValues
End With

End sub

HTH,
Bernie
MS Excel MVP


"ewan7279" wrote in message
...
Hi,

I am trying to write a code that will copy the contents of a cell - a

month
- into all subsequent empty cells, unless another month is

encountered. I
then want this new month to be copied into subsequent empty cells,

until
another month is encountered. And so on. This will occur in one

column only.

Reason: I have a spreadsheet that varies in size and format apart from

one
column. The column contains one reference only per month's worth of

line
items that follow. The line items do not contain any reference to the

month
that they relate to. I am trying to write a code that will copy the

only
reference of the month into every row that contains a line item for

that
month, and repeat this for however many months there may be.

This is what I have so far, but it does not work:

Sub Month_copy

Dim CURRPERIOD As String
Dim CELL As Range, RNG As Range
With ActiveSheet
Set RNG = .Range(.Range("A1:a10000"),

..Range("A1:A10000").End(xlDown))
End With

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
End sub

What I am trying to say is if the cell is blank, paste in the current

month
value and move onto the next. If it is not blank, it must be a month
reference, therefore copy this into the subsequent blank cells.

Thanks in advance for any help received. (Hope this makes sense)

Ewan.






  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Copy value until next value

I have just cleared the contents of some of the apparently blank cells in the
live data and the macro now works. Any ideas why this would be please? It
would appear that I now need to figure out how to clear the contents of every
apparently blank cell in the column before copying the months into them, so I
am faced with the same original problem...

"ewan7279" wrote:

Hi Tom - you were right about me using a test sheet.

I'm not really sure what the macro that Bernie described does i.e. how it
works, but I've just tried it on 'live' data and it doesn't work at all, even
to the same level as on the test sheet. I'm really stuck...

"Tom Ogilvy" wrote:

The only reason it would do what you describe is if you are testing it on a
dummy sheet you set up for testing and there is no data beyond the row that
contains June. If you put data in any other column that extends beyond the
row with June, then this will fill down to that row.

--
Regards,
Tom Ogilvy


"ewan7279" wrote in message
...
Thanks Bernie,

The code only copies up to the next month, but not the last month itself
i.e. April copies up to May, May up to June, but June does not copy any
further. I need the month to copy down for every subsequent line item.

Can
you help please?

"Bernie Deitrick" wrote:

Ewan,

Don't loop:

Sub Month_copy2()

With Range("A:A")
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Copy
.PasteSpecial xlPasteValues
End With

End sub

HTH,
Bernie
MS Excel MVP


"ewan7279" wrote in message
...
Hi,

I am trying to write a code that will copy the contents of a cell - a

month
- into all subsequent empty cells, unless another month is

encountered. I
then want this new month to be copied into subsequent empty cells,

until
another month is encountered. And so on. This will occur in one

column only.

Reason: I have a spreadsheet that varies in size and format apart from

one
column. The column contains one reference only per month's worth of

line
items that follow. The line items do not contain any reference to the

month
that they relate to. I am trying to write a code that will copy the

only
reference of the month into every row that contains a line item for

that
month, and repeat this for however many months there may be.

This is what I have so far, but it does not work:

Sub Month_copy

Dim CURRPERIOD As String
Dim CELL As Range, RNG As Range
With ActiveSheet
Set RNG = .Range(.Range("A1:a10000"),

..Range("A1:A10000").End(xlDown))
End With

For Each CELL In RNG
CURRPERIOD = ActiveCell.Value
If ActiveCell = "" Then
ActiveCell.Value = CURRPERIOD
Else
CURRPERIOD = ActiveCell.Value
End If
Next
End sub

What I am trying to say is if the cell is blank, paste in the current

month
value and move onto the next. If it is not blank, it must be a month
reference, therefore copy this into the subsequent blank cells.

Thanks in advance for any help received. (Hope this makes sense)

Ewan.






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy and paste versus copy and insert copied cells Alana New Users to Excel 1 September 28th 07 08:58 PM
Copy/Paste how to avoid the copy of formula cells w/o calc values Dennis Excel Discussion (Misc queries) 10 March 2nd 06 10:47 PM
EXCEL FILE a copy/a copy/a copy ....filename ve New Users to Excel 1 September 29th 05 09:12 PM
Code to copy range vs Copy Entire Worksheet - can't figure it out Mike Taylor Excel Programming 1 April 15th 04 08:34 PM
How copy format, font, color and border without copy/paste? Michel[_3_] Excel Programming 1 November 5th 03 04:43 PM


All times are GMT +1. The time now is 11:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"