Change a formula in VBA

  • Thread starter Thread starter Mahr Yon
  • Start date Start date
M

Mahr Yon

Can anyone help with this problem:

I am trying to write a macro to change a formula in a cell and place the new
formula in the same cell.

For instance:
Cel L1 in a worksheet contains: ='C:\TEST\[file.xls]data'!J7
By hand I can change this in: = TRIM('C:\TEST\[file10.xls]data'!J7) which
removes excess spaces.
How must I do this in a macro?

Thanks in advance

MahrYon
 
Excellent, Dave, this does exactly what I asked for. Thanks!

Dave Peterson said:
dim myCell as range
set mycell = activesheet.range("L1")
if mycell.hasformula then
mycell.formula = "=trim(" & mid(mycell.formula,2) & ")"
end if


Mahr said:
Can anyone help with this problem:

I am trying to write a macro to change a formula in a cell and place the new
formula in the same cell.

For instance:
Cel L1 in a worksheet contains: ='C:\TEST\[file.xls]data'!J7
By hand I can change this in: = TRIM('C:\TEST\[file10.xls]data'!J7) which
removes excess spaces.
How must I do this in a macro?

Thanks in advance

MahrYon
 
Thanks for your kind reaction, Leith.

Dave Peterson helped me already.
I intended to change once a formula in a number of cells, by a macro.
From now on these cells read out a certain file through that changed formula
(readfile + TRIM).
Every change in the file is observed, while the macro is not needed anymore.
Other functions might be added in the same way.
I wonder if it is simple to capitalize the first letter of the read strings.

Greetings, Mahr Yon

"Leith Ross" <[email protected]>
schreef in bericht
news:[email protected]...
 
dim myCell as range
set mycell = activesheet.range("L1")
if mycell.hasformula then
mycell.formula = "=trim(" & mid(mycell.formula,2) & ")"
end if


Mahr said:
Can anyone help with this problem:

I am trying to write a macro to change a formula in a cell and place the new
formula in the same cell.

For instance:
Cel L1 in a worksheet contains: ='C:\TEST\[file.xls]data'!J7
By hand I can change this in: = TRIM('C:\TEST\[file10.xls]data'!J7) which
removes excess spaces.
How must I do this in a macro?

Thanks in advance

MahrYon
 
Hello Mahr Yon,

From your post it looks like you are using the Worksheet Function TRIM
to remove the spaces in front of the string and at the end. Do you want
to create a VBA macro do to the same thing? Do you want the macro to
change the cell's value or it's formula? I ask because they are 2
separate properties and I want to be sure we are changing the right
one. Post back with your answers and I'll help you with the macro.

sincerely,
Leith Ross
 
It sounds like you want to use Excel's proper function which you can do in
VBA code by using worksheetfunction.proper. I did the following in the
Immediate pane:
x = "THIS is MY cOUNTRY"
? worksheetfunction.Proper(x)
This Is My Country

Mahr Yon said:
Thanks for your kind reaction, Leith.

Dave Peterson helped me already.
I intended to change once a formula in a number of cells, by a macro.
From now on these cells read out a certain file through that changed formula
(readfile + TRIM).
Every change in the file is observed, while the macro is not needed anymore.
Other functions might be added in the same way.
I wonder if it is simple to capitalize the first letter of the read strings.

Greetings, Mahr Yon

"Leith Ross" <[email protected]>
schreef in bericht
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top