newbie777

New Member
Joined
Oct 17, 2018
Messages
24
Hi Guys!

Again i need your help! :)

I have filled a 2D array with data that i need to populate to a range. The issue is, one D is populated in a row and the other is to a column. Typically i need them to be populated to two adjacent column. The first D has sector names and the 2nd D has Values. I need the first sector name and then the first value beside it.

1st Sector 1st value
2nd Sector 2nd value

.
.
.
.



The code as below

Code:
Sub weight_upload()
Dim Sht As Worksheet: Set Sht = Sheets("Main")
Dim sht1 As Worksheet: Set sht1 = Sheets("SDC")
Dim sht2 As Worksheet: Set sht2 = Sheets("Uploads")
Dim sht3 As Worksheet: Set sht3 = Sheets("Sector Mapping")
Dim sht4 As Worksheet: Set sht4 = Sheets("BDS")
Dim sht5 As Worksheet: Set sht5 = Sheets("PSE")
Dim sht6 As Worksheet: Set sht6 = Sheets("All Sectors")
Dim sht7 As Worksheet: Set sht7 = Sheets("ASDC")
Dim LastR1 As Integer: LastR1 = sht1.Cells(Cells.Rows.Count, "M").End(xlUp).Row
Dim LastR2 As Integer:  LastR2 = sht2.Cells(Cells.Rows.Count, "A").End(xlUp).Row
Dim LastR3 As Integer:  LastR3 = sht3.Cells(Cells.Rows.Count, "A").End(xlUp).Row
Dim LastR4 As Integer:  LastR4 = sht4.Cells(Cells.Rows.Count, "A").End(xlUp).Row
Dim LastR5 As Integer:  LastR5 = sht5.Cells(Cells.Rows.Count, "A").End(xlUp).Row
Dim LastR6 As Integer:  LastR6 = sht6.Cells(Cells.Rows.Count, "A").End(xlUp).Row
Dim LastR7 As Integer:  LastR7 = sht7.Cells(Cells.Rows.Count, "A").End(xlUp).Row
Dim R, R1, R2 As Range
Dim RGI_Weights, RGC_Weights As Variant
Dim Headers1(), Headers2() As String
Dim L, I, K, M, N, O As Integer: N = sht3.Cells(Cells.Rows.Count, "F").End(xlUp).Row: M = sht1.Cells(Cells.Rows.Count, "A").End(xlUp).Row: K = 1: L = 1: M = 0
Dim TMC, Adj_weights, AWF, Sector_MC, Sector_Cap, Sectors() As Variant




' calculating unchanged variables


For Each R In sht3.Range("F2:F" & sht3.Cells(Cells.Rows.Count, "F").End(xlUp).Row)


ReDim Preserve Sectors(0 To N, 0 To N)


  
    Sectors(K, 0) = R.Value
   
    For Each R1 In sht1.Range("m2:m" & LastR1)
    Set R2 = sht3.Range("A2:A" & LastR3).Find(R1.Value, LookIn:=xlValues, Lookat:=xlWhole)
    If R2 Is Nothing Then
        MsgBox (R1.Value & " cannot be found in the Sector Mapping sheet" & vbNewLine & "Please add the stock to the Sector mapping sheet and Run the Macro again"): Exit Sub
        End If
    Select Case Sectors(K, 0)
        Case Is = R2.Offset(0, 1).Value
     
           Sectors(0, L) = Sectors(0, L) + (R1.Offset(0, 11).Value * R1.Offset(0, 12).Value * R1.Offset(0, 13).Value * R1.Offset(0, 15).Value)
    
    End Select




Next R1
K = K + 1
L = L + 1
Next R


'populating the array


sht1.Range("AR2:AS6").Resize(N, N).Value = Sectors
----------------------

Can you please help me?

Many thanks,
 
Last edited by a moderator:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi,
Youneed to have the same range set in worksheet as in the array(the same no of rows and columns) in order to return values from the array directly to the worksheet.
You should correct the following lines as follows and it shoud work:
Code:
ReDim Preserve Sectors(1 To N, 1 To N)
sht1.Range(sht1.Range("AR2"),sht1.Range("AR2").Offset(N-1,N-1)).value=Sectors

Regards,
Sebastian
 
Upvote 0
Just for your information.
Code:
Dim L, I, K, M, N, O As Integer
The way you have it here means that L, I, K, M and N are declared as variables, not as you probaly assume as Integers.
If they all need to be declared as Integers, it needs to be as follows
Code:
Dim L As Integer, I As Integer, K As Integer and so forth.
 
Last edited:
Upvote 0
Just for your information.
Code:
Dim L, I, K, M, N, O As Integer
The way you have it here means that L, I, K, M and N are declared as variables, not as you probaly assume as Integers.
If they all need to be declared as Integers, it needs to be as follows
Code:
Dim L As Integer, I As Integer, K As Integer and so forth.


So you mean they are declared as Variants? You sure?

Many thanks to you :)
 
Upvote 0
I changed the lines but i still get it as below:
Agriculture400232590280.59401596794937.152433987054672.25108172375291.64974754290163.08
Alternatives
Energy
Forest Products
Metal & Mining

Any other Ideas?

<colgroup><col><col span="5"></colgroup><tbody>
</tbody>
 
Upvote 0
Change also in three lines in sectors array 0 to 1 and let me know the result. By the way, yiu have to declare each variae separately as integer, long or whatever, because otherwise it's a variant instead.
 
Upvote 0
Sorry there are four places, two lines of each: change sectors(K,0) to sectors(K,1) and sectors (O,L) to sectors(1,L)
 
Upvote 0
Hi mentor, Same result. It didnt work.

Anyway I changed the code with a workaround.

Many thanks for your help though :)
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,085
Members
449,206
Latest member
ralemanygarcia

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