Resizing array dynamically

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,
I have two arrays: Arr1 and Arr2 which is a copy from Arr1 having one extra column, so in my code I ReDim the Arr2 using ReDim Arr2(1 To AD1, 1 To 5).
Is it possible redim the Arr2 dynamically instead of fixing to 5 columns?

Code:

Sub Vlookup()

Dim Arr1() As Variant

Dim AD1 As Long

Dim AD2 As Long

Dim Arr2() As Variant

Dim i As Long

Dim Name As String

Dim Department As String

Dim Lookup_val As String

Dim Salary As Long


Arr1 = Range("A2", Range("A1").End(xlDown).End(xlToRight))


AD1 = UBound(Arr1, 1)

AD2 = UBound(Arr1, 2)


'Populating A2 with data from A1 and concatenating the data

ReDim Arr2(1 To AD1, 1 To 5)


For i = 1 To AD1

Arr2(i, 1) = Arr1(i, 2) & "_" & Arr1(i, 3)

Arr2(i, 2) = Arr1(i, 1)

Arr2(i, 3) = Arr1(i, 2)

Arr2(i, 4) = Arr1(i, 3)

Arr2(i, 5) = Arr1(i, 4)

Next i


Name = InputBox("Enter the name of the Employee")

Department = InputBox("Enter the department of the Employee")

Lookup_val = Name & "_" & Department

Salary = Application.WorksheetFunction.Vlookup(Lookup_val, Arr2, 5, False)

MsgBox ("The salary of the emmployee is " & Salary)


End Sub


Table:

IDNameDepartmentSalary
12201​
MaheshIT
25000​
12202​
RekhaHR
28000​
12203​
SashiMarketing
18000​
12204​
RishabhSales
21000​
12205​
AnkushPR
18000​
12206​
SashiIT
32000​
12207​
RishiHR
21000​
12208​
KrishnaMarketing
18000​
12209​
ShikhaSales
18000​
12210​
RiniSupport
32000​
12211​
VrishIT
21000​
12212​
DiaHR
18000​
12213​
DhruvMarketing
22000​
12214​
VishuSales
16000​
12215​
SachinPR
23000​
12216​
AshwinIT
21000​
12217​
AyushHR
22000​
12218​
BrijeshMarketing
18000​
12219​
DhruvSales
32000​
12220​
AbhishekProduction
21000​
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You can get the last used column and use that instead. I think I've understood.

Check the additional and amended code below.

VBA Code:
Sub Vlookup()

Dim Arr1() As Variant

Dim AD1 As Long

Dim AD2 As Long

Dim Arr2() As Variant

Dim i As Long

Dim Name As String

Dim Department As String

Dim Lookup_val As String

Dim Salary As Long

Dim lc As Long 'last column


    Arr1 = Range("A2", Range("A1").End(xlDown).End(xlToRight))
   
   
    AD1 = UBound(Arr1, 1)
   
    AD2 = UBound(Arr1, 2)
   
    'Get last used column on the first row. Change where necessary
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
   
    'Populating A2 with data from A1 and concatenating the data
   
    ReDim Arr2(1 To AD1, 1 To lc)
   
   
    For i = 1 To AD1
   
    Arr2(i, 1) = Arr1(i, 2) & "_" & Arr1(i, 3)
   
    Arr2(i, 2) = Arr1(i, 1)
   
    Arr2(i, 3) = Arr1(i, 2)
   
    Arr2(i, 4) = Arr1(i, 3)
   
    Arr2(i, 5) = Arr1(i, 4)
   
    Next i
   
   
    Name = InputBox("Enter the name of the Employee")
   
    Department = InputBox("Enter the department of the Employee")
   
    Lookup_val = Name & "_" & Department
   
    Salary = Application.WorksheetFunction.Vlookup(Lookup_val, Arr2, 5, False)
   
    MsgBox ("The salary of the emmployee is " & Salary)


End Sub
 
Upvote 0
How about
VBA Code:
ReDim arr2(1 To AD1, 1 To AD2 + 1)
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,916
Members
449,093
Latest member
dbomb1414

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