VBA String Functions

Today we will discussed about String in VBA, In any Programming language String plays a vital roles for its operations, like for storing information, displaying information and other many ways. When we are talking about String then a question arises in our Mind basically what is String if some have to say define a String in One line then what will be our answer.

String is basically a series of Characters. Now we will discuss some basic Operations on String and some functions of String in VBA.

As we know that in VBA to declare a variable we are following a syntax and that syntax is shown below

Syntax :   Dim var_name as data_type

For declaring a variable as String we will write

Dim myFirstString as String


Hence we are explaining it with an example in which we will take a variable and print this variable inside excel in cell(1,1)

Code :

Sub string_Operation()

Dim myString As String

     myString = "Hey it a String"

     ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value = myString

End Sub          
Press Alt +F8 and window Open   



OutPut :


Concatenate two String variables in VBA

I have two or more than two variables and I  want to concatenate these variables then I can complete this task with two methods.
Method 1 :

This method is also used in Object Oriented Programming languages like Java also for concatenating two Strings in this case we use “+” for concatenating two Strings.

Syntax : Stinng1 + String

Suppose you want to print gap between these two Strings then in that case
String1 +” “+String2

We explain it in more detail with example suppose I have two variables and I want to concatenate these two variables and Stores inside first row of Excel Sheet

Code:

Sub concatenate_String()

Dim fname As String
Dim lname As String
Dim name As String

         fname = "Dheeraj"
         lname = "Kumar"
        name = fname + " " + lname

        ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value = name

End Sub

OutPut :


Other VBA String Functions

Comments

Popular posts from this blog

Export data from mysql db to csv file using java

Secure Database Connectivity in node.js with mysql

Java Web Scraping