Solved

How do I call a function within a VBA procedure ?

Posted on 2013-11-04
3
518 Views
Last Modified: 2013-11-04
Hi Experts,

I am creating a VBA procedure within my Excel 2010 application in which I need to sort a 2-dimensional array.

Having scoured the Web I seem to have found a more robust VBA code example of a Sort function. However, not having ever used functions before, the function is falling over at the first stage, that is, it gives me compilation errors.

This is the code within my procedure:

   Dim ClientsArray() As Variant
   Dim InputArray() As Variant

' Load Clients array from Clients sheet (B3:L).........This works fine !
   ClientsArray() = Range(Sheets("Clients").Cells(3, 2), Sheets("Clients").Cells(Sheets("Clients").Cells(3, 1).End(xlDown), 12)).Value

   InputArray = ClientsArray()

'Sort the array by Name (2nd column) ascending........This doesn't work !
   ClientsArray() = QSortInPlace( _
    ByRef InputArray As Variant, _
    Optional ByVal LB As Long = -1&, _
    Optional ByVal UB As Long = -1&, _
    Optional ByVal Descending As Boolean = False, _
    Optional ByVal CompareMode As VbCompareMethod = vbTextCompare, _
    Optional ByVal NoAlerts As Boolean = False) As Boolean

The above piece of code is obviously wrong as it highlights in red but I can't seem to get the syntax right. I've attached the called function code in a text file.

Any help will be appreciated.
Toco
Sort-Function-VBA-code.txt
0
Comment
Question by:Tocogroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 39620954
Hi

you cannot use a function and declare it  at the same time

  Dim ClientsArray() As Variant
   Dim InputArray() As Variant

' Load Clients array from Clients sheet (B3:L).........This works fine !
   ClientsArray() = Sheets("Clients").Range(Cells(3, 2), Cells(Cells(3, 1).End(xlDown), 12)).Value

   InputArray = ClientsArray()

'Sort the array by Name (2nd column) ascending........This doesn't work !
   bIsSorted = QSortInPlace(ClientsArray)
'...
End Sub

Function QSortInPlace()
'...
End Function

Open in new window

Regards
0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 250 total points
ID: 39620960
Try

   successful = QSortInPlace(InputArray, LB, UB, Descending, CompareMode, NoAlerts)
0
 

Author Closing Comment

by:Tocogroup
ID: 39620992
Thanks both for your solutions. I understand now that this function returns a Boolean 'result'. However, what the function didn't tell me until I invoked it was that it only works for single arrays.

Back to the drawing board.
Thanks again
Toco
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question