VBA - Build an Array by Looping Through Another Array with Specific Criteria

0 votes

When it comes to the programming, I'm not sure where to begin. I have an Excel column of data with an array that is dispersed with non-consecutive zeros. I want to create a new column of data and choose the digit that comes before the zeros. A sample data set is provided below (I'll select out 1, 4, and 6):

  5
    1
    0
    4
    0
    820
    4
    6
    0
    74
    Sub Cat()   
     
          Range("V12").Select
    
          Do Until IsEmpty(ActiveCell)
    
             ActiveCell.Offset(1, 0).Select
          Loop
       
    End Sub
Mar 23, 2023 in Others by Kithuzzz
• 38,010 points
360 views

1 answer to this question.

0 votes

Please attempt the next option. It is assumed that A contains the range that needs to be processed. A column's content will be dropped into the resulting array in B:B, starting with B1:

Sub ExtractBeforeZero()
  Dim sh As Worksheet, lastR As Long, arr, arrB0, i As Long, k As Long
  
  Set sh = ActiveSheet
  lastR = sh.Range("A" & sh.rows.count).End(xlUp).row
  
  arr = sh.Range("A1:A" & lastR).Value2 'place the range in an array for faster  processing
  ReDim arrB0(1 To UBound(arr)) 'redim the array to keep the return of its maximum possible number of elements
  k = 1
  
  For i = 1 To UBound(arr)
        If arr(i, 1) = 0 Then arrB0(k) = arr(i - 1, 1): k = k + 1
  Next i
  
  If k > 1 Then 'if at least a zero has been found:
    ReDim Preserve arrB0(1 To k - 1) 'keep only the filled elements
    'drop the resulted array:
    sh.Range("B1").Resize(UBound(arrB0), 1).Value2 = Application.Transpose(arrB0)
  Else
     MsgBox "No zero could be found in the processed column..."
  End If
End Sub
answered Mar 23, 2023 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
1 answer

VBA - Build a Two-Column Array By Looping Through one Array with a Specific Criteria and Selecting From Another Array

As already mentioned as comment: Loop over ...READ MORE

answered Mar 24, 2023 in Others by Kithuzzz
• 38,010 points
271 views
0 votes
1 answer

Excel worksheet multi-criteria function (like Index-Match array) to fetch last value of an item, looking up by item name and date

Use: =SUMIFS(C:C,B:B,E12,A:A,MAXIFS(A:A,B:B,E12)) It will return the value at the ...READ MORE

answered Apr 6, 2023 in Others by narikkadan
• 63,420 points
442 views
0 votes
0 answers

SQL Group By with an Order By

I have a table of tags and ...READ MORE

Jun 22, 2022 in Others by nisha
• 2,210 points
193 views
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,420 points
874 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
909 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,230 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
522 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
763 views
0 votes
1 answer

VBA How do I replace the range with an array in SUMIF

You can't, in my opinion. When you ...READ MORE

answered Feb 7, 2023 in Others by narikkadan
• 63,420 points
511 views
0 votes
1 answer

Looping through a folder with Excel files and scraping date from selected cells usin VBA

You record the outcome of your comparison ...READ MORE

answered Feb 16, 2023 in Others by narikkadan
• 63,420 points
656 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP