Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Call VBA Function from Excel Cells? [duplicate]

Tags:

excel

vba

I am a VBA newbie, and I am trying to write a function that I can call from Excel cells, that can open a workbook that's closed, look up a cell value, and return it.

So far I know how to write a macro like this:

Sub OpenWorkbook()     Dim path As String     path = "C:\Users\UserName\Desktop\TestSample.xlsx"      Dim currentWb As Workbook     Set currentWb = ThisWorkbook       currentWb.Sheets("Sheet1").Range("A1") = OpenWorkbookToPullData(path, "B2") End Sub   Function OpenWorkbookToPullData(path, cell)      Dim openWb As Workbook     Set openWb = Workbooks.Open(path, , True)      Dim openWs As Worksheet     Set openWs = openWb.Sheets("Sheet1")      OpenWorkbookToPullData = openWs.Range(cell)      openWb.Close (False)  End Function 

The macro OpenWorkbook() runs perfectly fine, but when I am trying to call OpenWorkbookToPullData(...) directly from an Excel cell, it doesn't work. The statement:

    Set openWb = Workbooks.Open(path, , True) 

returns Nothing.

Does anyone know how to turn it into a working VBA function that can be called from Excel cell?

like image 233
user780069 Avatar asked Oct 26 '13 00:10

user780069


People also ask

How do I check for duplicates in Excel VBA?

Find and Highlight Duplicates in a Column Using Excel VBA ❶ First of all, press ALT + F11 to open the VBA editor. ❷ Then go to Insert >> Module. ❸ Copy the following VBA code. ❹ Paste and Save the code in the VBA editor.

How do I reference a function in Excel VBA?

Steps to follow: In Excel, hit Alt + F11 if on Windows, Fn + Option + F11 if on a Mac. Insert a new module. From the menu: Insert -> Module (Don't skip this!). Then use it in any cell like you would any other function: =findArea(B12,C12) .

Which function in VBA allows us to remove duplicates?

In this case, we have headers, so select “xlYes.” Run this code. It will VBA remove duplicates from the selected region. This is an explicit way of referring to the range of cells.


Video Answer


2 Answers

Here's the answer

Steps to follow:

  1. Open the Visual Basic Editor. In Excel, hit Alt+F11 if on Windows, Fn+Option+F11 if on a Mac.

  2. Insert a new module. From the menu: Insert -> Module (Don't skip this!).

  3. Create a Public function. Example:

    Public Function findArea(ByVal width as Double, _                          ByVal height as Double) As Double     ' Return the area     findArea = width * height End Function 
  4. Then use it in any cell like you would any other function: =findArea(B12,C12).

like image 173
FloatingRock Avatar answered Sep 28 '22 00:09

FloatingRock


The issue you have encountered is that UDFs cannot modify the Excel environment, they can only return a value to the calling cell.

There are several alternatives

  1. For the sample given you don't actually need VBA. This formula will work
    ='C:\Users\UserName\Desktop\[TestSample.xlsx]Sheet1'!$B$2

  2. Use a rather messy work around: See this answer

  3. You can use ExecuteExcel4Macro or OLEDB

like image 44
chris neilsen Avatar answered Sep 28 '22 02:09

chris neilsen