#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba

Hi, I would like to carry out an IF function so that if one cell has a text
of " Note", text of "N/A" is put in another cell ie =IF(C28="Note","N/A",
""), but I want to do this in VBA so that I can still type in the cell that
the answer would be put into, ie if the text isnt "Note" I want to be able to
manually write other text or values in the cell. I want this to be automatic
without running macros, so that "N/A automatically appears if "Note" is
written is the reference cell.
VBA is not my strong point but I'm guessing that an IF statement can be used
on the correct sheet in Excel objects.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default IF in vba

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub

--
Regards,
Tom Ogilvy


"Newbeetle" wrote in message
...
Hi, I would like to carry out an IF function so that if one cell has a
text
of " Note", text of "N/A" is put in another cell ie =IF(C28="Note","N/A",
""), but I want to do this in VBA so that I can still type in the cell
that
the answer would be put into, ie if the text isnt "Note" I want to be able
to
manually write other text or values in the cell. I want this to be
automatic
without running macros, so that "N/A automatically appears if "Note" is
written is the reference cell.
VBA is not my strong point but I'm guessing that an IF statement can be
used
on the correct sheet in Excel objects.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba

Hi Tom,

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub

--
Regards,
Tom Ogilvy


"Newbeetle" wrote in message
...
Hi, I would like to carry out an IF function so that if one cell has a
text
of " Note", text of "N/A" is put in another cell ie =IF(C28="Note","N/A",
""), but I want to do this in VBA so that I can still type in the cell
that
the answer would be put into, ie if the text isnt "Note" I want to be able
to
manually write other text or values in the cell. I want this to be
automatic
without running macros, so that "N/A automatically appears if "Note" is
written is the reference cell.
VBA is not my strong point but I'm guessing that an IF statement can be
used
on the correct sheet in Excel objects.





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default IF in vba

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba

Thank you

"JE McGimpsey" wrote:

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba

Thankyou

"JE McGimpsey" wrote:

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba

Hi,

I have modified the code as below;

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
End Sub

And with all the help thats been given, I'm glad to say it works a treat,

But I would now like to do the same for cell C29, to return to
F29,G29,H29,I29 then C30 etc but have got a little stuck.

"JE McGimpsey" wrote:

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba

Hi I have modified the code below to;

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
End Sub

With all the help thats been given I'm glad to say it works a treat, I would
now like to do the same for cell C29, to return to F29,G29,H29,I29 but have
got a little stuck.

"JE McGimpsey" wrote:

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba

Hi I have modified the code below to;

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
End Sub

With all the help thats been given I'm glad to say it works a treat, I would
now like to do the same for cell C29, to return to F29,G29,H29,I29 but have
got a little stuck.


"Newbeetle" wrote:

Thankyou

"JE McGimpsey" wrote:

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default IF in vba

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
If Target.Address(False, False) = "C29" Then
If InStr(1, Range("C29"), "note", vbTextCompare) Then
Range("F29,G29,H29,I29") = "N/A"
Else
If Range("F29,G29,H29,I29") = "N/A" Then
Range("F29,G29,H29,I29").ClearContents
End If
End If
End If
End Sub

this statement:
If Range("F28,G28,H28,I28") = "N/A" then

is a bit peculiar
Just do demonstrate from the immediate window:
Range("F28") = "N/A"
Range("G28,H28,I28").ClearContents
? Range("F28,G28,H28,I28") = "N/A"
True
Range("F28").ClearContents
Range("G28,H28,I28")= "N/A"
? Range("F28,G28,H28,I28") = "N/A"
False

so we see that it returns true or false solely on the value of the first
cell, F28.

that may or may not be what you want.

--
Regards,
Tom Ogilvy





"Newbeetle" wrote:

Hi I have modified the code below to;

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
End Sub

With all the help thats been given I'm glad to say it works a treat, I would
now like to do the same for cell C29, to return to F29,G29,H29,I29 but have
got a little stuck.

"JE McGimpsey" wrote:

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba

Hi Tom,

I could have sworn I tried that, but surely not as that works a treat, thank
you as always.

"Tom Ogilvy" wrote:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
If Target.Address(False, False) = "C29" Then
If InStr(1, Range("C29"), "note", vbTextCompare) Then
Range("F29,G29,H29,I29") = "N/A"
Else
If Range("F29,G29,H29,I29") = "N/A" Then
Range("F29,G29,H29,I29").ClearContents
End If
End If
End If
End Sub

this statement:
If Range("F28,G28,H28,I28") = "N/A" then

is a bit peculiar
Just do demonstrate from the immediate window:
Range("F28") = "N/A"
Range("G28,H28,I28").ClearContents
? Range("F28,G28,H28,I28") = "N/A"
True
Range("F28").ClearContents
Range("G28,H28,I28")= "N/A"
? Range("F28,G28,H28,I28") = "N/A"
False

so we see that it returns true or false solely on the value of the first
cell, F28.

that may or may not be what you want.

--
Regards,
Tom Ogilvy





"Newbeetle" wrote:

Hi I have modified the code below to;

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
End Sub

With all the help thats been given I'm glad to say it works a treat, I would
now like to do the same for cell C29, to return to F29,G29,H29,I29 but have
got a little stuck.

"JE McGimpsey" wrote:

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub

  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default IF in vba


Thanks Tom,

I could of sworn i had tried that, but surley not as that works a treat,
thank you for you help as always.

"Tom Ogilvy" wrote:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
If Target.Address(False, False) = "C29" Then
If InStr(1, Range("C29"), "note", vbTextCompare) Then
Range("F29,G29,H29,I29") = "N/A"
Else
If Range("F29,G29,H29,I29") = "N/A" Then
Range("F29,G29,H29,I29").ClearContents
End If
End If
End If
End Sub

this statement:
If Range("F28,G28,H28,I28") = "N/A" then

is a bit peculiar
Just do demonstrate from the immediate window:
Range("F28") = "N/A"
Range("G28,H28,I28").ClearContents
? Range("F28,G28,H28,I28") = "N/A"
True
Range("F28").ClearContents
Range("G28,H28,I28")= "N/A"
? Range("F28,G28,H28,I28") = "N/A"
False

so we see that it returns true or false solely on the value of the first
cell, F28.

that may or may not be what you want.

--
Regards,
Tom Ogilvy





"Newbeetle" wrote:

Hi I have modified the code below to;

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("F28,G28,H28,I28") = "N/A"
Else
If Range("F28,G28,H28,I28") = "N/A" Then
Range("F28,G28,H28,I28").ClearContents
End If
End If
End If
End Sub

With all the help thats been given I'm glad to say it works a treat, I would
now like to do the same for cell C29, to return to F29,G29,H29,I29 but have
got a little stuck.

"JE McGimpsey" wrote:

Change the line in the second event macro from

If Target.Address = "C28" Then

to

If Target.Address(False, False) = "C28" Then


In article ,
Newbeetle wrote:

I copied the code into a completly new workbook and placed it in Excel
objects sheet one, I then went to the Excel sheet and typed note in Cell C28,
but the cell D28 stays blank.

Any futher ideas appreciated.

"Tom Ogilvy" wrote:

You would need to run a macro which is written in VBA.

Right click on the sheet tab and select view code.

Put in code like this:

Private Sub Worksheet_Calculate()
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "C28" Then
If InStr(1, Range("C28"), "note", vbTextCompare) Then
Range("D28") = CVErr(xlErrNA)
Else
If Range("D28") = CVErr(xlErrNA) Then
Range("D28").ClearContents
End If
End If
End If
End Sub

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



All times are GMT +1. The time now is 10:48 AM.

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"