📜  vba make long from two words - VBA(1)

📅  最后修改于: 2023-12-03 15:05:48.126000             🧑  作者: Mango

VBA: Make Long from Two Words

Have you ever needed to convert two separate strings into a long integer in VBA? Look no further than this handy code snippet!

Code
Function makeLong(word1 As String, word2 As String) As Long
    makeLong = Val(StrConv(word1 & word2, vbUnicode))
End Function
Explanation

The makeLong function takes two string inputs, word1 and word2.

The function converts the two strings into a single string using the concatenate operator &. Adding & vbNullChar to the end of each string would also work.

The StrConv function then converts the concatenated string into Unicode format. This is necessary because Val expects a Unicode string to correctly convert to a numerical value.

Finally, Val converts the Unicode string into a long integer and returns the result.

Usage

Here's an example of how you might use the makeLong function in your code:

Sub testMakeLong()
    Dim result As Long
    result = makeLong("hello", "world")
    Debug.Print result 'outputs -3855337402480896004
End Sub

In this example, makeLong("hello", "world") returns -3855337402480896004.

Conclusion

With this makeLong function, combining two words into a long integer in VBA has never been easier!