![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Greg's use of functions is part of an effort to create modular code which over the long term enables you to reuse old code and save a lot of time. Many different projects need to get data from Excel. Greg's function enables him to copy and paste that chunk of code into many different projects and saves him lots of rework. Say you have a large block of code and have 5 instances where you need to go out to Excel to get some tabular data. All 5 instances are essentially the process but the details will vary (what workbook, what sheet, is there headings at the top). Instead of repeating all the necessary coding in 5 places (which then has to be amended in 5 places if there is a problem), a function enables you to write it once and pass in those different input variables to get different datasets from the same code. Subs and functions are similar but functions have the ability to return a variable (in this case a variant array). A function (or a sub with inputs) can't be run directly - they need to be called by other subroutines or functions.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#2
|
|||
|
|||
|
Quote:
Code:
Private Function fcnExcelDataToArray(strWorkbook As String, _
Optional strRange As String = "Sheet1", _
Optional bIsSheet As Boolean = True, _
Optional bHeaderRow As Boolean = True) As Variant
Not by me or other user manually in the Excel cell, right?? It took me a while to grasp that... Going back to my code... What I want to do now with my code, preferably in the end of it, before this line: Code:
' Display the results.
' txtBooks.Text = txt
Can anybody help me with that? This is my current code: Code:
Sub GetRows_returns_a_variant_array()
Dim connection As ADODB.connection
Dim recSet As ADODB.Recordset
Dim exclApp As Excel.Application 'This code is written and lunched from Word VBA Editor
Dim exclWorkbk As Excel.Workbook
Dim mySheet As Excel.Worksheet
Dim wordDoc As Word.Document
Dim strPESELfromWord As String
Dim strQuery0 As String
Dim strQuery1 As String
Dim strQuery2 As String
Dim strSexDigit As String
Dim values As Variant 'Should there be values() ??
Dim txt As String
Dim intRemainder As Integer
Dim r As Integer
Dim c As Integer
Set wordDoc = Word.ActiveDocument
Debug.Print wordDoc.Name
Set connection = New ADODB.connection
Debug.Print "ADODB.connection.Version = " & connection.Version
strPESELfromWord = Trim(Selection.Text)
Debug.Print "Wybrano PESEL " & strPESELfromWord & "."
strSexDigit = Mid(strPESELfromWord, 10, 1) 'Extract 10th digit from PESEL number
Debug.Print strSexDigit
intRemainder = strSexDigit Mod 2 'If 1 is remaider it means it's a male, and if there's no remainder, than it's a female.
Debug.Print intRemainder
' Open the database connection.
connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\custdb.xlsm;" & _
"Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";" 'now it works
' Select the data.
strQuery0 = "SELECT * FROM Books ORDER BY Title, Year"
strQuery1 = "SELECT * FROM [data$]" '[data$] is the table name; in this case it's the sheet name;
strQuery2 = "SELECT * FROM [data$] WHERE pesel <> ''" 'col B = pesel; col C = data_urzodzenia; col D = imie_nazwisko
' Get the records.
Set recSet = connection.Execute(strQuery2, , adCmdText)
' Load the values into a variant array.
values = recSet.GetRows 'GetRows returns a variant array holding the Recordset's values.
' Close the recordset and connection.
recSet.Close
connection.Close
' Use the array to build a string containing the results.
For r = LBound(values, 2) To UBound(values, 2)
For c = LBound(values, 1) To UBound(values, 1) 'max "c" = 78
'txt = txt & values(c, r) & ", "
txt = txt & values(c, r) & "| "
Next c
'txt = Left$(txt, Len(txt) - 1) & vbCrLf 'Left Returns a Variant (String) containing a specified number of characters from the left side of a string.
txt = Left(txt, Len(txt) - 1) & vbCrLf 'The trailing $ is a type declaration character for the String data type in VBA.
Next r 'The result returned from Left$() is a string, whereas Left() returns a Variant.
'You must use Left(), not Left$() if there is any chance of Null values, since the Variant can be Null but the String cannot.
' Display the results.
' txtBooks.Text = txt
End Sub
|
|
| Tags |
| word fields, word vba, word vba macro |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Reset "Button" to clear Word user-filled form, from all filled details.
|
Rafi | Word VBA | 20 | 01-20-2023 02:16 PM |
VBA to Populate text content controls in word from excel
|
lmoir87 | Word VBA | 15 | 05-27-2022 04:22 PM |
| Auto populate form (data from excel) in Word based on drop down list selection (data from excel) | wvlls | Word VBA | 1 | 03-22-2019 02:29 PM |
| Form with content controls - expands but at the bottom of the form | louiseword | Word | 3 | 05-27-2016 12:47 AM |
Date auto-populates based on checkbox
|
mcarter9000 | Word VBA | 5 | 12-23-2010 12:39 PM |