The post ActiveX Controls – ComboBox in Excel VBA appeared first on OfficeInside.Org.

]]>The **VBA ComboBox** is a drop-down list that you can use in Excel. Remember, that this is not the same type of list that you can enter in Excel for data correctness. As in the case of a **VBA TexBox**, to insert a VBA ComboBox, go to the **Developer tab**. Turn on the **design mode**, then select the ComboBox from ActiveX controls.

After inserting Combo Box, check its **Properites **right-clicking on box, which you inserted on **Excel Sheet**. First value is the system name of your Combo Box. It will be using in programming this element.

As in the case of a text box, combo boxes should be properly programmed. The combo boxes are programmed as follows:

- Go to the
**VisualBasic Editor**and select the “This Workbook” object on the left or “This Workbook” depending on the version of Excel - In the Code window we set the “Workbook” object and the “Open” trigger. Thanks to this, the values in the combo boxes will be loaded every time you open the workbook.
- To enter values into a combo box list, we refer to the appropriate sheet and combo box as
**SheetName.CombiName “Value that should appear in the field”**. In this way, we enter all our values in the following lines of the code, e.g. voivodeships. In our case, the name of the combo box will be ComboBox1 - It is important that you save, close and reopen the Excel file each time you edit the value in the combo box.

Private Sub Workbook_Open () Arkusz1.ComboBox1.AddItem "dolnośląskie" Arkusz1.ComboBox1.AddItem "kujawsko-pomorskie" Sheet1.ComboBox1.AddItem "lubelskie" '(...) End Sub

If you are using a UserForm and not controls embedded in a worksheet, be sure to read one of the next chapters of the VBA course on inserting a ComboBox in a UserForm .

If there are many values, we should use the **With** clause . We only refer to combo box and worksheet this way once and save time writing our code

Private Sub Workbook_Open () With Sheet1.ComboBox1 .AddItem "dolnośląskie" .AddItem "kujawsko-pomorskie" .AddItem "lubelskie" .AddItem "lubuskie" .AddItem "łódzkie" .AddItem "małopolskie" .AddItem "mazowieckie" .AddItem "opolskie" .AddItem "podkarpackie" .AddItem "podlaskie" .AddItem "pomorskie" .AddItem "śląskie" .AddItem "świętokrzyskie" .AddItem "warmińsko-mazurskie" .AddItem "wielkopolskie" .AddItem "Zachodniopomorskie" End With End Sub

The code entered in the above way should be closed using the **End With** expression . It is also important to save, close and reopen the Excel file each time you edit the value in the combo box.

At this point, we can already use the combo box functionality. If we want to program successive fields corresponding to the day, month and year of birth, the easiest way is to use the **For … Next loop** . If we want the ComboBox2 field responsible for the day of birth to display values from 1 to 31 like the days of the month, we should program the combo box as follows:

With Sheet1.ComboBox2 Dim intLicznikBox2 As Integer For intLicznikBox2 = 1 To 31 .AddItem intCountBox2 Next End With

The combo box is programmed as before in the ” **This workbook** ” object code . Displaying the days of the week as numbers from 1 to 31 is of course a simplification, as not every month has so many days. We can program the code of our application in such a way as to eliminate such cases. To complete filling the values in combo boxes 3 and 4, we program the code responsible for the month and year in the same way. For a change, let’s also use a While..Wend loop to create values in our fields. Our program code in the ” **This Workbook** ” object should look like this:

Private Sub Workbook_Open () With Sheet1.ComboBox1 .AddItem "dolnośląskie" .AddItem "kujawsko-pomorskie" .AddItem "lubelskie" .AddItem "lubuskie" .AddItem "łódzkie" .AddItem "małopolskie" .AddItem "mazowieckie" .AddItem "opolskie" .AddItem "podkarpackie" .AddItem "podlaskie" .AddItem "pomorskie" .AddItem "śląskie" .AddItem "świętokrzyskie" .AddItem "warmińsko-mazurskie" .AddItem "wielkopolskie" .AddItem "Zachodniopomorskie" End With With Sheet1.ComboBox2 Dim intLicznikBox2 As Integer For intLicznikBox2 = 1 To 31 .AddItem intCountBox2 Next End With With Sheet1.ComboBox3 Dim intLicznikBox3 As Integer intCountBox3 = 1 While intLumerBox3 <= 12 .AddItem intCountBox3 intCountBox3 = intCountBox3 + 1 Wend End With With Sheet1.ComboBox4 Dim intLicznikBox4 As Integer For intLicznikBox4 = 1980 To 2000 .AddItem intCountBox4 Next End With End Sub

And this is the result of our script:

To enter data from our combo boxes into the table, we should enter the reference to the ComboBox elements 1,2,3,4 in the code of the Sheet1 object. In the case of fields responsible for the day, month, year, we should use the **DateSerial ()** function to create a date from our elements. After modifying the code, we can test it. Remember that we are editing this part of the code in Sheet1.

Private Sub CommandButton1_Click () Dim sngId As Single Dim sngWiersz As Single Dim sngLogin Counter of As Single 'SELECTING THE LAST UNSAVED LINE AND ID sngId = 1 + Application.WorksheetFunction.Max (Range ("A: A")) sngWiersz = 6 + Application.WorksheetFunction.CountA (Range ("A: A")) 'CHECK LOGIN sngLogin Counter = 7 Do While sngLoginCount <= sngWiersz If Cells (sngLoginCount, 2) = LCase (TextBox1) Then MsgBox "This login is already in the database, please enter another", vbCritical GoTo endlabel End If sngLoginCount = sngLoginCount + 1 Loop 'DATA INPUT Cells (sngWiersz, 1) = sngId Cells (sngWiersz, 2) = LCase (TextBox1) Cells (sngWiersz, 3) = UCase (Left (TextBox2, 1)) & LCase (Mid (TextBox2, 2)) Cells (sngWiersz, 4) = StrConv (TextBox3, vbProperCase) Cells (sngWiersz, 5) = LCase (TextBox4) Cells (sngWiersz, 6) = ComboBox1 Cells (sngWiersz, 7) = DateSerial (ComboBox4, ComboBox3, ComboBox2) endlabel: End Sub

The combo box is already known to us. We got to know them while creating the application for entering data . Due to the fact that the field inserted in the UserForm form is programmed in a different place, we will create an example in which we will show how to program it. Select the ComboBox control from the **ToolBox** and put it to the right of the TextBox1 and TextBox3 fields in the first two tabs of our application. Our fields shouldto be called ComboBox1 and ComboBox2.

The next step is to program the list that will be displayed after clicking on the field. Unlike ActiveX controls inserted into the data sheet, the ComboBox field in the UserForm form is programmed directly in the UserForm form code. We go successively in the project window to the **View Code** view of the UserForm form. The most important thing is to set the **UserForm** object and the **Initialize** trigger in the **Code** window . As a result, the values to the ComboBox field will load when the form is launched.

The code in the **Initialize** trigger is programmed in the same way as in the case of regular ActiveX controls. We set the values right away for both combo boxes. The example below:

Private Sub UserForm_Initialize() With ComboBox1 .AddItem 5 .AddItem 8 .AddItem 23 End With With ComboBox2 .AddItem 5 .AddItem 8 .AddItem 23 End With End Sub

Next, we should program the **CommandButton** buttons in the first two tabs of our program. Double click on the button in the “Netto gross” tab. The CommandButton1 button is programmed in the following way using the TextBox1, TextBox2 and ComboBox1 fields:

Private Sub CommandButton1_Click() TextBox2 = TextBox1 + TextBox1 * (ComboBox1 / 100) End Sub

In the above code, we have programmed the TextBox2 field after choosing the VAT rate. The analogous operation is performed for the CommandButton2 button in the “Gross on net” tab. All of our code in the **UserForm** object looks like this:

Private Sub CommandButton1_Click() TextBox2 = TextBox1 + TextBox1 * (ComboBox1 / 100) End Sub Private Sub CommandButton2_Click() TextBox4 = TextBox3 / (1 + (ComboBox2 / 100)) End Sub Private Sub UserForm_Initialize() With ComboBox1 .AddItem 5 .AddItem 8 .AddItem 23 End With With ComboBox2 .AddItem 5 .AddItem 8 .AddItem 23 End With End Sub

At the end of the “About the program” tab, you can enter any text using, for example, the Label **label** . Place the label content in the frame by selecting **Frame** earlier.

Below I attach a file with our program:

The post ActiveX Controls – ComboBox in Excel VBA appeared first on OfficeInside.Org.

]]>The post CVar VBA Function – How to Convert Data to Variant appeared first on OfficeInside.Org.

]]>The **CVar VBA** function returns a value with the **Variant** data type . The Variant data type can store both numeric and text data. When using this function, remember that the Variant data type itself is the least efficient data type in VBA.

CVar (Expression) as Variant

**Expression** : Any value stored in any data type.

**Function return** : Variant / Any numerical or text value.

How to use **CVar** function in VBA Excel? Below are examples of using the CVar function in the VisualBasic Editor.

**Example 1:** Converting to Variant. CVar – function example. The result in the MsgBox window.

Dim varValue As Variant varValue = CVar ("any value") MsgBox varValue

**Example 1:** Converting to Variant. VBA CVar – example. The result in the worksheet cells.

Dim varValue As Variant varValue = CVar (123) Range ("A1") = varValue

- none

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The article is part of the VBA Excel function list. You can find a list of all VBA functions at this address: VBA functions .

The post CVar VBA Function – How to Convert Data to Variant appeared first on OfficeInside.Org.

]]>The post CStr VBA function – How to convert data to string type appeared first on OfficeInside.Org.

]]>The **CStr VBA** function returns a value of the **String** data type . The simplest form of the String data type with a variable number of characters takes up as many bytes in memory as there are characters in a given text string. We typically use functions when converting text stored in **Variant** to more efficient data types.

CStr (Expression) As String

**Expression** : Any value.

**Function return** : String / text value.

How to use **CStr** function in VBA Excel? Below are examples of using the **CStr** function in the VisualBasic Editor.

**Example 1:** Converting to a String type. CStr – VBA function example. The result in the MsgBox window.

Dim varValue As Variant varValue = "any text" MsgBox CStr (varValue)

**Example 2:** Converting to a String type. VBA CStr – function example. The result in a sheet cell.

Dim varValue As Variant varValue = "any text" Range ("A1") = CStr (varValue)

- lack

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The article is part of the VBA Excel function list. You can find a list of all VBA functions at this address: VBA functions .

The post CStr VBA function – How to convert data to string type appeared first on OfficeInside.Org.

]]>The post CSng VBA Function – How to Convert Data to Single Type appeared first on OfficeInside.Org.

]]>The **CSng VBA** function returns a value with the **Single** data type . It is a number between **-3.402823e38** and **1.401298e45** . The Single data type is 4 bytes in memory. We usually use functions when we want our data to be more efficient. Using functions for numbers out of range or for non-numeric data will return an error. This function can be used to convert a floating point number written with a comma, not a period, to a floating point number recognized by VBA. The function also converts text to number.

CSng (Expression) As Single

**Expression** : Any number between -3.402823e38 and 1.401298e45 stored in any data type (including String).

**Function return** : Single / A floating point number ranging from -3.402823e38 to 1.401298e45.

How to use CSng function in VBA Excel? Below are examples of using the CSng function in the VisualBasic Editor.

**Example 1:** Conversion to the Single type. CSng – function example.

Dim strValue As String strValue = "123.12" MsgBox CSng (strValue) / 100

**Example 2:** Converting to the Single type. VBA CSng – an example of a function.

Dim strValue As String strValue = "1233.12" Range ("A1") = CSng (strValue) * 5

- The function used for numbers outside the range -3.402823e38 through 1.401298e45 will return an error.
- This function can be used to convert a floating point number written with a comma, not a period, to a floating point number recognized by VBA.

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The article is part of the VBA Excel function list. You can find a list of all VBA functions at this address: VBA functions .

The post CSng VBA Function – How to Convert Data to Single Type appeared first on OfficeInside.Org.

]]>The post CLngLng VBA function – How to convert data to LongLong type appeared first on OfficeInside.Org.

]]>The **CLngLng VBA** function returns a value of the **LongLong** data type . It is an integer ranging from **-9,223,372,036,854,775,808** to **9,223,372,036,854,775,807** . The LongLong data type is 8 bytes in memory. It is important that the LongLong data type itself, unlike the Long type, only works in the 64-bit architecture. The function can also be used to convert a number stored as text to a number in the LongLong data type. Attempting to call a function with an argument outside the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 will generate an error. We will also get an error when we enter a value that contains characters other than digits as an argument, or we enter a floating point number as an argument

CLngLng (Expression) As LongLong

**Expression** : Any integer from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 stored in any data type (including String).

**Function return** : LongLong / Integer ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

How to use CLngLng function in VBA Excel? Below are examples of using the CLngLng function in the VisualBasic Editor.

**Example 1:**

Dim StrNumber As String StrNumber = "14522434566778" MsgBox CLngLng (StrNumber)

**Example 2:**

Dim intNumber As Integer intNumber = 32000 MsgBox CLngLng (intNumber)

- A function that is applied to numbers other than integers or outside the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 will return an error.
- The feature only works on 64-bit architecture, similar to the LongLong data type

The function can be used in: Excel 2013, Excel 2016.

The post CLngLng VBA function – How to convert data to LongLong type appeared first on OfficeInside.Org.

]]>The post VBA CLng Function – How to Convert Data to Long Type appeared first on OfficeInside.Org.

]]>The **CLng VBA** function returns a value of the **Long** data type . It is an integer in the range **–2,147,483,648** to **2,147,486,647** . The Long data type is 4 bytes in memory. The function can also be used to convert a number stored as text to a number in the Long data type. Attempting to call a function with an argument outside the range –2,147,483,648 to 2,147,486,647 will generate an error. We will also get an error if we enter a value that contains characters other than numbers as an argument.

CLng (Expression) As Long

**Expression** : Any integer between –2,147,483,648 and 2,147,486,647 stored in any data type (including String).

**Function return** : Long / An integer ranging from –2,147,483,648 to 2,147,486,647.

How to use CLng function in VBA Excel? Below are examples of using the CLng function in the VisualBasic Editor.

**Example 1:**

Dim StrNumber As String StrNumber = "14522478" MsgBox CLng (StrNumber)

**Example 2:**

Dim intNumber As Integer intNumber = 32000 MsgBox CLng (intNumber)

- When used on numbers other than integers or outside the range of –2,147,483,648 to 2,147,486,647, an error will be returned.

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The post VBA CLng Function – How to Convert Data to Long Type appeared first on OfficeInside.Org.

]]>The post CInt VBA function – How to convert data to Integer type appeared first on OfficeInside.Org.

]]>The **CInt VBA** function returns a value of the **Integer** data type . It is an integer in the range **-32768 to 32767** . The Integer data type takes 2 bytes in memory and is therefore very efficient. This way, we can convert low-valued numbers to store them in more

efficient data types. The function can also be used to convert a number written as text to a number in the Integer data type. Attempting to call a

function with an argument outside the range -32768 to 32767 will generate an error.

CInt (Expression) As Integer

**Expression** : Any integer in the range -32768 to 32767 stored in any data type (including String).

**Function return** : Integer / An integer in the range -32768 to 32767.

How to use the CInt function in VBA Excel? Below are examples of using the CInt function in the VisualBasic Editor.

**Example 1:**

Dim StrNumber As String StrNumber = "255" MsgBox CInt (StrNumber)

**Example 2:**

Dim LngNumber As Long LngNumber = 123 MsgBox CInt (LngNumber)

- A function used for numbers other than integers or outside the range -32768 to 32767 will return an error.

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The post CInt VBA function – How to convert data to Integer type appeared first on OfficeInside.Org.

]]>The post CDbl VBA Function – How to Convert Data to Double Type appeared first on OfficeInside.Org.

]]>The **CDbl VBA** function returns a value with the **Double** data type . It is a number between **-1.79769313486232e +308** to **-4.94065645841247E-324** . The Double data type is 8 bytes in memory. We usually use functions when converting very large floating point numbers. Using the function for numbers outside the range or for non-numeric data will return an error. This function can be used to convert a floating point number written with a comma, not a period, to a floating point number recognized by VBA. The function converts text to a number.

CDbl (Expression) As Double

**Expression** : Any number from -1.79769313486232e + 308 to -4.94065645841247E-324 stored in any data type (including String).

**Function return** : Double / A floating point number ranging from -1.79769313486232e + 308 to -4.94065645841247E-324.

How to use **CDbl** function in VBA Excel? Below are examples of using the **CDbl** function in the VisualBasic Editor.

**Example 1:** Conversion to type Double. CDbl – function example. The result in the MsgBox window.

Dim strValue As String strValue = "1234233,12343" MsgBox CDbl (strValue)

**Example 2:** CDbl – function example. Convert to Double. The result in a sheet cell.

Dim strValue As String strValue = "1234233,12343" Range ("A1") = CDbl (strValue) * 5

- The function used for numbers outside the range -1.79769313486232e + 308 to -4.94065645841247E-324 will return an error.
- This function can be used to convert a floating point number written with a comma, not a period, to a floating point number recognized by VBA.

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The post CDbl VBA Function – How to Convert Data to Double Type appeared first on OfficeInside.Org.

]]>The post CDate VBA Function – How to Convert Data to Date appeared first on OfficeInside.Org.

]]>The **CDate VBA** function returns a value with the **Date** data type . The Date data type is 8 bytes in memory. The function converts the date and time saved in text form to VBA formatted date and time. The function works like the **VBA DateValue** and **VBA TimeValue** functions when replacing text with a date . The difference is that CDate can return both date and time.

CDate (Expression) As Date

**Expression** : Date and time saved as text e.g. 25-12-2018, 1 March 2018, 2018-05-19.

**Function return** : Date / Date and time

How to use CDate function in VBA Excel? Below are examples of using the CDate function in the VisualBasic Editor.

**Example 1** : Example of VBA CDate Function – result in the MsgBox window. Text on VBA date and time.

Dim strValue As String strValue = "March 1, 2018 1:5:15 am" Range ("A1") = CDate (strValue)

**Example 2** : Example of VBA CDate Function – result in the MsgBox window. Convert text to date and time.

Dim strValue As String strValue = "March 1, 2018 1:5:15 am" Range ("A1") = CDate (strValue)

- The function can take various time or date and time formats as an argument, e.g. “1: 5:15”, or # 12/28/2017 11:15:55 AM #, 25-12-2018, March 1, 2018, May 5, 2018 19.
- Unlike the
**DateValue**and**TimeValue**functions , it converts text to a date and time at the same time.

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The post CDate VBA Function – How to Convert Data to Date appeared first on OfficeInside.Org.

]]>The post CCur VBA Function – How to Convert Data to Currency Type appeared first on OfficeInside.Org.

]]>The **CCur VBA** function returns a value with the **Currency** data type . It is a floating point number formatted as a currency ranging from **-922,337,203,477.5808** to **922,337,203,685,477.5807** . The Currency data type is 8 bytes in memory. . Attempting to call a function with an argument outside the range -922,337,203,477.5808 to 922,337,203,685,477.5807 will generate an error. We will also get an error if we enter a value that contains characters other than numbers as an argument. In practice, the currency type is rarely used. As a rule, the usual Single or Double types are used to hold numbers that represent monetary amounts.

CCur (Expression) As Currency

**Expression** : Any number from -922,337,203,477.5808 to 922,337,203,685,477.5807 stored in any data type (including String).

**Function return** : Currency / Number ranging from -922,337,203,477.5808 to 922,337,203,685,477.5807.

How to use CCur function in VBA Excel? Below are examples of using the CCur function in the VisualBasic Editor.

**Example 1** : Example of VBA CCur function – result in the MsgBox window.

Dim CurValue As Currency CurValue = 12345.1234 MsgBox CCur (CurValue)

**Example 2** : CCur VBA – Application example. The result in a sheet cell.

Dim CurValue As Currency CurValue = 4567.1234 Range ("A1") = CCur (CurValue)

- The function used for numbers outside the range -922,337,203,477.5808 to 922,337,203,685,477.5807 will return an error.

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The post CCur VBA Function – How to Convert Data to Currency Type appeared first on OfficeInside.Org.

]]>