Problem with ubound array after delete elements

Nervatos

New Member
Joined
Dec 19, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hello there

I have tried to make an array and then I want to delete some of the elements. But the problem is that, UBound i still the same after deleting. How can I fix that?

Cause I need it to be pasted to A1 and then to D1 (cause I deleted two letters, If I don't delete some letters then it should be to F1).

So it could look like:
A B C D E F

And then I want to put some vlookup under every letter just for 10 down.

B C E F
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup
Vlookup Vlookup Vlookup Vlookup

The vlookup should I made like formula VBA.

Some there can see what I'm doing wrong?
Hope you can understand my bad English. Have a great night!

VBA Code:
Function MyArray()
    Dim arr, arr2
    arr = Array("A", "B", "C", "D", "E", "F")
    Dim c As Long: c = 0
    Dim j As Long, jj As Long: jj = 1
    ReDim arr2(1 To UBound(arr), 1 To UBound(arr, 2))
    
    For j = 1 To UBound(arr)
        If arr(j, 1) <> "A" And arr(j, 1) <> "D" Then
            arr2(jj, 1) = arr(j, 1)
            jj = jj + 1
            c = c + 1
        End If
    Next j
    
    ReDim Preserve arr2(c)
    MyArray = arr2
End Function
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Sorry, it's not clear why you need to create an array in VBA.

It looks like you want to use VBA to write VLOOKUP formulae in B1:10, C1:C10, E1:E10 and F1:F10?

Is that correct? If so, what do your VLOOKUP formulas look like?
 
Upvote 0
Hello Stephen

I forgot the another code.

VBA Code:
Sub test()
    Dim i, j As Long
    Dim v As Variant
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Ark45")
    Dim Target As Range: Set Target = Range("E1")
    
    With ws
        For i = LBound(MyArray(), 1) To UBound(MyArray(), 1)
            Cells(1, i + 4).Value = MyArray()(i, 1)
            For j = 1 To 10
                Cells(j + 1, i + 4).Value = j
            Next j
        Next i
    End With
End Sub

I want to create an VBA array cause of the list is dynamic and I use it on many other sheets. So for me, it's more perfekt to just be in an function.

I got this result then I run this code:

1653722363067.png


So it's taking coloumn I and J with, cause of UBound(MyArray(), 1) result 6 and not 4.
I want in the cells down like this: =IfError(Vlookup(A2,B!A:C,3,FALSE),"") =IfError(Vlookup(A3,B!A:C,3,FALSE),""). The name of the sheet is in the top. So B is also the named of the Sheet where it have to search.

Hope you understand me now. I'm sorry about my English.
 
Upvote 0
Sorry, I still don't understand why you are using VBA code to write numbers to the worksheet.

Is this what you are trying to do with the VLOOKUPs?

ABCDE
1BCEF
2xxx318711
3yyy115 22
4zzz212633
Sheet1
Cell Formulas
RangeFormula
B2:E4B2=IFERROR(VLOOKUP($A2,INDIRECT("'"&B$1&"'!A:C"),3,),"")

ABC
1
2
3
4yyy1
5
6
7
8zzz2
9
10
11xxx3
B

ABC
1
2zzz12
3
4yyy15
5xxx18
C

Book4
ABC
1
2zzz6
3
4
5xxx7
E

ABC
1
2xxx11
3
4yyy22
5
6
7
8zzz33
F
 
Upvote 0
Yes, the VLOOKUP is perfect - thanks!

I do it in VBA, as I have another code that makes sheets by array (same array that should make an overview). Then I need an overview of all those sheets together. So it finds data from the different sheets. It should run automatically and dynamically, so that if more users come on - then they come automatically.

A B C D E F G is just instead of names, but there are some names that should not be part of this count. That's why I try to do it that way. It's probably not quite right and can probably also be made much better. But I'm not the best at VBA, unfortunately.
 
Upvote 0
You could generate these formulae using VBA, perhaps something like this:

VBA Code:
arr = Array("B", "C", "E", "F")
c = UBound(arr) - LBound(arr) + 1

Range("B1").Resize(, c).Value = arr
Range("B2").Resize(Range("A" & Rows.Count).End(xlUp).Row - 1, c).Formula = "=IFERROR(VLOOKUP($A2,INDIRECT(""'""&B$1&""'!A:C""),3,),"""")"
 
Upvote 0
Solution
Hello Stephen

That works great. But If I have an array at A B C D E F, and want to remove A and D? Cause I get the array list from cell value from another sheet? How would be the best way to do that be my function?
 
Upvote 0
That works great. But If I have an array at A B C D E F, and want to remove A and D? Cause I get the array list from cell value from another sheet? How would be the best way to do that be my function?
You could do something like this, similar to your code in Post #1:

VBA Code:
Sub MyArray()
    
    Dim arr As Variant, arr2 As Variant
    Dim i As Long, c As Long
    
    arr = Array("A", "B", "C", "D", "E", "F")
    arr2 = arr
      
    c = LBound(arr)
    For i = LBound(arr) To UBound(arr)
        'Your test here to eliminate some elements of the array
        If arr(i) <> "A" And arr(i) <> "D" Then
            arr2(c) = arr(i)
            c = c + 1
        End If
    Next i
    
    Range("B1").Resize(, c - LBound(arr)).Value = arr2

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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