Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
ali ali is offline
external usenet poster
 
Posts: 1
Default Applying formulae

Hello,

Can anyone tell me how to create a macro that when a range of cells are
selected and the macro run will prompt the user to enter a formula into
a box that appears on screen and then when the user presses ok the
formula entered is applied to the selected cells?

Thanks very much


---
Message posted from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Applying formulae

Try this, but it doesn't check for validity of formula


Sub myFormlae()
Dim sformula
sformula = InputBox("Supply formula (inlclude the leading =)")
If Not IsEmpty(sformula) Then
Selection.Formula = sformula
End If
End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"ali" wrote in message
...
Hello,

Can anyone tell me how to create a macro that when a range of cells are
selected and the macro run will prompt the user to enter a formula into
a box that appears on screen and then when the user presses ok the
formula entered is applied to the selected cells?

Thanks very much


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Applying formulae

Thanks but there is a problem with the line
"Selection.Formula = sformula"
which results is an application defined or object defined error

Any ideas?


---
Message posted from http://www.ExcelForum.com/

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Applying formulae

Sub myFormula()
Dim sformula as String
sformula = InputBox("Supply formula (inlclude the leading =)")
If Not IsEmpty(sformula) Then
sFormula = Trim(sFormula)
if left(sFormula,1) < "=" then _
sFormula = "=" & sFormula
On Error Resume Next
Selection.Formula = sformula
On Error goto 0
if err.Number < 0 then
msgbox "Invalid formula
err.clear
end if
End If
End Sub

--
Regards,
Tom Ogilvy

ian123 wrote in message
...
Thanks but there is a problem with the line
"Selection.Formula = sformula"
which results is an application defined or object defined error

Any ideas?


---
Message posted from http://www.ExcelForum.com/



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Applying formulae

Apologies if i am missing something really obvious here but i cant get
this to workas desired.

Imagine i have the values 1,2,3,4,5 in cells a1:a5. I want to run the
macro, enter "=*2" in the input box and have the values 2,4,6,8,10
replace 1,2,3,4,5 in cells a1:a5

However at present i'm running it and having "*2" returned in a1:a5 -
am i making a fundamental error at the input stage or is it something
else?

Thanks again


---
Message posted from http://www.ExcelForum.com/



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Applying formulae

What did you enter as the formula? I'm betting that something was wrong with
it. And excel would have yelled if you did it manually.

Depending on the formula, this might head off some errors:

Option Explicit
Sub myFormlae()
Dim sformula As Variant
sformula = InputBox("Supply formula (inlclude the leading =)")
If Not IsEmpty(sformula) Then
If Left(sformula, 1) < "=" Then
sformula = "=" & sformula
End If
If IsError(Evaluate(sformula)) Then
MsgBox "Hey, that can't be right"
Exit Sub
Else
Selection.Formula = sformula
End If
End If
End Sub

ian123 wrote:

Thanks but there is a problem with the line
"Selection.Formula = sformula"
which results is an application defined or object defined error

Any ideas?

---
Message posted from http://www.ExcelForum.com/


--

Dave Peterson

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Applying formulae

I'm entering "=+1" into the input box (for example). This simply
returns the value 1 to the selected cell rather than apply +1 to the
value already in the cell. Any ideas where i'm going wrong?


---
Message posted from http://www.ExcelForum.com/

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Applying formulae

Bob wrote the macro as if you were typing the complete formula into the
cell--not modifying an existing value/formula.

If you put 2 in an unused cell and edit|copy, then you can select A1:A5 and
edit|paste special|and check multiply.

You won't end up with a formula, but it's pretty easy.

Do you really want a macro/formula solution?

Option Explicit
Sub myFormlae2()

Dim myRng As Range
Dim myCell As Range
Dim myVal As Variant

myVal = Application.InputBox(Prompt:="Enter a number", Type:=1)
If myVal = False Then
Exit Sub 'user hit cancel
End If

Set myRng = Selection

For Each myCell In myRng.Cells
With myCell
If .HasFormula Then
.Formula = "=(" & Mid(.Formula, 2) & ")*" & myVal
Else
.Formula = "=" & .Formula & "*" & myVal
End If
End With
Next myCell

End Sub

This'll have trouble if the cell isn't numeric.



ian123 wrote:

Apologies if i am missing something really obvious here but i cant get
this to workas desired.

Imagine i have the values 1,2,3,4,5 in cells a1:a5. I want to run the
macro, enter "=*2" in the input box and have the values 2,4,6,8,10
replace 1,2,3,4,5 in cells a1:a5

However at present i'm running it and having "*2" returned in a1:a5 -
am i making a fundamental error at the input stage or is it something
else?

Thanks again

---
Message posted from http://www.ExcelForum.com/


--

Dave Peterson

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Applying formulae

I didn't notice that you wanted to pass the operand, too

Option Explicit
Sub myFormlae2()

Dim myRng As Range
Dim myCell As Range
Dim myVal As String

myVal = InputBox(Prompt:="Enter a String")
If Trim(myVal) = "" Then
Exit Sub 'user hit cancel
End If

Set myRng = Selection

For Each myCell In myRng.Cells
With myCell
If .HasFormula Then
.Formula = "=(" & Mid(.Formula, 2) & ")" & myVal
Else
.Formula = "=" & .Formula & myVal
End If
End With
Next myCell

End Sub

Don't put the equal sign.

ian123 wrote:

I'm entering "=+1" into the input box (for example). This simply
returns the value 1 to the selected cell rather than apply +1 to the
value already in the cell. Any ideas where i'm going wrong?

---
Message posted from http://www.ExcelForum.com/


--

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
Applying Formulae to a worksheet which are hidden Ra New Users to Excel 11 May 23rd 09 07:16 PM
Formulae: Paste value formulae after doing an average operation Lim Excel Discussion (Misc queries) 4 April 20th 08 07:31 PM
Applying Logic Jayvee Excel Discussion (Misc queries) 2 June 10th 07 11:14 PM
Searching TEXT in formulae, rather than results of formulae AndyE Excel Worksheet Functions 1 July 15th 05 10:57 AM
Applying formulae to cells??? index[_11_] Excel Programming 4 December 6th 03 08:32 PM


All times are GMT +1. The time now is 12:21 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"