Expand the range value and assign it to a new array

anilsharaf

New Member
Joined
Apr 8, 2014
Messages
43
Office Version
  1. 2007
Platform
  1. Windows
I have this data
Sitting Board_V1 Principle and Module InProcess.xlsm
ABCDEFGH
1Sn_Final_according_to_Check_ListRN_fromRN_ToMediumTypeSort3_Center_Number_School_Name_Hss_2024Total_Students
2122432037862243203830HindiRegular321042Barpali45
3222432038312243203899HindiRegular321044Boys69
4322432039462243203981HindiRegular321070Madwa36
5422432039822243204046HindiRegular321224Pithampur65
6522432041152243204139HindiRegular322292DVM25
7622432800212243280021HindiDiv Improve321044Boys01
8722432990022243299002HindiDiv Improve321070Madwa01
9822432039002243203945EnglishRegular321044Atmanand46
10922432040472243204114EnglishRegular322071Lions68
111022432800222243280022EnglishDiv Improve322071Lions01
12Tot357
AllotedRollNos


And Output data wanted first putting it in an array of NewTable (1 to 357, 1 to 7) and then to sheet named RollNos Like this
Sitting Board_V1 Principle and Module InProcess.xlsm
ABCDEFG
1Sort 3 SnRoll NoMediumType RegOrDivImproveCenter NoSchool NameNominal Sheet No
213786HindiRegular321042Barpali1
323787HindiRegular321043Barpali1
433788HindiRegular321044Barpali1
543789HindiRegular321045Barpali1
653790HindiRegular321046Barpali1
763791HindiRegular321047Barpali2
873792HindiRegular321048Barpali2
983793HindiRegular321049Barpali2
1093794HindiRegular321050Barpali2
11103795HindiRegular321051Barpali2
12113796HindiRegular321052Barpali3
13123797HindiRegular321053Barpali3
14133798HindiRegular321054Barpali3
15143799HindiRegular321055Barpali3
16153800HindiRegular321056Barpali3
17163801HindiRegular321057Barpali4
18173802HindiRegular321057Barpali4
RollNos


Nominal Sheet No increses 1 after 5 Roll Nos. ( 1 Nominal Roll contains 5 Roll Nos)
VBA Code:
Option Base 1
Sub CreateRollNumberArray_notReady()
    Dim RollNumbers()
    Dim Medium() As Variant
    Dim i, ir As Long
    Dim RowCounter As Long
    
    ' Define the range for Roll Numbers and Medium
    'Dim RollNumberRange() As Range
    Dim MediumRange As Range
   RollNumberRange = Worksheets("AllotedRollNos").Range("B2:C2") ' Adjust the range as needed
    Set MediumRange = Worksheets("AllotedRollNos").Range("D2") ' Medium is in D2
    
    ' Initialize the array size based on the number of Roll Numbers
    'ReDim RollNumbers(1 To RollNumberRange.Columns.Count)
    'ReDim Medium(1 To RollNumberRange.Columns.Count)
    
    ' Expand RollNumbers
    q = LBound(RollNumberRange, 2) 'column
    q2 = UBound(RollNumberRange, 2)
    
    
    '8888888888888888888
     startNum = RollNumberRange(1, 1)
        'Debug.Print startNum
        endNum = RollNumberRange(1, 1 + 1)
        'Debug.Print endNum
        
        Tot = (endNum - startNum) + 1
        
        'ir = 1
    For i = startNum To endNum
    ir = ir + 1
    '888888888888888888888
    
        ReDim Preserve RollNumbers(ir)
        RollNumbers(ir) = i
        ReDim Preserve Medium(ir)
        Medium(ir) = MediumRange.Value
        
    Next i
    
    ' Now you have the Roll Numbers and Medium in separate arrays
    ' You can access them individually or use them as needed
    
    ' Example: Print Roll Numbers and Medium
    For i = 1 To UBound(RollNumbers)
        Debug.Print "Roll Number: " & RollNumbers(i) & ", Medium: " & Medium(i)
    Next i
End Sub
There should be a better code to do this. So please do not base the code on my code. It is based on superficial knowledge. I wanted to do it myself but I failed. Thanks in Advance.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi there,

Can you try this code:

VBA Code:
Sub GetList()

Dim wsO As Worksheet
Dim wsD As Worksheet

'Original raw Data
Set wsO = Sheets("AllotedRollNos")
'Destination data
Set wsD = Sheets("RollNos")

wsD.Activate
'clear Destination data
wsD.Range("A2:G2000").Clear

Dim x As Long
Dim y As Long
Dim z As Long

'row count from Original data
x = 2
'row count for Destination data
y = 2
'sequential row for total_students
z = 2

Dim a As Long
'running RN No for Destination data
a = 0

Dim data_lastrow As Long
'defined last row in Original data
data_lastrow = wsO.Cells(Rows.Count, 1).End(xlUp).Row

Do Until x = data_lastrow + 1
        a = Right(wsO.Range("B" & x).Value, 4)
        'Decide how many row to repeat
        Do Until y = z + wsO.Range("H" & x).Value
            With wsD
                .Range("A" & y).Value = y - 1
                .Range("B" & y).Value = a
                .Range("C" & y).Value = wsO.Range("D" & x).Value
                .Range("D" & y).Value = wsO.Range("E" & x).Value
                .Range("E" & y).Value = wsO.Range("F" & x).Value
                .Range("F" & y).Value = wsO.Range("G" & x).Value

            End With
            y = y + 1
            a = a + 1
        Loop
    z = z + wsO.Range("H" & x).Value
    x = x + 1
Loop

wsD.Range("G2").Value = "=IF(ROW()=2,""1"",IF(MOD(ROW()-1,5)=1,R[-1]C+1,R[-1]C))"

Dim data_lastrow2 As Long
'defined last row in Destination data
data_lastrow2 = wsD.Cells(Rows.Count, 1).End(xlUp).Row

wsD.Range("G2:G" & data_lastrow2).Select
Selection.FillDown

wsD.Range("G2:G" & data_lastrow2).Value = wsD.Range("G2:G" & data_lastrow2).Value

wsO.Activate
    
x = 0
y = 0
z = 0
a = 0
data_lastrow = 0
data_lastrow2 = 0
Set wsO = Nothing
Set wsD = Nothing

MsgBox "Data RollNos generated"

End Sub

Let me know if you get any error.
Thanks!
 
Upvote 1
Solution
Hi there,

Can you try this code:

VBA Code:
Sub GetList()

Dim wsO As Worksheet
Dim wsD As Worksheet

'Original raw Data
Set wsO = Sheets("AllotedRollNos")
'Destination data
Set wsD = Sheets("RollNos")

wsD.Activate
'clear Destination data
wsD.Range("A2:G2000").Clear

Dim x As Long
Dim y As Long
Dim z As Long

'row count from Original data
x = 2
'row count for Destination data
y = 2
'sequential row for total_students
z = 2

Dim a As Long
'running RN No for Destination data
a = 0

Dim data_lastrow As Long
'defined last row in Original data
data_lastrow = wsO.Cells(Rows.Count, 1).End(xlUp).Row

Do Until x = data_lastrow + 1
        a = Right(wsO.Range("B" & x).Value, 4)
        'Decide how many row to repeat
        Do Until y = z + wsO.Range("H" & x).Value
            With wsD
                .Range("A" & y).Value = y - 1
                .Range("B" & y).Value = a
                .Range("C" & y).Value = wsO.Range("D" & x).Value
                .Range("D" & y).Value = wsO.Range("E" & x).Value
                .Range("E" & y).Value = wsO.Range("F" & x).Value
                .Range("F" & y).Value = wsO.Range("G" & x).Value

            End With
            y = y + 1
            a = a + 1
        Loop
    z = z + wsO.Range("H" & x).Value
    x = x + 1
Loop

wsD.Range("G2").Value = "=IF(ROW()=2,""1"",IF(MOD(ROW()-1,5)=1,R[-1]C+1,R[-1]C))"

Dim data_lastrow2 As Long
'defined last row in Destination data
data_lastrow2 = wsD.Cells(Rows.Count, 1).End(xlUp).Row

wsD.Range("G2:G" & data_lastrow2).Select
Selection.FillDown

wsD.Range("G2:G" & data_lastrow2).Value = wsD.Range("G2:G" & data_lastrow2).Value

wsO.Activate
   
x = 0
y = 0
z = 0
a = 0
data_lastrow = 0
data_lastrow2 = 0
Set wsO = Nothing
Set wsD = Nothing

MsgBox "Data RollNos generated"

End Sub

Let me know if you get any error.
Thanks!
No Error. Code worked Fine! Thank you so much
 
Upvote 1

Forum statistics

Threads
1,224,822
Messages
6,181,165
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