Insert different data type into a function using array.

Squareroot

New Member
Joined
Nov 9, 2022
Messages
15
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I am trying to generate headers for a table. I decided to create a function just to generate header for my table. and to decide which row the header will go to my idea is to put the first element of my array(array that will be pass into the function)as the row number. following is my code.

Function create(header)
For x = 1 To 5
Sheets(1).Cells(header(0), x).Value = header(x)
Sheets(1).Cells(header(0), x).Interior.ColorIndex = 43

Next x
End Function
XXXXXXXXXXXXXXXXXXXXXXXXX main function XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Dim header As Variant
....
...
create (header = Array(1, "apple", "banana.", "carrot", "durian", "eggplant" ))
create (header = Array(2, "a", "b", "c", "d", "e" ))

the above code is not working as it will give me mismatch data type. Does anyone know how I can pass both int and string using the same array? or VBA doesn't allow user to do that.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi Squareroot,

maybe like this:

VBA Code:
Function fncCreateHeader(varHeader) As Boolean
'https://www.mrexcel.com/board/threads/insert-different-data-type-into-a-function-using-array.1222176/
Dim x As Long
On Error GoTo err_here
For x = LBound(varHeader) + 1 To UBound(varHeader)
  With Sheets(1).Cells(varHeader(LBound(varHeader)), x)
    .Value = varHeader(x)
    .Interior.ColorIndex = 43
  End With
Next x
fncCreateHeader = True
Exit Function

err_here:
End Function

Sub MrE1222176()
Dim varHeader As Variant
Dim blnRet As Boolean
'....
'...
varHeader = Array(1, "apple", "banana", "carrot", "durian", "eggplant")
If fncCreateHeader(varHeader) = False Then
  MsgBox "Error creating varHeader", vbInformation, "Ending here"
  Exit Sub
End If
varHeader = varHeader = Array(2, "a", "b", "c", "d", "e")
If fncCreateHeader(varHeader) = False Then
  MsgBox "Error creating varHeader", vbInformation, "Ending here"
  Exit Sub
End If
End Sub

Ciao,
Holger
 
Upvote 0
Solution
Hi Squareroot,

maybe like this:

VBA Code:
Function fncCreateHeader(varHeader) As Boolean
'https://www.mrexcel.com/board/threads/insert-different-data-type-into-a-function-using-array.1222176/
Dim x As Long
On Error GoTo err_here
For x = LBound(varHeader) + 1 To UBound(varHeader)
  With Sheets(1).Cells(varHeader(LBound(varHeader)), x)
    .Value = varHeader(x)
    .Interior.ColorIndex = 43
  End With
Next x
fncCreateHeader = True
Exit Function

err_here:
End Function

Sub MrE1222176()
Dim varHeader As Variant
Dim blnRet As Boolean
'....
'...
varHeader = Array(1, "apple", "banana", "carrot", "durian", "eggplant")
If fncCreateHeader(varHeader) = False Then
  MsgBox "Error creating varHeader", vbInformation, "Ending here"
  Exit Sub
End If
varHeader = varHeader = Array(2, "a", "b", "c", "d", "e")
If fncCreateHeader(varHeader) = False Then
  MsgBox "Error creating varHeader", vbInformation, "Ending here"
  Exit Sub
End If
End Sub

Ciao,
Holger
it worked thank you
 
Upvote 0
Thanks Alex, I really missed that when alteringn code changing names - glad you pointed it out.

Holger
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top