Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default VBA replaces formula but adds row in multiplier

The formula in cell V2 is:
=IF(BB2="SURG",(IF(H2<13,"No Outlier",(H2-13)*5128)),"Go to Med")

The VBA below, copies the formula down into other cells but it replaces the
multiplier of 5128 by inserting the row number into it. For example if
copied to row 81 then the 5128 becomes 51818. It apparently is replacing all
2's with the new corresponding row number. Is there a way to fix this VBA so
it only changes the cell values in the formula and not the multiplier like so:
=IF(BB81="SURG",(IF(H81<13,"No Outlier",(H81-13)*5128)),"Go to Med")

For x = 21 To 2500
If Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "Go to surg" Or _
Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "#N/A" Then
Cells(x, 22).Formula = Replace(Cells(2, 22).Formula, 2, x)
flag = 1
End If
Next
Calculate
End Sub

Thanks for your help,
-Bill

  #2   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 145
Default VBA replaces formula but adds row in multiplier

s=Cells(2, 22).Formula
s=replace(s, "BB2","BB" & x)
s=replace(s, "H2","H" & x)
Cells(x, 22).Formula = x

...or just copy/paste the formula in code.

You might also consider making your "If ... Then" criteria more explicit:
ie. not
A and B or C and D
but
(A and B) or (C and D)

Easier to follow, and no chance of unexpected results.

Tim



"bill ch" wrote in message
...
The formula in cell V2 is:
=IF(BB2="SURG",(IF(H2<13,"No Outlier",(H2-13)*5128)),"Go to Med")

The VBA below, copies the formula down into other cells but it replaces
the
multiplier of 5128 by inserting the row number into it. For example if
copied to row 81 then the 5128 becomes 51818. It apparently is replacing
all
2's with the new corresponding row number. Is there a way to fix this VBA
so
it only changes the cell values in the formula and not the multiplier like
so:
=IF(BB81="SURG",(IF(H81<13,"No Outlier",(H81-13)*5128)),"Go to Med")

For x = 21 To 2500
If Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "Go to surg" Or _
Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "#N/A" Then
Cells(x, 22).Formula = Replace(Cells(2, 22).Formula, 2, x)
flag = 1
End If
Next
Calculate
End Sub

Thanks for your help,
-Bill



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default VBA replaces formula but adds row in multiplier

Tim, is there a typo in that last line?

"Tim" wrote:

s=Cells(2, 22).Formula
s=replace(s, "BB2","BB" & x)
s=replace(s, "H2","H" & x)
Cells(x, 22).Formula = x

...or just copy/paste the formula in code.

You might also consider making your "If ... Then" criteria more explicit:
ie. not
A and B or C and D
but
(A and B) or (C and D)

Easier to follow, and no chance of unexpected results.

Tim



"bill ch" wrote in message
...
The formula in cell V2 is:
=IF(BB2="SURG",(IF(H2<13,"No Outlier",(H2-13)*5128)),"Go to Med")

The VBA below, copies the formula down into other cells but it replaces
the
multiplier of 5128 by inserting the row number into it. For example if
copied to row 81 then the 5128 becomes 51818. It apparently is replacing
all
2's with the new corresponding row number. Is there a way to fix this VBA
so
it only changes the cell values in the formula and not the multiplier like
so:
=IF(BB81="SURG",(IF(H81<13,"No Outlier",(H81-13)*5128)),"Go to Med")

For x = 21 To 2500
If Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "Go to surg" Or _
Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "#N/A" Then
Cells(x, 22).Formula = Replace(Cells(2, 22).Formula, 2, x)
flag = 1
End If
Next
Calculate
End Sub

Thanks for your help,
-Bill




  #4   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 145
Default VBA replaces formula but adds row in multiplier

Yes, should be "s":

Cells(x, 22).Formula = s

Thanks
Tim

"JLGWhiz" wrote in message
...
Tim, is there a typo in that last line?

"Tim" wrote:

s=Cells(2, 22).Formula
s=replace(s, "BB2","BB" & x)
s=replace(s, "H2","H" & x)
Cells(x, 22).Formula = x

...or just copy/paste the formula in code.

You might also consider making your "If ... Then" criteria more explicit:
ie. not
A and B or C and D
but
(A and B) or (C and D)

Easier to follow, and no chance of unexpected results.

Tim



"bill ch" wrote in message
...
The formula in cell V2 is:
=IF(BB2="SURG",(IF(H2<13,"No Outlier",(H2-13)*5128)),"Go to Med")

The VBA below, copies the formula down into other cells but it replaces
the
multiplier of 5128 by inserting the row number into it. For example if
copied to row 81 then the 5128 becomes 51818. It apparently is
replacing
all
2's with the new corresponding row number. Is there a way to fix this
VBA
so
it only changes the cell values in the formula and not the multiplier
like
so:
=IF(BB81="SURG",(IF(H81<13,"No Outlier",(H81-13)*5128)),"Go to Med")

For x = 21 To 2500
If Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "Go to surg" Or _
Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "#N/A" Then
Cells(x, 22).Formula = Replace(Cells(2, 22).Formula, 2, x)
flag = 1
End If
Next
Calculate
End Sub

Thanks for your help,
-Bill






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default VBA replaces formula but adds row in multiplier

Yeah, that makes more sense.<g

"Tim" wrote:

Yes, should be "s":

Cells(x, 22).Formula = s

Thanks
Tim

"JLGWhiz" wrote in message
...
Tim, is there a typo in that last line?

"Tim" wrote:

s=Cells(2, 22).Formula
s=replace(s, "BB2","BB" & x)
s=replace(s, "H2","H" & x)
Cells(x, 22).Formula = x

...or just copy/paste the formula in code.

You might also consider making your "If ... Then" criteria more explicit:
ie. not
A and B or C and D
but
(A and B) or (C and D)

Easier to follow, and no chance of unexpected results.

Tim



"bill ch" wrote in message
...
The formula in cell V2 is:
=IF(BB2="SURG",(IF(H2<13,"No Outlier",(H2-13)*5128)),"Go to Med")

The VBA below, copies the formula down into other cells but it replaces
the
multiplier of 5128 by inserting the row number into it. For example if
copied to row 81 then the 5128 becomes 51818. It apparently is
replacing
all
2's with the new corresponding row number. Is there a way to fix this
VBA
so
it only changes the cell values in the formula and not the multiplier
like
so:
=IF(BB81="SURG",(IF(H81<13,"No Outlier",(H81-13)*5128)),"Go to Med")

For x = 21 To 2500
If Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "Go to surg" Or _
Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "#N/A" Then
Cells(x, 22).Formula = Replace(Cells(2, 22).Formula, 2, x)
flag = 1
End If
Next
Calculate
End Sub

Thanks for your help,
-Bill









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default VBA replaces formula but adds row in multiplier

Dont know about the context of this issue - but might it not be easier just
to make the formula itself do the work:

=IF(BB2="SURG",(IF(H2<13,"No Outlier",(H2-13)*VALUE("51"&ROW() &"8"))),"Go
to Med")

"bill ch" wrote:

The formula in cell V2 is:
=IF(BB2="SURG",(IF(H2<13,"No Outlier",(H2-13)*5128)),"Go to Med")

The VBA below, copies the formula down into other cells but it replaces the
multiplier of 5128 by inserting the row number into it. For example if
copied to row 81 then the 5128 becomes 51818. It apparently is replacing all
2's with the new corresponding row number. Is there a way to fix this VBA so
it only changes the cell values in the formula and not the multiplier like so:
=IF(BB81="SURG",(IF(H81<13,"No Outlier",(H81-13)*5128)),"Go to Med")

For x = 21 To 2500
If Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "Go to surg" Or _
Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "#N/A" Then
Cells(x, 22).Formula = Replace(Cells(2, 22).Formula, 2, x)
flag = 1
End If
Next
Calculate
End Sub

Thanks for your help,
-Bill

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default VBA replaces formula but adds row in multiplier

This works great! Thanks for the education.
-Bill

"Tim" wrote:

Yes, should be "s":

Cells(x, 22).Formula = s

Thanks
Tim

"JLGWhiz" wrote in message
...
Tim, is there a typo in that last line?

"Tim" wrote:

s=Cells(2, 22).Formula
s=replace(s, "BB2","BB" & x)
s=replace(s, "H2","H" & x)
Cells(x, 22).Formula = x

...or just copy/paste the formula in code.

You might also consider making your "If ... Then" criteria more explicit:
ie. not
A and B or C and D
but
(A and B) or (C and D)

Easier to follow, and no chance of unexpected results.

Tim



"bill ch" wrote in message
...
The formula in cell V2 is:
=IF(BB2="SURG",(IF(H2<13,"No Outlier",(H2-13)*5128)),"Go to Med")

The VBA below, copies the formula down into other cells but it replaces
the
multiplier of 5128 by inserting the row number into it. For example if
copied to row 81 then the 5128 becomes 51818. It apparently is
replacing
all
2's with the new corresponding row number. Is there a way to fix this
VBA
so
it only changes the cell values in the formula and not the multiplier
like
so:
=IF(BB81="SURG",(IF(H81<13,"No Outlier",(H81-13)*5128)),"Go to Med")

For x = 21 To 2500
If Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "Go to surg" Or _
Cells(x, 2).Text = "J" And _
Cells(x, 22).Text = "#N/A" Then
Cells(x, 22).Formula = Replace(Cells(2, 22).Formula, 2, x)
flag = 1
End If
Next
Calculate
End Sub

Thanks for your help,
-Bill







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
replaces formula to its value InspectorJim Excel Worksheet Functions 1 May 13th 09 02:41 AM
what is the formula that identifies v8 in a row & adds v8+v8=v16? Stacy Excel Discussion (Misc queries) 3 December 31st 08 10:56 PM
#Value replaces formula result when file is opened. But why? Arlen Excel Discussion (Misc queries) 8 July 18th 08 09:13 PM
How to set up a formula that it adds value on a certain date LoriKLynn Excel Discussion (Misc queries) 5 October 15th 07 11:54 PM
how to create a repeat formula using the same multiplier? Lisa Excel Worksheet Functions 2 August 8th 06 03:15 AM


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