Trying to write VBA script for the choosecols and filter function in Excel

Rcflife2335

New Member
Joined
Feb 15, 2024
Messages
2
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello,
I’m trying to write a vba script to return all columns that match a list of numbers. Some numbers have multiple rows of data. I use the choosecols and filter function to return the data which works. Now I’m trying to write a VBA script that does the same thing. Problem now is that it’s not returning the full data and my counter doesn’t continue. Any help is welcome thank you. Code below




Sub all()

Dim shODS As Worksheet
Dim shCFC As Worksheet
Dim indx As Integer
Dim LastROW As Integer


Set shODS = ThisWorkbook.Worksheets("ODS_PROD")
Set shCFC = ThisWorkbook.Worksheets("CFC_PROD")
LastROW = shODS.Cells(Rows.Count, 1).End(xlUp).Row



shODS.Range("N5").Formula = "=CHOOSECOLS(FILTER(CFC_PROD!A:L,CFC_PROD!A:A=ODS_PROD!D941," & Chr(34) & " " & Chr(34) & "), 3,6,9)"
shODS.Range("O5").Formula = "=CHOOSECOLS(FILTER(CFC_PROD!A:L,CFC_PROD!A:A=ODS_PROD!D941," & Chr(34) & " " & Chr(34) & "),3)"

For indx = 2 To LastROW
shODS.Range("K" & indx).Formula = "
=CHOOSECOLS(FILTER(CFC_PROD!A:L,CFC_PROD!A:A=ODS_PROD!D941," & Chr(34) & " " & Chr(34) & "),3)"
Next

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Does this help?

Rich (BB code):
shODS.Range("N5").Formula2 = ...
shODS.Range("O5").Formula2 = ...

... and my counter doesn’t continue.
I'm not clear what you mean by this?

Some numbers have multiple rows of data.
It's also not clear what you're trying to do in the loop? It will put the same formula into multiple cells.

And if you use .Formula2, you will get #SPILL errors if the filter returns multiple rows.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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