Trying to load range of strings/Numbers into variant array and things arent going well can anyone assist?

Tcm43

New Member
Joined
Jun 28, 2013
Messages
14
Code:
Function LastRowInOneColumn(ByRef RngOfData As Integer)
'Find the last used row in a Column: column A in this example
    Dim LastRow As Long
    With ActiveSheet
        LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
    End With
    RngOfData = LastRow
    MsgBox LastRow
End Function

Function Array_Unique(Arr As Variant) As Variant
  Dim tempArray As Variant
  Dim i As Long
  ' start the temp array with one element and
  ' populate with first value
  ReDim tempArray(0)
  tempArray(0) = Arr(LBound(Arr))
 
  For i = LBound(Arr) To UBound(Arr)
    If Not IsInArray(tempArray, Arr(i)) And Arr(i) <> "" Then  ' not in destination array
      ReDim Preserve tempArray(UBound(tempArray) + 1)
      tempArray(UBound(tempArray)) = Arr(i)
    End If
  Next i
  Array_Unique = tempArray
End Function

Function IsInArray(Arr As Variant, valueToFind As Variant) As Boolean
  Dim i As Long
  For i = LBound(Arr) To UBound(Arr)
    If StrComp(Arr(i), valueToFind) = 0 Then
      IsInArray = True
      Exit For
    End If
  Next i
End Function





Sub Testingthis()


Dim arr As Variant
Dim arrOriginal As Variant
Dim Rng As Range
'Dim arrOriginal() As Variant
Dim arrTemp() As String
Dim strPrintMsg As String    'For debugging
Dim i As Long, lCounter As Long
Dim FunctionDerivedRng As Integer
LastRowInOneColumn FunctionDerivedRng
Set Rng = Range(Cells(1, 1), Cells(FunctionDerivedRng, 1))
arrOriginal = Rng
  
 arr = Array("January", "May", "", "April", "May")
  ArrTest = Array("January", "May", "", "April", "May")
  ArrTest = Array_Unique(arr)
  Stop
  arrOriginal = Array_Unique(arrOriginal)
 ' Arr = CleanUpArray(Arr)
  Debug.Print "Blank"




End Sub

My Test case works where I manually enter string or numbers etc into the array but when I try to feed a range into the array thinks explode and I cant seem to make any progress :(
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Actually nevermind.... New problem.... Now my attempt at removing the blank values from the array has proven to fail. :( Sigh...
 
Upvote 0
In your test case you are passing a one dimensional array to the function, an array from a range is two dimensional.

How are you passing a range to the function?
 
Upvote 0
In your test case you are passing a one dimensional array to the function, an array from a range is two dimensional.

How are you passing a range to the function?

I think I got that part working with altering my code to look as:
Code:
Sub Testingthis()


Dim arr As Variant
Dim arrCampaign As Variant
Dim arrCampaignTemp() As String
Dim arrCampaignTemp1() As Variant
Dim arrUCM As Variant
Dim arrUCMTemp() As String
Dim arrUCMTemp1() As Variant
Dim CombinedArrays() As Variant
Dim CampaignRng As Range
Dim UCMRng As Range
Dim strPrintMsg As String    'For debugging
Dim i As Long, lCounter As Long


Dim x As Integer, y As Integer, z As Integer


Dim UCMFeedFieldsRng As Integer
Dim CampaignFieldsRng As Integer


Sheets("Campaign Fields").Select
LastRowInCampaignFields CampaignFieldsRng
Set CampaignRng = Range(Cells(2, 8), Cells(CampaignFieldsRng, 8))
arrCampaign = CampaignRng
ReDim arrCampaignTemp(LBound(arrCampaign) - 1 To UBound(arrCampaign) - 1)
For i = LBound(arrCampaign) To UBound(arrCampaign)
    arrCampaignTemp(i - 1) = CStr(arrCampaign(i, 1))
Next i
  
arrCampaignTemp1 = Array_Unique(arrCampaignTemp)
  
Sheets("UCM CRM Feed Fields").Select
LastRowInUCMFeedFields UCMFeedFieldsRng
Set UCMRng = Range(Cells(2, 1), Cells(UCMFeedFieldsRng, 1))
arrUCM = UCMRng
ReDim arrUCMTemp(LBound(arrUCM) - 1 To UBound(arrUCM) - 1)
For i = LBound(arrUCM) To UBound(arrUCM)
    arrUCMTemp(i - 1) = CStr(arrUCM(i, 1))
Next i
  arrUCMTemp1 = Array_Unique(arrUCMTemp)
  
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,094
Messages
6,053,506
Members
444,667
Latest member
KWR21

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