0

I'm trying to create a macro that can collect data from an excel spreadsheet in the local active workbook and then create header file which I would later incorporate into my project. But for the life of me I must be missing something so DUMB that I can't create a working function that returns a string (which would construct a C++ structure) to the calling function. I've simplified the example code to is absolute bare minimum to isolate the problem but I still cannot figure out what I'm doing wrong. I'm not an expert at VBA but I know how to create code and I can't narrow down what VBA is unhappy about. I keep getting "compile error, syntax error." Please copy the following code into your module and see if compiles properly for you. If you know where I went wrong please let me know. Much Appreciated!!!

Sub CREATE_FACTORY_SETTING_HEADER()
    Dim FS, TSsource
    Set FS = CreateObject("Scripting.FileSystemObject")

    Dim TSout
    Set TSout = FS.Createtextfile("HeaderFile.h", True)

    Dim fileHeading As String
    fileHeading = "File Heading for Header file"

    Dim fileBody As String
    fileBody = "Some initial file body lines"

    fileBody = fileBody & createStructBody

    TSout.Write fileHeading & fileBody
    TSout.Close    
End Sub


Public Function createStructBody() As String
    Dim structBody As String
    structBody = "Hey I'm a struct body, but I can't be returned for some     reason"
    Return structBody
End Function
1
  • You need to specify WHAT and WHERE your errors are. Which lines of code cause what errors? Commented Jun 24, 2015 at 17:25

1 Answer 1

5

Both VBA and VBScript use 'assignment to function' (instead of 'return' or 'result of last statement') to return results from functions. So

Public Function createStructBody() As String
    createStructBody = "Hey I'm a string and can be returned."
End Function
Sign up to request clarification or add additional context in comments.

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.