Serving the Quantitative Finance Community

 
User avatar
Late
Topic Author
Posts: 0
Joined: August 25th, 2007, 9:49 am

Couple of simple VBA questions

November 26th, 2008, 6:31 pm

HiI am quite new to VBA programming having mostly coded C++ and with Matlab and I need to be able to do some simple things with VBA.First question: How can I define a macro that runs every time a certain cell changes in a worksheet? I know that there exists these pre defined worksheet events and one that should suit this problem is the Worksheet_Change event. However the problem is that the cells that I want to monitor change from user selected drop down lists and if user selects a new selection from the drop down lists the list changes the monitored target cell. But the problem is that the Worksheet_Change event does not run if the cell change comes from a formula. I need to manipulate manually the target cell if I want the Worksheet_Change macro to run and this is not the case here. Is there some other nice method that can solve this?The other problem is that I receive the function ouput from calling external C++ code that returns me a vector whose size is determined only when the external C++ code has returned the result. I want to substitute this vector (whose size I can define in the VBA code) to the first column in the workbook. Suppose the vector is stored in output VECTOR and suppose its size is "size". What I want to do is the substitution Range("A1:A"&size) = VECTOR but this does not work. As this does not work I had to use the method to substitute it to large enough array Range("A1:A100") = VECTOR, but then the extra unused slots get the #N/A error in these cells and I want to find the elegant answer to substitute to the correct amount of space.If someone could show here some lines of code how to handle this it would be appreciated. I want to avoid for loops.thanks!
 
sjoo
Posts: 0
Joined: March 24th, 2003, 1:54 am

Couple of simple VBA questions

November 27th, 2008, 3:16 am

Hi LateThe first:You're right! the Worksheet_Change event is triggered only when a user changes a value. You can solve the trouble with an event procedure of DropDown as following:'///////////////////////////////////////Sub DropDown1_Change() Dim rng As Range Set rng = Sheet1.Range("selectedtext") MsgBox rng.ValueEnd Sub'///////////////////////// among the above codes, Range("selectedtext") is a cell containging a formula,=INDEX(yourlist,A2)yourlist is a range containing a listA2 is an index of selected item from the Dropdown control.The Second:As I know, You should use an array instead of STL's vector.I made the related code before but I can not find where that is stored right now.and more there is an error on uploading an excel file for the first problem.If you mail me, I'll send the files.Plz email me sjoo.kwak at gmail.com take care
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Couple of simple VBA questions

November 27th, 2008, 12:20 pm

I think what you need is a range formula for the second part.
 
User avatar
jpsnj
Posts: 0
Joined: February 12th, 2008, 2:42 pm

Couple of simple VBA questions

December 1st, 2008, 2:16 pm

vector = Array(1, 2, 3, 4, 5)Range("A1:A" & UBound(vector) + 1).Value = Application.Transpose(vector) ORRange("A1").Resize(UBound(vector) + 1, 1).Value = Application.Transpose(vector)
Last edited by jpsnj on November 30th, 2008, 11:00 pm, edited 1 time in total.
 
User avatar
StatGuy
Posts: 0
Joined: November 20th, 2007, 9:03 am

Couple of simple VBA questions

December 6th, 2008, 11:54 pm

OR vector = Array(1, 2, 3, 4, 5)Range(Cells(1, 1), Cells(UBound(vector) + 1, 1)).Value = Application.Transpose(vector)