Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Replacing "error msg" in VBa

I have a lot of cells returning #N/A! as a result of failed VLOOKUP's.
I would like to change all of these to "blanks", using

Range("A1:B70").Copy
Range("A1:B70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

'replace "#N/A" with "replword"
Cells.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

The first part transforms the erro codes into a value, displayed as #N/A
without the exclamationmark from the code. The latter part does absolutely
nothing, but leaves the cells as is.
When i use this code on any of the cells,

Sub t()
Dim a As Variant
a = ActiveCell.Value
Debug.Print a
End Sub

Ir returns Error 2042. If I try to replace "#N/A" in the replace statement
with "Erro 2042" still nothing happens as Error 2042 apparently is not a
value.

So how do I replace the formulas returning #N/A! with nothing?

Jan


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Replacing "error msg" in VBa

An error value is a special type of Variant, not the string "#N/A" (or
one of the other error values). You can't do a Replace operation with
an error type as the Find value, so you'll need to use a loop to find
the #N/A cells. To get an error type of test against the cell values,
you use the CVErr function to convert the error number to an error
type variant. For example,

Dim R As Range
For Each R In Range("A1:F10").SpecialCells(xlCellTypeFormulas)
If R.Value = CVErr(xlErrNA) Then
R.Value = "replaced"
End If
Next R

This will replace all the #N/A errors in cells with a formula with the
string "replaced". Change "replaced" to whatever you want to replace
the #N/A values with.

An Error 2042 is the representation of an #N/A error. The constant
value xlErrNA is a Long type value equal to the number 2042. When
that number is passed to CVErr, CVErr returns the corresponding Error
Type variable, an #N/A error.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Tue, 3 Nov 2009 16:31:56 +0100, "Jan Kronsell"
wrote:

I have a lot of cells returning #N/A! as a result of failed VLOOKUP's.
I would like to change all of these to "blanks", using

Range("A1:B70").Copy
Range("A1:B70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

'replace "#N/A" with "replword"
Cells.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

The first part transforms the erro codes into a value, displayed as #N/A
without the exclamationmark from the code. The latter part does absolutely
nothing, but leaves the cells as is.
When i use this code on any of the cells,

Sub t()
Dim a As Variant
a = ActiveCell.Value
Debug.Print a
End Sub

Ir returns Error 2042. If I try to replace "#N/A" in the replace statement
with "Erro 2042" still nothing happens as Error 2042 apparently is not a
value.

So how do I replace the formulas returning #N/A! with nothing?

Jan

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Replacing "error msg" in VBa

Try this:
Sub dl()
Dim c As Range
For Each c In Range("A1:C5") 'Change to actual
If IsError(c.Value) Then
c = ""
End If
Next
End Sub



"Jan Kronsell" wrote in message
...
I have a lot of cells returning #N/A! as a result of failed VLOOKUP's.
I would like to change all of these to "blanks", using

Range("A1:B70").Copy
Range("A1:B70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

'replace "#N/A" with "replword"
Cells.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

The first part transforms the erro codes into a value, displayed as #N/A
without the exclamationmark from the code. The latter part does absolutely
nothing, but leaves the cells as is.
When i use this code on any of the cells,

Sub t()
Dim a As Variant
a = ActiveCell.Value
Debug.Print a
End Sub

Ir returns Error 2042. If I try to replace "#N/A" in the replace
statement with "Erro 2042" still nothing happens as Error 2042 apparently
is not a value.

So how do I replace the formulas returning #N/A! with nothing?

Jan



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Replacing "error msg" in VBa

Try recording a macro when you:

Select the range
Edit|goto special (or F5 or ctrl-g)
Special
Formulas
Uncheck Numbers, text, logicals, but keep Errors checked.

Then hit the delete key on the keyboard.

Stop recording

This will clean those #n/a's along with #ref!'s, div/0, ..., well, all those
errors!

Jan Kronsell wrote:

I have a lot of cells returning #N/A! as a result of failed VLOOKUP's.
I would like to change all of these to "blanks", using

Range("A1:B70").Copy
Range("A1:B70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

'replace "#N/A" with "replword"
Cells.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

The first part transforms the erro codes into a value, displayed as #N/A
without the exclamationmark from the code. The latter part does absolutely
nothing, but leaves the cells as is.
When i use this code on any of the cells,

Sub t()
Dim a As Variant
a = ActiveCell.Value
Debug.Print a
End Sub

Ir returns Error 2042. If I try to replace "#N/A" in the replace statement
with "Erro 2042" still nothing happens as Error 2042 apparently is not a
value.

So how do I replace the formulas returning #N/A! with nothing?

Jan


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Replacing "error msg" in VBa

You could use the .text property instead of the .value property:

Sub t()
Dim a As String
a = ActiveCell.Text
Debug.Print a
End Sub


Jan Kronsell wrote:

I have a lot of cells returning #N/A! as a result of failed VLOOKUP's.
I would like to change all of these to "blanks", using

Range("A1:B70").Copy
Range("A1:B70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

'replace "#N/A" with "replword"
Cells.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

The first part transforms the erro codes into a value, displayed as #N/A
without the exclamationmark from the code. The latter part does absolutely
nothing, but leaves the cells as is.
When i use this code on any of the cells,

Sub t()
Dim a As Variant
a = ActiveCell.Value
Debug.Print a
End Sub

Ir returns Error 2042. If I try to replace "#N/A" in the replace statement
with "Erro 2042" still nothing happens as Error 2042 apparently is not a
value.

So how do I replace the formulas returning #N/A! with nothing?

Jan


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Replacing "error msg" in VBa

Thank you all. I used Chips method and it worked great.

Jan

Chip Pearson wrote:
An error value is a special type of Variant, not the string "#N/A" (or
one of the other error values). You can't do a Replace operation with
an error type as the Find value, so you'll need to use a loop to find
the #N/A cells. To get an error type of test against the cell values,
you use the CVErr function to convert the error number to an error
type variant. For example,

Dim R As Range
For Each R In Range("A1:F10").SpecialCells(xlCellTypeFormulas)
If R.Value = CVErr(xlErrNA) Then
R.Value = "replaced"
End If
Next R

This will replace all the #N/A errors in cells with a formula with the
string "replaced". Change "replaced" to whatever you want to replace
the #N/A values with.

An Error 2042 is the representation of an #N/A error. The constant
value xlErrNA is a Long type value equal to the number 2042. When
that number is passed to CVErr, CVErr returns the corresponding Error
Type variable, an #N/A error.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Tue, 3 Nov 2009 16:31:56 +0100, "Jan Kronsell"
wrote:

I have a lot of cells returning #N/A! as a result of failed
VLOOKUP's.
I would like to change all of these to "blanks", using

Range("A1:B70").Copy
Range("A1:B70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

'replace "#N/A" with "replword"
Cells.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

The first part transforms the erro codes into a value, displayed as
#N/A without the exclamationmark from the code. The latter part does
absolutely nothing, but leaves the cells as is.
When i use this code on any of the cells,

Sub t()
Dim a As Variant
a = ActiveCell.Value
Debug.Print a
End Sub

Ir returns Error 2042. If I try to replace "#N/A" in the replace
statement with "Erro 2042" still nothing happens as Error 2042
apparently is not a value.

So how do I replace the formulas returning #N/A! with nothing?

Jan



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
Replacing "IF" or "LOOKUP" formula's with VBA Function? dhunter43 Excel Programming 0 August 10th 07 06:52 PM
Replacing "IF" or "Lookup" formulas with VBA function dhunter43 Excel Programming 6 August 10th 07 06:22 PM
Replacing an ERROR message with "NA" COL Excel Discussion (Misc queries) 4 July 13th 06 11:09 PM
What is Error "Method "Paste" of object "_Worksheet" failed? vat Excel Programming 7 February 17th 06 08:05 PM
Getting "compile error" "method or data member not found" on reinstall Bp Excel Programming 1 April 23rd 04 04:42 PM


All times are GMT +1. The time now is 10:12 PM.

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

About Us

"It's about Microsoft Excel"