Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 27
Default Adding formulas via macro

Hi,

I'm trying to add some formulas with a macro. With the first I want the
value of "D2" to be "XXX" if the 3 left characters of cell "C2" are "YYY" but
I'm not sure of the correct syntax. This is what I have so far:

If Left.Range("c2", 3) = "095" Then .Range("D" & 2).Value = "ZB900"
..Range("D" & 2).Copy _
Destination:=.Range("D" & 2 & ":D" & lstrw)

In this next case I'm trying to load the formula:

..Range("E" & 2).Formula = "=("0"&right(c2,4))"

but I keep getting an error.

Any ideas?
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Adding formulas via macro

Do you want the macro to produce a formula that does the work or do you want the
macro to do the work?

If a formula:

with worksheets("Sheet9999") '<--what sheet???
.range("d2").formular1c1 _
= "=if(right(rc[-1],3)=""yyy"",""xxx"","""")"
end with

If you want the macro to do the work:

with worksheets("Sheet9999")
if lcase(right(.range("c2").value,3)) = "yyy" then
.range("d2").value = "XXX"
else
.range("D2").value = ""
end if
end with

I chose to make the cell look empty if the criterion wasn't met. I'm not sure
that's what you wanted.

=====
And try this:

.Range("E" & 2).Formula = "=(""0""&right(c2,4))"

Notice how the double quotes in strings in formulas in your code are doubled up.

mattg wrote:

Hi,

I'm trying to add some formulas with a macro. With the first I want the
value of "D2" to be "XXX" if the 3 left characters of cell "C2" are "YYY" but
I'm not sure of the correct syntax. This is what I have so far:

If Left.Range("c2", 3) = "095" Then .Range("D" & 2).Value = "ZB900"
.Range("D" & 2).Copy _
Destination:=.Range("D" & 2 & ":D" & lstrw)

In this next case I'm trying to load the formula:

.Range("E" & 2).Formula = "=("0"&right(c2,4))"

but I keep getting an error.

Any ideas?


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 27
Default Adding formulas via macro

I went with option 2 for the first macro and it worked great. The fix on the
second formula work also.

Thanks!!!!

"Dave Peterson" wrote:

Do you want the macro to produce a formula that does the work or do you want the
macro to do the work?

If a formula:

with worksheets("Sheet9999") '<--what sheet???
.range("d2").formular1c1 _
= "=if(right(rc[-1],3)=""yyy"",""xxx"","""")"
end with

If you want the macro to do the work:

with worksheets("Sheet9999")
if lcase(right(.range("c2").value,3)) = "yyy" then
.range("d2").value = "XXX"
else
.range("D2").value = ""
end if
end with

I chose to make the cell look empty if the criterion wasn't met. I'm not sure
that's what you wanted.

=====
And try this:

.Range("E" & 2).Formula = "=(""0""&right(c2,4))"

Notice how the double quotes in strings in formulas in your code are doubled up.

mattg wrote:

Hi,

I'm trying to add some formulas with a macro. With the first I want the
value of "D2" to be "XXX" if the 3 left characters of cell "C2" are "YYY" but
I'm not sure of the correct syntax. This is what I have so far:

If Left.Range("c2", 3) = "095" Then .Range("D" & 2).Value = "ZB900"
.Range("D" & 2).Copy _
Destination:=.Range("D" & 2 & ":D" & lstrw)

In this next case I'm trying to load the formula:

.Range("E" & 2).Formula = "=("0"&right(c2,4))"

but I keep getting an error.

Any ideas?


--

Dave Peterson

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
Adding next row of formulas Lee New Users to Excel 3 March 19th 09 03:42 AM
Adding formulas bolludvi Excel Discussion (Misc queries) 4 January 30th 09 08:19 PM
Adding formulas Steved Excel Worksheet Functions 4 April 11th 07 06:10 PM
Why are my formulas not adding up automatically Brittany Excel Worksheet Functions 1 November 2nd 06 07:26 PM
Adding rows in between formulas Mary Excel Worksheet Functions 0 June 21st 06 05:50 PM


All times are GMT +1. The time now is 10:28 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"