Translating a forumla into Code

Tweeks

Board Regular
Joined
Jul 27, 2004
Messages
52
Hi there,

I need to translate this formula:
=LEFT(C5,FIND("*",C5,1)-1)
into VBA code where the C5 is 'activecell.value'.

I think its all generally the same bar the FIND which i am not sure what it should be.

Using XL 97.
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,061
Office Version
  1. 365
Platform
  1. Windows
What's the formula actually meant to do?

When you say you want to translate it what do you mean?

Do you want this formula in the active cell or do you want the VBA equivalent?
 

Tweeks

Board Regular
Joined
Jul 27, 2004
Messages
52
I want to take a value from a cell that will be

number*number

So like 1*8 or 10*12

I want to store the first number into a variable, and then the other one into another variable. I need to repeat this lots of times so ideally not doing it as a formula in a spreadsheet. I can make it work in a spreadsheet ok but I would really like to be able to do this in VBA code instead. There are reasons but they are far too boring and long winded to go into here.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,061
Office Version
  1. 365
Platform
  1. Windows
Do you just want to seperate these 2 numbers into seperate cells?

If you do why not use Data>Text to Columns... with * as a delimiter.

In VBA you will have problems as the function for finding text regards * as a wildcard.
 

Tweeks

Board Regular
Joined
Jul 27, 2004
Messages
52

ADVERTISEMENT

It has to be in VBA code, although the '*' can be anything if that helps.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,061
Office Version
  1. 365
Platform
  1. Windows
Could you please give more details of what you are doing?

You give an example using * then say that could be anything.

Are you just trying to extract 2 numbers?
 

Tweeks

Board Regular
Joined
Jul 27, 2004
Messages
52
It is really so long winded to explain.

I need to use the two numbers to loop through, so 1*8 i need to go from 1 through to 8 in some code i guess is the simplest way to explain it.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,061
Office Version
  1. 365
Platform
  1. Windows
I'm sorry but I really can't help without further explanation.

Code:
For x = Left(ActiveCell.Text, InStr(ActiveCell.Text, "*") - 1) To Mid(ActiveCell.Text, InStr(ActiveCell.Text, "*") + 1)
        MsgBox x
Next x
 

Watch MrExcel Video

Forum statistics

Threads
1,118,812
Messages
5,574,456
Members
412,595
Latest member
slim313
Top