Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Copy value from next cell in a row if cell value is NaN

Hi

I have an excel sheet with cells mostly filled by numeric values. But
in each row some cells wont have a value but a "not available number"
(NaN) instead.

So what I'm looking for is a macro that checks if a cell contains a
NaN. If that's the case go one (or more) column(s) to the right in this
row until there is the next numeric value and copy/paste this value to
the cell where the NaN has been found in the first place. Do this for
all cells in the row, then jump one row down and start over again.

input:
320 300 NaN 310 NaN 400 380 NaN NaN 370
330 308 NaN 313 NaN 407 379 NaN NaN 378
....

desired output:
320 330 310 400 380 370
330 308 313 407 479 378
.....

thanks for your help
Stefan

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Copy value from next cell in a row if cell value is NaN

Try this (but make sure you have backed up you data!):

Sub removeNan()
With Worksheets("Sheet1")
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
lastcol = .Cells(r, Columns.Count).End(xlToLeft).Column
For col = lastcol To 1 Step -1
If .Cells(r, col) = "NaN" Then .Cells(r, col).Delete
Shift:=xlToLeft
Next col
Next r
End With
End Sub

HTH

" wrote:

Hi

I have an excel sheet with cells mostly filled by numeric values. But
in each row some cells wont have a value but a "not available number"
(NaN) instead.

So what I'm looking for is a macro that checks if a cell contains a
NaN. If that's the case go one (or more) column(s) to the right in this
row until there is the next numeric value and copy/paste this value to
the cell where the NaN has been found in the first place. Do this for
all cells in the row, then jump one row down and start over again.

input:
320 300 NaN 310 NaN 400 380 NaN NaN 370
330 308 NaN 313 NaN 407 379 NaN NaN 378
....

desired output:
320 330 310 400 380 370
330 308 313 407 479 378
.....

thanks for your help
Stefan


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Copy value from next cell in a row if cell value is NaN

Works great.
Thanks alot for your fast reply and the helpful code.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Copy value from next cell in a row if cell value is NaN

Thanks for feedback.

" wrote:

Hi

I have an excel sheet with cells mostly filled by numeric values. But
in each row some cells wont have a value but a "not available number"
(NaN) instead.

So what I'm looking for is a macro that checks if a cell contains a
NaN. If that's the case go one (or more) column(s) to the right in this
row until there is the next numeric value and copy/paste this value to
the cell where the NaN has been found in the first place. Do this for
all cells in the row, then jump one row down and start over again.

input:
320 300 NaN 310 NaN 400 380 NaN NaN 370
330 308 NaN 313 NaN 407 379 NaN NaN 378
....

desired output:
320 330 310 400 380 370
330 308 313 407 479 378
.....

thanks for your help
Stefan


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
Code to copy the formulae of one cell to all the cell in the rangewith the specific cell and columnnumber changing Options Yuvraj Excel Discussion (Misc queries) 0 June 29th 09 11:20 AM
Code to copy the formulae of one cell to all the cell in the rangewith the specific cell and columnnumber changing Yuvraj Excel Discussion (Misc queries) 0 June 26th 09 06:01 PM
How can I copy a value from a cell and paste it into another cell while adding it to the previous value in that cell [email protected] Excel Worksheet Functions 2 November 7th 07 09:39 AM
VBA to copy to empty cell directly below a cell when analogous cells in different column have same value as each other? Steven Rosenberg Excel Programming 0 August 5th 03 06:10 AM
VBA to copy to empty cell directly below a cell when analogous cells in different column have same value as each other? SROSENYC Excel Programming 1 August 5th 03 04:34 AM


All times are GMT +1. The time now is 09:35 AM.

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"