Help with vba array

michaelsmith559

Well-known Member
Joined
Oct 6, 2013
Messages
881
Office Version
  1. 2013
  2. 2007
I need help with vba and array. Array's are hard for me to grasp, so need some help. I am wanting to input a large data set into an array. I want the array to remove the first three characters and return the results to a new sheet. In the example below, range(a1:e5) is the example data that would be put into the array and range(a7:e11) is how I would want the results. The data set is large from A1:K1048576 & L1:L568678. I have no clue how to put this into an array let alone work through it efficiently. Any help will be much appreciated. Thanks.

mm combinations.xlsm
ABCDE
11, 2, 3, 4, 52, 7, 10, 41, 523, 15, 34, 64, 674, 43, 44, 50, 536, 18, 26, 49, 52
21, 2, 3, 4, 62, 7, 10, 41, 533, 15, 34, 64, 684, 43, 44, 50, 546, 18, 26, 49, 53
31, 2, 3, 4, 72, 7, 10, 41, 543, 15, 34, 64, 694, 43, 44, 50, 556, 18, 26, 49, 54
41, 2, 3, 4, 82, 7, 10, 41, 553, 15, 34, 64, 704, 43, 44, 50, 566, 18, 26, 49, 55
51, 2, 3, 4, 92, 7, 10, 41, 563, 15, 34, 65, 664, 43, 44, 50, 576, 18, 26, 49, 56
6
72, 3, 4, 57, 10, 41, 5215, 34, 64, 6743, 44, 50, 5318, 26, 49, 52
82, 3, 4, 67, 10, 41, 5315, 34, 64, 6843, 44, 50, 5418, 26, 49, 53
92, 3, 4, 77, 10, 41, 5415, 34, 64, 6943, 44, 50, 5518, 26, 49, 54
102, 3, 4, 87, 10, 41, 5515, 34, 64, 7043, 44, 50, 5618, 26, 49, 55
112, 3, 4, 97, 10, 41, 5615, 34, 65, 6643, 44, 50, 5718, 26, 49, 56
Sheet3
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I don't know if there is a more efficient way:
VBA Code:
Sub test()
  Dim myArr1 As Variant, myArr2 As Variant
  myArr1 = Range("A1:K1048576")
  For i = 1 To UBound(myArr1)
    For j = 1 To UBound(myArr1, 2)
      myArr1(i, j) = Right(myArr1(i, j), Len(myArr1(i, j)) - 3)
    Next
  Next
  Range("A1048578").Resize(UBound(myArr1), UBound(myArr1, 2)) = myArr1
 
  myArr2 = Range("L1:L568678")
  For i = 1 To UBound(myArr2)
    myArr2(i, 1) = Right(myArr2(i, 1), Len(myArr2(i, 1)) - 3)
  Next
  Range("L568680").Resize(UBound(myArr2), 1) = myArr2
End Sub
 
Upvote 0
I am finding your data ranges a bit confusing but given there are no other complications the below should be the fastest way to achieve the removal of the 1st 3 characters.

PS: It won't be best example for learning arrays though since it avoids looping.

VBA Code:
Sub RemoveLeadingCharacters()
    Dim srcSht As Worksheet, destSht As Worksheet
    Dim srcRng As Range, destRng As Range
    Dim srcArr As Variant
    
    Set srcSht = ActiveSheet
    Set destSht = Worksheets.Add(after:=Worksheets(Worksheets.Count))
    
    With srcSht
        Set srcRng = .Range("A1").CurrentRegion         ' Method Depends on sheet layout
        srcArr = srcRng
    End With
    
    Set destRng = destSht.Range("A1")
    
    With Application
        srcArr = .Replace(srcArr, 1, 3, "")             ' Remove 1st 3 characters
        srcArr = .Trim(srcArr)                          ' Remove leading space
    End With
    
    destRng.Resize(UBound(srcArr, 1), UBound(srcArr, 2)) = srcArr

End Sub
 
Upvote 0
I don't know if there is a more efficient way:
VBA Code:
Sub test()
  Dim myArr1 As Variant, myArr2 As Variant
  myArr1 = Range("A1:K1048576")
  For i = 1 To UBound(myArr1)
    For j = 1 To UBound(myArr1, 2)
      myArr1(i, j) = Right(myArr1(i, j), Len(myArr1(i, j)) - 3)
    Next
  Next
  Range("A1048578").Resize(UBound(myArr1), UBound(myArr1, 2)) = myArr1
 
  myArr2 = Range("L1:L568678")
  For i = 1 To UBound(myArr2)
    myArr2(i, 1) = Right(myArr2(i, 1), Len(myArr2(i, 1)) - 3)
  Next
  Range("L568680").Resize(UBound(myArr2), 1) = myArr2
End Sub
I tried the code but excel crashed and gave a out of memory error. So, I am going to try a different approach and start a new thread. Thanks for helping.
 
Upvote 0
I am finding your data ranges a bit confusing but given there are no other complications the below should be the fastest way to achieve the removal of the 1st 3 characters.

PS: It won't be best example for learning arrays though since it avoids looping.

VBA Code:
Sub RemoveLeadingCharacters()
    Dim srcSht As Worksheet, destSht As Worksheet
    Dim srcRng As Range, destRng As Range
    Dim srcArr As Variant
   
    Set srcSht = ActiveSheet
    Set destSht = Worksheets.Add(after:=Worksheets(Worksheets.Count))
   
    With srcSht
        Set srcRng = .Range("A1").CurrentRegion         ' Method Depends on sheet layout
        srcArr = srcRng
    End With
   
    Set destRng = destSht.Range("A1")
   
    With Application
        srcArr = .Replace(srcArr, 1, 3, "")             ' Remove 1st 3 characters
        srcArr = .Trim(srcArr)                          ' Remove leading space
    End With
   
    destRng.Resize(UBound(srcArr, 1), UBound(srcArr, 2)) = srcArr

End Sub
I tried the code but excel crashed and gave a out of memory error. So, I am going to try a different approach and start a new thread. Thanks for helping.
 
Upvote 0
Like I said your data ranges don't really make sense. Are you really using all 1M + rows in Excel ?
My Excel instal is 64 bit handled the volume without any issues but I imagine that may not be the case for 32 bit instal.

At those volumes you really should be using Power Query and the data model.
Using all the rows in Excel would leave you at a significant risk of truncating your data (ie dropping rows beyond the 1,048,576)
 
Upvote 0
I'm afraid that you are using very old versions of Excel. Power Query it is standard from windows excel 2016 on.
In for 2010 it was an add-in and I am not sure about 2013 I think it might depend which type of licence you have.
PQ, Power Pivot & the Data Model and Power BI are all geared towards handling larger volumes of data. Even volumes exceeding Excel's total number of rows.

Here is a 9 min intro on Power Query from Leila Gharani:

 
Upvote 0
I'm afraid that you are using very old versions of Excel. Power Query it is standard from windows excel 2016 on.
In for 2010 it was an add-in and I am not sure about 2013 I think it might depend which type of licence you have.
PQ, Power Pivot & the Data Model and Power BI are all geared towards handling larger volumes of data. Even volumes exceeding Excel's total number of rows.

Here is a 9 min intro on Power Query from Leila Gharani:

I'll look into downloading the power query add-in.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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