Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
vtj vtj is offline
external usenet poster
 
Posts: 9
Default Enter formula in Macro/VBA

I have a sheet that I want to put formulas into. The formula that I want to
paste in is similar to +A2+B2-C2+D2 with the result going into E2. If I put
that into the E2 location, the following will paste that formula all the way
down the sheet after I insert a Do Loop:

Sub Macro6()
'
' Macro6 Macro
'

'
Range("N187").Select
Selection.Copy
Debug.Print (xlFormulas)
Range("N188").Select
Debug.Print (xlFormulas)
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub

The Debug.Print always shows -4123 as the value even if I use a different
formula or have it in different columns or rows. I am using Excel 2007
running on XP.

What I want to do is be able to define a formula in the Macro/VBA that I
could then paste so that I could change the formula whenever I needed to
based on the sheet that I'm working with.

Any help will be greatly appreciated. Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default Enter formula in Macro/VBA

Sub Macro6()
With Range("E2")

.Formula = "=A2+B2-C2+D2"
.Copy .Offset(1, 0)
End With
End Sub




--
__________________________________
HTH

Bob

"vtj" wrote in message
...
I have a sheet that I want to put formulas into. The formula that I want
to
paste in is similar to +A2+B2-C2+D2 with the result going into E2. If I
put
that into the E2 location, the following will paste that formula all the
way
down the sheet after I insert a Do Loop:

Sub Macro6()
'
' Macro6 Macro
'

'
Range("N187").Select
Selection.Copy
Debug.Print (xlFormulas)
Range("N188").Select
Debug.Print (xlFormulas)
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub

The Debug.Print always shows -4123 as the value even if I use a different
formula or have it in different columns or rows. I am using Excel 2007
running on XP.

What I want to do is be able to define a formula in the Macro/VBA that I
could then paste so that I could change the formula whenever I needed to
based on the sheet that I'm working with.

Any help will be greatly appreciated. Thanks.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Enter formula in Macro/VBA

Excel's VBA has a bunch of constants that make coding easier to understand.

For instance, vbRed is a constant that is equivalent to 255.

That makes this kind of code:
activesheet.range("a1").font.color = vbred
easier to understand than
activesheet.range("a1").font.color = 255

The code will do the same, but one is easier to understand.

Same thing with xlformulas. It's a constant that makes the code easier to use.

If you wanted to retrieve the formula of a single cell, you could use:

dim myFormula as string
myformula = activecell.formula

I'd recommend that you record a macro while you enter a formula in the cell and
you'll see how it works:

with activesheet
.range("e2").formula = "=a2+b2-c2+d2"
'then fill down
end with

Manually you can select the range (E2:E99, say) type the formula for E2 and hit
ctrl-enter to fill the selection with that formula. Excel will adjust the
formula (if you write it nicely) to what you want.

You can do the same thing in code.

dim LastRow as long
with activesheet
lastrow = .cells(.rows.count,"A").end(xlup).row
.range("E2:E" & lastrow).formula = "=a2+b2-c2+d2"
end with

LastRow is based on the last used cell in column A. That may not be what you
want. Change the "A" to what you need.

vtj wrote:

I have a sheet that I want to put formulas into. The formula that I want to
paste in is similar to +A2+B2-C2+D2 with the result going into E2. If I put
that into the E2 location, the following will paste that formula all the way
down the sheet after I insert a Do Loop:

Sub Macro6()
'
' Macro6 Macro
'

'
Range("N187").Select
Selection.Copy
Debug.Print (xlFormulas)
Range("N188").Select
Debug.Print (xlFormulas)
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub

The Debug.Print always shows -4123 as the value even if I use a different
formula or have it in different columns or rows. I am using Excel 2007
running on XP.

What I want to do is be able to define a formula in the Macro/VBA that I
could then paste so that I could change the formula whenever I needed to
based on the sheet that I'm working with.

Any help will be greatly appreciated. Thanks.


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
vtj vtj is offline
external usenet poster
 
Posts: 9
Default Enter formula in Macro/VBA

Thank You very much both Bob and Dave. You have saved me a lot of work!!

"Dave Peterson" wrote:

Excel's VBA has a bunch of constants that make coding easier to understand.

For instance, vbRed is a constant that is equivalent to 255.

That makes this kind of code:
activesheet.range("a1").font.color = vbred
easier to understand than
activesheet.range("a1").font.color = 255

The code will do the same, but one is easier to understand.

Same thing with xlformulas. It's a constant that makes the code easier to use.

If you wanted to retrieve the formula of a single cell, you could use:

dim myFormula as string
myformula = activecell.formula

I'd recommend that you record a macro while you enter a formula in the cell and
you'll see how it works:

with activesheet
.range("e2").formula = "=a2+b2-c2+d2"
'then fill down
end with

Manually you can select the range (E2:E99, say) type the formula for E2 and hit
ctrl-enter to fill the selection with that formula. Excel will adjust the
formula (if you write it nicely) to what you want.

You can do the same thing in code.

dim LastRow as long
with activesheet
lastrow = .cells(.rows.count,"A").end(xlup).row
.range("E2:E" & lastrow).formula = "=a2+b2-c2+d2"
end with

LastRow is based on the last used cell in column A. That may not be what you
want. Change the "A" to what you need.

vtj wrote:

I have a sheet that I want to put formulas into. The formula that I want to
paste in is similar to +A2+B2-C2+D2 with the result going into E2. If I put
that into the E2 location, the following will paste that formula all the way
down the sheet after I insert a Do Loop:

Sub Macro6()
'
' Macro6 Macro
'

'
Range("N187").Select
Selection.Copy
Debug.Print (xlFormulas)
Range("N188").Select
Debug.Print (xlFormulas)
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub

The Debug.Print always shows -4123 as the value even if I use a different
formula or have it in different columns or rows. I am using Excel 2007
running on XP.

What I want to do is be able to define a formula in the Macro/VBA that I
could then paste so that I could change the formula whenever I needed to
based on the sheet that I'm working with.

Any help will be greatly appreciated. Thanks.


--

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
Using a macro to enter a formula Tami Excel Worksheet Functions 4 September 24th 09 11:47 AM
Macro to Enter Formula in a Cell Thomas M. Excel Programming 2 September 21st 06 01:10 AM
Enter formula in a macro Annette Excel Programming 3 May 1st 06 06:37 PM
Can you enter a formula in a cell to run a macro? Nevaeh Excel Worksheet Functions 2 February 14th 05 11:51 PM
Macro to enter formula with text bambam77[_3_] Excel Programming 5 January 2nd 04 01:56 AM


All times are GMT +1. The time now is 11:13 AM.

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"