How to load the outcome of a code into a string or array using vba by looping through multiple Worksheets

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello guys,
I have tried to narrow down what I want to achieve to this level and I think with a little push from the seniors on this great platform, my challenge will be handled.



Code:
Sub GetMissingNum ()
    Dim e&, lr&, sht As Worksheet 
        For Each sht In Worksheets 
             Select Case sht.Name 
                 Case "Data 1", "Data 2", "Data 3", "Report 1", "Report 2"
                  lr = sht.Cells (Rows.Count, "A").End (xlUp).Row
                  If lr > 3 then
                  e  = 0
                  With sht.Range("A4:A" & lr)
                       Do 
                           e = e + 1
                       Loop Until IsError(Application.Match(e, .Cells, 0))
                       ' I want to load e into a string or array like:
                       ' ray = ("..., ..., ....") or 
                       ' ray = Array(..., ...., .....)
                  End With 
                  End If
            End Select 
         Next sht 
End Sub

I want someone to help me out with how to load each of the e into the string or array as explained above?

Thanks in advance.
 
Just take a glance to the VBA help of Currency data type for example …​
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hello guys,
I have tried to narrow down what I want to achieve to this level and I think with a little push from the seniors on this great platform, my challenge will be handled.



Code:
Sub GetMissingNum ()
    Dim e&, lr&, sht As Worksheet
        For Each sht In Worksheets
             Select Case sht.Name
                 Case "Data 1", "Data 2", "Data 3", "Report 1", "Report 2"
                  lr = sht.Cells (Rows.Count, "A").End (xlUp).Row
                  If lr > 3 then
                  e  = 0
                  With sht.Range("A4:A" & lr)
                       Do
                           e = e + 1
                       Loop Until IsError(Application.Match(e, .Cells, 0))
                       ' I want to load e into a string or array like:
                       ' ray = ("..., ..., ....") or
                       ' ray = Array(..., ...., .....)
                  End With
                  End If
            End Select
         Next sht
End Sub

I want someone to help me out with how to load each of the e into the string or array as explained above?

Thanks in advance.
To load 'e' into an array, how about:
VBA Code:
eArray(e) = e

You would have to dim the eArray, That code may or may not yield the results you desire.
 
Upvote 0
As your link is not for VBA ! Do not confuse VB & VBA …​
Ahhhh. Good catch @Marc L. Thank you!

This list better?

VBA Code:
'Identifier type character  Data type                   Example
'           %               Integer                     Dim L%
'           &               Long                        Dim M&
'           @               Currency                    Dim cProfit@
'           !               Single                      Dim Q!
'           #               Double                      Dim X#
'           $               String                      Dim V$ = "Some String"
'           ^               LongLong (64 bit versions)  Dim llDiscount^
 
Upvote 0
To load 'e' into an array, how about:
VBA Code:
eArray(e) = e

You would have to dim the eArray, That code may or may not yield the results you desire.

How do I Dim the eArray?
 
Upvote 0
Code:
Sub GetMissingNum ()
    Dim e&, lr&, sht As Worksheet, ray 
       ray = ""
        For Each sht In Worksheets 
             Select Case sht.Name 
                 Case "Data 1", "Data 2", "Data 3", "Report 1", "Report 2"
                  lr = sht.Cells (Rows.Count, "A").End (xlUp).Row
                  If lr > 3 then
                  e  = 0
                  With sht.Range("A4:A" & lr)
                       Do 
                           e = e + 1
                       Loop Until IsError(Application.Match(e, .Cells, 0))
                       ray = ray & e & ","
                      'ray = ray & e & " " 
                  End With 
                  End If
            End Select 
         Next sht 
         MsgBox ray
End Sub

Upon several tests, I have been able to get closer than I imagined.

Now, my MsgBox alert is giving me:
2,6,1,8,7,

Which are the exact missing numbers of the respective sheets are indicated in my sample code above.
Which means that the variable ray = 2,6,1,8,7,

How do I make it:

ray = ("2, 6, 1, 8, 7")
Or
ray = Array (2, 6, 1, 8, 7)
???????
 
Upvote 0
@kelly mort Try the following code to test the storage of values into an array:

VBA Code:
Sub GetMissingNum()
'
    Dim e&, lr&, eArraySlotNumber&, UsedCellsIneArray&, UsedeArraySlotNumber&, sht  As Worksheet
'
    Dim eArrayCellContents  As String
    Dim eArray(1 To lr)     As Variant
'
    For Each sht In Worksheets
        Select Case sht.Name
            Case "Data 1", "Data 2", "Data 3", "Report 1", "Report 2"
                lr = sht.Cells(Rows.Count, "A").End(xlUp).Row
'
                ReDim eArray(1 To lr) As Variant
'
                If lr > 3 Then
                    e = 0
                    eArraySlotNumber = 0
'
                    With sht.Range("A4:A" & lr)
                        Do
                            e = e + 1
                        Loop Until IsError(Application.Match(e, .Cells, 0))
'
'                       I want to load e into a string or array like:
'                           ray = ("..., ..., ....")        OR
'                           ray = Array(..., ...., .....)
                        eArraySlotNumber = eArraySlotNumber + 1
                        eArray(eArraySlotNumber) = e
                    End With
                End If
        End Select
    Next sht
'
'
'   eArray check values section \/ \/ \/
'
    eArrayCellContents = ""
    UsedCellsIneArray = Application.Count(eArray) ' find # of used cells in an array
'
    If UsedCellsIneArray > 0 Then
        For UsedeArraySlotNumber = 1 To UsedCellsIneArray
            If UsedeArraySlotNumber < UsedCellsIneArray Then
                eArrayCellContents = eArrayCellContents & eArray(UsedeArraySlotNumber) & ", "
            Else
                eArrayCellContents = eArrayCellContents & eArray(UsedeArraySlotNumber)
            End If
        Next
    End If
'
    MsgBox "The " & UsedCellsIneArray & " values stored in 'eArray' are ... " & eArrayCellContents
End Sub

Please report back with what the messagebox says.
 
Upvote 0
@johnnyL
I get the error message: compiler error

Constant expression required
Code:
 Dim eArray(1 To lr)     As Variant
Then the lr is highlighted
 
Upvote 0
@johnnyL
I get the error message: compiler error

Constant expression required
Code:
Dim eArray(1 To lr)     As Variant
Then the lr is highlighted
Crap, I forgot about that line, Change that line with 'lr' to something like:
VBA Code:
Dim eArray(1 To 5)     As Variant

It is changed later on to 'lr'
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,407
Members
449,448
Latest member
Andrew Slatter

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