PC Review


Reply
Thread Tools Rate Thread

Creating Formulas using BV

 
 
Straightpool
Guest
Posts: n/a
 
      27th Mar 2009
I need to create a formula in a VB macro based on the ActiveCell. The formula
should look like the following:

=IF(ISBLANK(J7),,I7/J7)

I7 & J7 will change based on the current row.

I am new using VB so any help will be greatly appreciated.

Thanks,
Brad
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      28th Mar 2009
If ActiveCell.Value > "" Then
ActiveCell = ActiveCell.Offset(0, 1).Value/ActiveCell.Value
End If

Restated:

Dim rng As Range
rng = ActiveCell
If rng.Value > "" Then
rng = rng.Offset(0, 1).Value/rng.Value
End If



"Straightpool" <(E-Mail Removed)> wrote in message
news:8A155E63-15B1-4DA7-98C4-(E-Mail Removed)...
>I need to create a formula in a VB macro based on the ActiveCell. The
>formula
> should look like the following:
>
> =IF(ISBLANK(J7),,I7/J7)
>
> I7 & J7 will change based on the current row.
>
> I am new using VB so any help will be greatly appreciated.
>
> Thanks,
> Brad



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      28th Mar 2009
One way is to look at the row that the activecell is on and use that to build
the formula:

Option Explicit
Sub testme01()

Dim myRow As Long
Dim myFormula As String

With ActiveCell
myRow = .Row
myFormula = "=if(isblank(J" & myRow & "),,i" _
& myRow & "/j" & myRow & ")"
.Formula = myFormula
End With

End Sub

This works ok if I don't know what column the cell is in that's going to get the
formula.

But if I know the column, I'll use the .formulaR1c1 reference style.

For instance, if this formula is going in column K, then the code gets much
easier and looks like:

Option Explicit
Sub testme02()
'=IF(ISBLANK(J7),,I7/J7)

Dim myRow As Long
Dim myFormulaR1C1 As String

With ActiveSheet.Cells(ActiveCell.Row, "K")
myFormulaR1C1 = "=if(isblank(rc[-1]),,rc[-2]/rc[-1])"
.Formula = myFormulaR1C1
End With

End Sub

In fact, when I have to do this, I'll write the formula in the cell to get it
working correctly.

Then I'll turn on the macro recorder and select that cell, hit F2 and enter.
Then turn off the macro recorder.

This gives me what that .formular1c1 reference style formula should be:

ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-1]),,RC[-2]/RC[-1])"

R1C1 reference style can be toggled (xl2003 menus):
tools|options|General|check or uncheck R1C1 reference style

rc[-1]
is the same row as the cell with the formula, but one column to the left.





Straightpool wrote:
>
> I need to create a formula in a VB macro based on the ActiveCell. The formula
> should look like the following:
>
> =IF(ISBLANK(J7),,I7/J7)
>
> I7 & J7 will change based on the current row.
>
> I am new using VB so any help will be greatly appreciated.
>
> Thanks,
> Brad


--

Dave Peterson
 
Reply With Quote
 
Straightpool
Guest
Posts: n/a
 
      28th Mar 2009
Thanks for the info. The RC referencing is just what I was looking for.

fwiw, I looked at the subject line of my thread and had to laugh when I saw
I had "VB" as "BV." lol

Brad

"Dave Peterson" wrote:

> One way is to look at the row that the activecell is on and use that to build
> the formula:
>
> Option Explicit
> Sub testme01()
>
> Dim myRow As Long
> Dim myFormula As String
>
> With ActiveCell
> myRow = .Row
> myFormula = "=if(isblank(J" & myRow & "),,i" _
> & myRow & "/j" & myRow & ")"
> .Formula = myFormula
> End With
>
> End Sub
>
> This works ok if I don't know what column the cell is in that's going to get the
> formula.
>
> But if I know the column, I'll use the .formulaR1c1 reference style.
>
> For instance, if this formula is going in column K, then the code gets much
> easier and looks like:
>
> Option Explicit
> Sub testme02()
> '=IF(ISBLANK(J7),,I7/J7)
>
> Dim myRow As Long
> Dim myFormulaR1C1 As String
>
> With ActiveSheet.Cells(ActiveCell.Row, "K")
> myFormulaR1C1 = "=if(isblank(rc[-1]),,rc[-2]/rc[-1])"
> .Formula = myFormulaR1C1
> End With
>
> End Sub
>
> In fact, when I have to do this, I'll write the formula in the cell to get it
> working correctly.
>
> Then I'll turn on the macro recorder and select that cell, hit F2 and enter.
> Then turn off the macro recorder.
>
> This gives me what that .formular1c1 reference style formula should be:
>
> ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-1]),,RC[-2]/RC[-1])"
>
> R1C1 reference style can be toggled (xl2003 menus):
> tools|options|General|check or uncheck R1C1 reference style
>
> rc[-1]
> is the same row as the cell with the formula, but one column to the left.
>
>
>
>
>
> Straightpool wrote:
> >
> > I need to create a formula in a VB macro based on the ActiveCell. The formula
> > should look like the following:
> >
> > =IF(ISBLANK(J7),,I7/J7)
> >
> > I7 & J7 will change based on the current row.
> >
> > I am new using VB so any help will be greatly appreciated.
> >
> > Thanks,
> > Brad

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help creating formulas Ken G Microsoft Excel Programming 6 30th Jan 2009 03:29 PM
Creating formulas =?Utf-8?B?ZnJvc3R5Ym95?= Microsoft Excel Programming 2 18th Nov 2006 10:09 AM
Self creating formulas antonov Microsoft Excel Programming 4 12th Dec 2005 12:38 AM
Re: Creating formulas Frank Kabel Microsoft Excel Programming 0 19th Mar 2004 10:18 PM
Creating Formulas Douglas J. Steele Microsoft Access Database Table Design 1 27th Dec 2003 05:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:49 PM.