Tuesday, March 12, 2013

VBA - Retrieve Last Row From More Than One Column


Normally, its enough to know the last row in a specific column. In those cases I normally just use this widely method:

`Getting last row from column A

LastRow = Range("A" & Rows.Count).End(xlUp).row

But when you have several columns (for example 1 to 10) and you need to retrieve the last cell in use in any of these columns, we need something else.

VBA - Get name of file without extension

The easiest way to get the name of a file is of course to use ThisWorkbook.Name. It will supply you with the name and the extension of the file (for example "MyWorkbook.xlsx").But if you want to retrieve only the name the workbook and not the extension, you’ll need this:



Dim NameOfWorkbook

NameOfWorkbook = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))



It uses the function InStrRev to find the last occurance of "." and the Left() function is then used to assign all chars left of this position to the NameOfWorkbook variable.

Tuesday, February 5, 2013

MS Excel: VBA Functions - Logical Functions

CASE (VBA)

IF-THEN-ELSE (VBA)

Information Functions

ISDATE (VBA)

ISERROR (WS, VBA)

ISNULL (VBA)

ISNUMERIC (VBA)

MS Excel: VBA Functions - Numeric / Mathematical Functions

ABS (WS, VBA)


ATN (VBA)

COS (WS, VBA)

EXP (WS, VBA)

FIX (VBA)

FORMAT Numbers (VBA)

INT (WS, VBA)

LOG (WS, VBA)

RND (VBA)

ROUND (VBA)

SGN (VBA)

SIN (WS, VBA)

TAN (WS, VBA)

MS Excel: VBA Functions - String Functions

ASC (VBA)


CHR (VBA)

Concatenate with & (WS, VBA)

CURDIR (VBA)

FORMAT Strings (VBA)

INSTR (VBA)

INSTRREV (VBA)

LCASE (VBA)

LEFT (WS, VBA)

LEN (WS, VBA)

LTRIM (VBA)

MID (WS, VBA)

REPLACE (VBA)

RIGHT (WS, VBA)

RTRIM (VBA)

SPACE (VBA)

STR (VBA)

STRCONV (VBA)

TRIM (WS, VBA)

UCASE (VBA)

VAL (VBA)

GETATTR Function (VBA)

In Microsoft Excel, the GETATTR function returns an integer that represents the attributes of a file, folder, or directory.


Syntax

The syntax for the GETATTR function is:

GetAttr ( path )path is the path to a file, folder, or directory that you wish to retrieve the attributes for.

VBA Function Example

The GETATTR function can only be used in VBA code. Here are some examples of what the GETATTR function would return:

GetAttr("C:\Chithu.doc") would return 0

GetAttr("H:\Documents\Chithu.xls") would return 1

REPLACE function (VBA)

In Microsoft Excel, the REPLACE function replaces a sequence of characters in a string with another set of characters. Please note that the worksheet version of the REPLACE function has different syntax.


Syntax:-
 
The syntax for the REPLACE function is:
 
Replace ( string1, find, replacement, [start, [count, [compare]]] )string1 is the string to replace a sequence of characters with another set of characters.

  • find is the string that will be searched for in string1.
  • replacement will replace find in string1.
  • start is optional. This is the position in string1 to begin the search. If this parameter is omitted, the REPLACE function will begin the search at position 1.
  • count is optional. This is the number of occurrences to replace. If this parameter is omitted, the REPLACE function will replace all occurrences of find with replacement.
 
VBA Function Example:-
 
The REPLACE function can be used in VBA code. Here are some examples of what the REPLACE function would return:

Replace("cpalani1", "palani1", "hidambaram") would return "chidambaram"