Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Insert Formula in Range

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
scott - 13 Dec 2007 01:38 GMT
I'm trying to insert the formula shown in CODE 2 below into cells C3:C13.
C3:C13 is a named range that always will be constantly growing, so my
function
InsertFormula() in CODE 1 should handle the range expanding dilema.

My question is syntax with the formula. CODE 2 shows the formula needed in
C3. However, when my function loops to the C4 cell, the formula being
inserted needs to change from using A3 cell to the A4 cell, and so on until
the function fills my range will the formula.

Can someone help me modify my InsertFormula() function to insert the formula
in each cell while incrementing the relative cell reference?

CODE 1:

Sub InsertFormula()

   Dim c As Range

   Set c = ActiveSheet.Range("C3")

   Do While c.Offset(0, -2).Value <> ""
       c.Value = xxxxxx ' Should be my formula
       Set c = c.Offset(1, 0)
   Loop

End Sub

CODE 2:

=IF(ISNA(VLOOKUP(A3,DataRange,1,FALSE)),"",A3)
sjoo.kwak@gmail.com - 13 Dec 2007 02:18 GMT
> I'm trying to insert the formula shown in CODE 2 below into cells C3:C13.
> C3:C13 is a named range that always will be constantly growing, so my
[quoted text clipped - 27 lines]
>
> =IF(ISNA(VLOOKUP(A3,DataRange,1,FALSE)),"",A3)

not c.Value , try c.Formula="=blahblah~"

HTH
sjoo
scott - 13 Dec 2007 02:36 GMT
thanks, but how do I make the formula's relative cell reference increment?

On 12¿ù13ÀÏ, ¿ÀÀü10½Ã38ºÐ, "scott" <sbai...@mileslumber.com> wrote:
> I'm trying to insert the formula shown in CODE 2 below into cells C3:C13.
> C3:C13 is a named range that always will be constantly growing, so my
[quoted text clipped - 29 lines]
>
> =IF(ISNA(VLOOKUP(A3,DataRange,1,FALSE)),"",A3)

not c.Value , try c.Formula="=blahblah~"

HTH
sjoo
Per Jessen - 13 Dec 2007 07:43 GMT
Hi Scott

Option Explicit

Sub InsertFormula()

   Dim c As Range
   Dim i As Integer

   Set c = ActiveSheet.Range("C3")

   Do While c.Offset(0, -2).Value <> ""
       i = c.Row
       c.Formula = "=IF(ISNA(VLOOKUP(A" & i & ",DataRange,1,FALSE)),"""",A"
& i & ")"

       Set c = c.Offset(1, 0)

   Loop

End Sub

Regards

Per

> thanks, but how do I make the formula's relative cell reference increment?
>
[quoted text clipped - 38 lines]
> HTH
> sjoo
scott - 13 Dec 2007 15:07 GMT
thank you, thank you!

i appreciate that and will learn from it.

> Hi Scott
>
[quoted text clipped - 66 lines]
>> HTH
>> sjoo
Tim Zych - 13 Dec 2007 08:47 GMT
This is why I like R1C1 referencing in VBA:

If it's a named range as you say:

Sub InsertFormula()
   Range("C3:C13").FormulaR1C1 =
"=IF(ISNA(VLOOKUP(RC1,DataRange,1,FALSE)),"""",RC1)"
   ' Add your named range in place of C3:C13
End Sub

or to loop:

Sub InsertFormula()
   Dim c As Range
   Set c = ActiveSheet.Range("C3")
   Do While c.Offset(0, -2).Value <> ""
       c.FormulaR1C1 = "=IF(ISNA(VLOOKUP(RC1,DataRange,1,FALSE)),"""",RC1)"
       Set c = c.Offset(1, 0)
   Loop
End Sub

Signature

Tim Zych
SF, CA

> I'm trying to insert the formula shown in CODE 2 below into cells C3:C13.
> C3:C13 is a named range that always will be constantly growing, so my
[quoted text clipped - 28 lines]
>
> =IF(ISNA(VLOOKUP(A3,DataRange,1,FALSE)),"",A3)
Dave Peterson - 13 Dec 2007 14:59 GMT
Another way is to just populate the whole range in one assignment:

Option Explicit
Sub InsertFormula2()

   Dim myRng As Range
   Dim LastRow As Long
   
   
   With ActiveSheet
       LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
       Set myRng = .Range("C3:C" & LastRow)
   End With
   
   myRng.Formula = "=IF(ISNA(VLOOKUP(A3,DataRange,1,FALSE)),"""",A3)"

End Sub

But I wouldn't use =vlookup() to check for a match.

I'd use a single column named range and a formula like:

=if(isnumber(match(a3,datarangecol1,0)),a3,"")

(Remember to double up your double quotes if you're going to use that formula in
your code.)

> I'm trying to insert the formula shown in CODE 2 below into cells C3:C13.
> C3:C13 is a named range that always will be constantly growing, so my
[quoted text clipped - 27 lines]
>
> =IF(ISNA(VLOOKUP(A3,DataRange,1,FALSE)),"",A3)

Signature

Dave Peterson

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.