Help expanding array

gordsky

Well-known Member
Joined
Jun 2, 2016
Messages
555
Office Version
  1. 2016
Platform
  1. Windows
Have been trying to sort this for a while so any help appreciated.

the below code works great for looping through the range and adding a single cell to an array before looping and adding another single cell etc and finally pasting the array to another sheet.
What I want to do is rather than add a single cell I want to add the cell and the adjacent cell to the array before continuing the loop and then pasting to the other sheet.

I know I could run the array twice and just change the column references but can someone explain how i expand the array to do it in 1 go

The array would take data from say col "C:D" from arrow before continuing the loop through the rows.

VBA Code:
For Each cel In ws1.Range(ws1.Cells(TPS1, Mycol), ws1.Cells(TPS2 - 3, Mycol))
      If cel <> "" Then
         If cel = "A" Or cel = "B" Or cel = "C" Or cel = "D" Then
            ReDim Preserve MySupv(1 To x)
            MySupv(x) = ws1.Cells(cel.Row, Mycol - 3)
            x = x + 1
            
         Else
            'do something else if criteria not met
         End If
      Else
      End If
      
   Next cel

ws11.Cells(1, 1).Resize(x - 1) = Application.Transpose(MySupv)
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Sounds like you need a two-dimensional array instead of a one-dimensional
 
Upvote 0
Sounds like you need a two-dimensional array instead of a one-dimensional
Thanks for the reply, could you expand on how as that is the bit I am not sure of
 
Upvote 0
Probably something like


VBA Code:
 ReDim MySupv(1, 0)
 
 For Each cel In ws1.Range(ws1.Cells(TPS1, Mycol), ws1.Cells(TPS2 - 3, Mycol))
    If cel <> "" Then
       If InStr("ABCD", cel) Then
          ReDim Preserve MySupv(1, x)
          MySupv(0, x) = ws1.Cells(cel.Row, Mycol - 3)
          MySupv(1, x) = ws1.Cells(cel.Row, Mycol - 2)
          x = x + 1
         Else
            'do something else if criteria not met
         End If
      Else
    End If
 Next cel

 ws11.Cells(1, 1).Resize(x, 2) = Application.Transpose(MySupv)
 
Upvote 0
Solution
Probably something like


VBA Code:
 ReDim MySupv(1, 0)
 
 For Each cel In ws1.Range(ws1.Cells(TPS1, Mycol), ws1.Cells(TPS2 - 3, Mycol))
    If cel <> "" Then
       If InStr("ABCD", cel) Then
          ReDim Preserve MySupv(1, x)
          MySupv(0, x) = ws1.Cells(cel.Row, Mycol - 3)
          MySupv(1, x) = ws1.Cells(cel.Row, Mycol - 2)
          x = x + 1
         Else
            'do something else if criteria not met
         End If
      Else
    End If
 Next cel

 ws11.Cells(1, 1).Resize(x, 2) = Application.Transpose(MySupv)
Ah many thanks I see so I have to do it as individual cells but in one. Thanks
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,016
Members
448,543
Latest member
MartinLarkin

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