Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Formula to VBA

You need to define the addresses as a range something like this...

with sheets("Sheet1")
tmpName = Left(.Range("B1"), 3) & " " & Right(.Range("B1"), 2)
end with
MsgBox tmpName

As a complete aside you should have Option Explicit at the top of your
module. Oddly enough it would catch this error for you as you would end up
with undeclared varaibles that would not compile... If you have more
questions about that just ask...
--
HTH...

Jim Thomlinson


"Ronbo" wrote:

What is the VBA routine that is the equivalent to the formula;

= Left(B1, 3) & " " & Right(B1, 2)

When I use:

tmpName = Left(B1, 3) & " " & Right(B1, 2)
MsgBox tmpName

In works without an error, but the message box is empty?

As always, any help is appreciated.

Regards
Ronbo

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Formula to VBA

I do see the grurs and some in particular, including you, suggesting the
"Option Explicit".

I have never taken the time to figure out why or how. Should I?


You should ALWAYS have Option Explicit at the top of your code module. There
is absolutely NO excuse not to.

Option Explicit forces you to declare all your variables using the Dim
statement. If you attempt to access a variable that is not previously
declared, you'll get a compiler error.

Without Option Explicit, VBA will declare variables as they are encountered
in code. Thus, if you misspell a variable name, VBA will create a new
variable with that misspelled name and assign it a default value of Empty
(which is interpreted as 0, vbNullString, or Nothing, depending on the
context). For example, suppose you're writing a large application that does
something with printing, and you use a variable named "PageCount". VBA will
create a variable by that name and give it a default value. A thousand lines
later, you use "PageCounter" instead of "PageCount". VBA will create a
variable named "PageCounter" and give it a default value. But that isn't the
value you expect. You expect the value in "PageCount", not "PageCounter".
This will likely cause a logic problem or cause a runtime error, depending
on what you are doing in the code.

Debugging errors in code that does not use Option Explicit is a nightmare.
In the example above, if someone else wrote the code and it is up to you to
debug it, how do determine whether there is supposed to be a single variable
named "PageCount" and "PageCounter" is a mistake, or whether the original
programmer really did intend for there to be two separate variables? You
really can't tell. You have go line-by-line looking for undeclared
variables. Debugging becomes nearly impossible.

Also, if you don't declare your variables, VBA declares them As Variant.
Variant variables add considerable overhead to the processing because behind
the scenes, VBA must execute code to determine what type of value is stored
within the Variant and then do type conversions to get the value out of the
Variant. Variants should be used sparingly, only when absolutely necessary.

In summary, ALWAYS use Option Explicit.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"Ronbo" wrote in message
...
Jim -

I did not see your writting on "Option Explicit" until I re-read. Your
programming worked perfect and I did not continue to read.

I do see the grurs and some in particular, including you, suggesting the
"Option Explicit".

I have never taken the time to figure out why or how. Should I?


"Jim Thomlinson" wrote:

You need to define the addresses as a range something like this...

with sheets("Sheet1")
tmpName = Left(.Range("B1"), 3) & " " & Right(.Range("B1"), 2)
end with
MsgBox tmpName

As a complete aside you should have Option Explicit at the top of your
module. Oddly enough it would catch this error for you as you would end
up
with undeclared varaibles that would not compile... If you have more
questions about that just ask...
--
HTH...

Jim Thomlinson


"Ronbo" wrote:

What is the VBA routine that is the equivalent to the formula;

= Left(B1, 3) & " " & Right(B1, 2)

When I use:

tmpName = Left(B1, 3) & " " & Right(B1, 2)
MsgBox tmpName

In works without an error, but the message box is empty?

As always, any help is appreciated.

Regards
Ronbo



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
Regression Leverage Formula (Jerry W. Lewis or Mike Middleton)already have DFITS formula PJ[_3_] Excel Worksheet Functions 2 June 2nd 10 03:45 PM
copy formula down a column and have cell references change within formula brad New Users to Excel 5 May 13th 07 04:38 PM
Build excel formula using field values as text in the formula val kilbane Excel Worksheet Functions 2 April 18th 07 01:52 PM
Formula expected end of statement error, typing formula into cell as part of VBA macro [email protected] Excel Programming 1 July 20th 06 07:58 PM
Commenting custom formula fields/formula on formula editor Muxer Excel Programming 2 July 24th 03 01:02 AM


All times are GMT +1. The time now is 12:22 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"