VBA shorthand for x=x+1? - vba

Sub btn1_Click()
Static value As Integer
value = value + 1
MsgBox value
End Sub
I swear when I was taking a VB.net course in college there was a shorter way to tell a variable to add '' to itself. Maybe x=+1. I am using Access now though instead of visual studio. When I try that within the VBE it removes the +. I even removed Option Explicit with no change
Assuming the answer will be no, there is no way to short-hand it & its just a peculiarly of VBA

Sadly there are no operation-assignment operators in VBA.
(Addition-assignment += are available in VB.Net)
Pointless workaround;
Sub Inc(ByRef i As Integer)
i = i + 1
End Sub
...
Static value As Integer
inc value
inc value

If you want to call the incremented number directly in a function, this solution works bettter:
Function inc(ByRef data As Integer)
data = data + 1
inc = data
End Function
for example:
Wb.Worksheets(mySheet).Cells(myRow, inc(myCol))
If the function inc() returns no value, the above line will generate an error.

Related

Considerations when using Function name as a variable

In VBA, you can treat a UDF name just like a Dimmed variable. For example
Function ADD(a, b) As Long
ADD = a
ADD = ADD + b
End Function
Where ADD stores the intermediate value as well as the end result. My question is; in terms of what's going on behind the scenes, is there any difference between storing a number/piece of data in a standard variable vs a function name variable.
I worry that perhaps the routine that called the function gets an update whenever the variable changes if you use the function name, rather than only when End Function is executed. IE. if you have some code
answer = ADD(1, 2)
then in memory answer gets written to twice, once when ADD = a, once when ADD = ADD + b. Of course we don't see this, because answer is left with whatever the final ADD value is
I ask because I often find I build up a function answer in stages using an intermediate variable, then pass that to the function name itself, where instead I could just write directly to the function name.
E.g.
Function ADD(a, b) As Long
Dim tot As Long
tot = a
tot = tot + b
ADD = tot
End Function
vs the first example. They acheive the same thing, in the second example tot represents the formula result, and so we need a final ADD = tot line at the end. For speed I would like to reduce any writes that are made, so is there any drawback in terms of speed, not readability to using the first method as opposed to declaring intermediates?
NB, to clarify, that's not all intermediates, I just mean the single intermediate that represents the function result, and could be replaced by the function name in the code.
In speed the first method should be slightly faster - you declare one variable less (but I doubt that someone would be able to notice it).
In general, using the first method can bring you to a recursion, if you are not careful (or if you are a VBA beginner):
Option Explicit
Dim lng10 As Long
Public Sub Test()
lng10 = 0
Debug.Print ADD2(1, 1)
End Sub
Function ADD2(a As Long, b As Long) As Long
lng10 = lng10 + 1
ADD2 = a + b
If lng10 < 10 Then
ADD2 = ADD2 + ADD2(1, 1)
End If
End Function
And if the recursion does not have a bottom, it would go to an overflow error.
With other words, this would be an runtime error:
Option Explicit
Dim lng10 As Long
Public Sub Test()
lng10 = 0
Debug.Print ADD2(1, 1)
End Sub
Function ADD2(a As Long, b As Long) As Long
lng10 = lng10 + 1
ADD2 = ADD2(a, b)
End Function

Get the nth character, string, or number after delimiter in Visual Basic

In VB.net (Visual Studio 2015) how can I get the nth string (or number) in a comma-separated list?Say I have a comma-separated list of numbers like so:13,1,6,7,2,12,9,3,5,11,4,8,10How can I get, say, the 5th value in this string, in this case 12?I've looked at the Split function, but it converts a string into an array. I guess I could do that and then get the 5th element of that array, but that seems like a lot to go through just to get the 5th element. Is there a more direct way to do this, or am I pretty much limited to the Split function?
In case you are looking for an alternative method, which is more basic, you can try this:
Module Module1
Sub Main()
Dim a As String = "13,1,6,7,2,12,9,3,5,11,4,8,10"
Dim counter As Integer = 5 'the number you want (in this case, 5th one)
Dim movingcounter As Integer = 0 'how many times we have moved
Dim startofnumber, endofnumber, i As Integer
Dim numberthatIwant As String
Do Until movingcounter = counter
startofnumber = InStr(i + 1, a, ",")
i = startofnumber
movingcounter = movingcounter + 1
Loop
endofnumber = InStr(startofnumber + 1, a, ",")
numberthatIwant = (Mid(a, startofnumber + 1, endofnumber - startofnumber - 1))
Console.WriteLine("The number that I want: " + numberthatIwant)
Console.ReadLine()
End Sub
End Module
Edit: You can make this into a procedure or function if you wish to use it in a larger program, but this code run in console mode will give the output of 12.
The solution provided by Plutonix as a comment to my question is straightforward and exactly what I was looking for, to wit:result = csv.Split(","c)(5)In my case I was incrementing a variable each time my program ran and needed to get the nth character or string after the incremented value. That is, if my program had incremented the variable 5 times, then I needed the string after the 4th comma, which of course, is the 5th string. So my solution was something like this:result = WholeString.Split(","c)(IncrementedVariable)Note that this is a zero-based variable.Thanks, Plutonix.

How to apply bitwise formula to a range using VBA

I am looking for a way to apply a bitwise AND on a value in excel and apply it on range. Here is a picture to illustrate :
I want to apply a bitwise AND on the Column "Status" with a fixed value, lets say (1001b).
If Status & 1001b = 1001b, then report the "Value" field in "Output". Else, put 0.
Both Excel and VBA can be used, but as the table can be huge, the solution may not use loops (Too slow, time efficient solution would be great). I am using excel 2010, and therefore the excel BITAND function is not available for me (only available since Excel 2013 version)
I search on SO and internet and didn't find a correct approach to a similar problem.
I try to optimize my initial solution which use recursivity, to be more time efficient, here is what I have done :
For cptDonnee_s32 = 0 To JeuDonnee_rs.RecordCount - 2 ' Loop through all my data
If (Value And 8) = 8) And (Value And 1) = 1) Then ' Compare bitfields one by one
Output = Value ' Replace output by value here
Else
Output = 0 ' put 0 in output there
End If
next
You could use a simple UDF:
Function BitAnd(dIn As Double, lMatch As Long) As Boolean
BitAnd = (dIn And lMatch) = lMatch
End Function
and enter:
=if(BitAnd(K2,9),L2,0)
and copy down.

Count characters between two empty space to dashes() in vba

How do I get the length of character between beginning with space and ending with * Here is the image. Column B shows the total len before dasher(-) and my code
Sub xn()
Dim x As Integer
x = 1
If Worksheet("Sheet1").Range("A"& x).len(Right," ") Or _
Worksheet("Sheet1").Range("A"&x)len(Left,"-") Then
len(totallen)
End If
x = x + 1
End Sub
The code posted has multiple issues:
Worksheet is not a valid object - you need to use Worksheets.
.len is not a property of a Range object.
Even in .len was a property of a Range, you would need a
de-reference operator (aka '.') in here: Range("A"&x)len(Left,"-")
If you intend to use the function Len(), it only takes one argument.
You apparently are trying to loop, but you need to use either a For
or For Each loop - it won't loop automatically when you increment x
at the bottom of the sub.
Right is a function, but you're calling it without arguments and they are not optional.
Similarly, Left is a function, but you're also calling it without
the required arguments.
totallen is not declared anywhere, so Len(totallen) will assume
that totallen is a Variant (default for undeclared variables), then
cast it to a String, and then always return 0 because it has never
been given a value.
Anything else I may have missed.
The solution is to use the InStr function. It returns the location in a string of a given sub-string.
Sub xn()
Dim x As Long
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Worksheets("Sheet1")
For x = 1 To sheet.Range("A" & sheet.Rows.Count).End(xlUp).Row
sheet.Cells(x, 2) = InStr(1, sheet.Cells(x, 1), "-") - 1
Next x
End Sub
I'd also recommend taking a look at the MSDN article on Looping Through a Range of Cells (2003 vintage, but still valid), and Error Finding Last Used cell In VBA.

VB.Net Replace not working?

Not sure if I'm doing something wrong or not, basically my code starts at "111111111" and counts up by adding "1" to the original number every time the thread is able to. I want the method to skip 0's in the sequence though, instead of going to "111111120" after "111111119" I would like it to go straight to "111111121".
Private Sub IncreaseOne()
If count < 999999999 Then
count += 1
Else
done = True
End If
If CStr(count).Contains("0") Then
MsgBox("theres a 0 in that...darn.")
CStr(count).Replace("0", "1")
End If
End Sub
*note, my message box displays when it is suppose to but, 0s are not changed to 1s
Replace returns a string with the effects of the Replace, It doesn't work in place....
(Remember, in NET the strings are immutable objects)
Dim replaced = CStr(count).Replace("0", "1")
However you need to convert the string obtained to an integer and reassign to count.
count = Convert.ToInt32(replaced)
Replace is a function that returns a sting.
In other words, you need a variable to hold the result, like this:
Dim newValue = CStr(count).Replace("0", "1")