0

I am trying to write a couple lines of code in VBA for excel, here is what the VB.Net version of the code looks like.

            ThisVariable <<= 8

        Variable.Add(ThisVariable)

How do I write these lines of code in VBA? VBA does not have the "<<=" operator and does not have the .Add property of an array. Any help would be greatly appreciated.

2 Answers 2

2

For the first question, VBA doesn't have built in bit shifting, but you can add a function that does it for you. Try this one.

For the second question, to increase an array size, you have to use the ReDim command. Here's the info on that.

Also, you could use a collection instead of an array. With a collection, you can add & delete item within it at will.

Good luck

Sign up to request clarification or add additional context in comments.

Comments

1

<<= left-shifts the number by 8 binary digits, i.e. multiplies the number by 256. For adding something to an array you can re-dim the array:

ThisVariable = ThisVariable * 256
Dim U As Long
Dim L As Long
L = LBound (Variable)
U = UBound (Variable)
ReDim Preserve Variable (L To U+1)
Variable (U+1) = ThisVariable

Note that redim'ing the array just to add an element is not very efficient. You should try to find another approach (e.g. use a larger array and store the number of "valid" elements in a counter variable -- or even create a class module for that).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.