last day (14 days later) » 

2:24 PM
2
A: VBA Selecting Columns depending on a cell value

EngJonInsert this function: Function FindCols(colVal As String) Columns("B:DD").EntireColumn.Hidden = False Dim LastCol As Integer With ActiveSheet LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column End With Dim i As Integer Dim rng As Range For i = 1 To LastC...

 
good idea, I'll edit that.
 
Thanks for your answer. Where can I change the row where I put the "s"? I've tried row 1 after looking at the code, but this doesn't work.
 
At the if-clause, the code checks for 'Cells(1,i)'. The "1" stands for the first row. So in order to change the row, alter that number. If you want row 3, then change it to 'Cells(3,i)' and dont forget to alter the line under it, too (in this case, that would be 'Set rng = Cells(3,i)')
 
I have tried changing it to row 13 and it still doesn't work. It just hides all columns B:DD. My A column is empty if this makes any difference.
When I MsgBox LastCol, it says 1.
Replacing For i = 1 To LastCol to For i = 1 To 5 gets it to check all columns up to column E.
 
Ugh ... failure on my part. Hiding all columns at the start won't do the trick. I edit my answer accordingly. That has to work. edit: edit on answer is done
 
2:24 PM
Hi,
We're getting there. Thanks for your help. The sort works, but doesn't hide any columns after the last "s".
 
should it?
you have one last s, and every column after that one is not hidden?
 
No, it stops at the last "s", also it doesn't work if it is set to work on another row.
 
Works for me if I try it with "s". Columns not containing "s" are all hidden (also after the last s)
hmm
How many columns are there after the last s?
 
17
Could it be because the LastCol checks the top row?
 
If you output LastCol right after its assignment, does it tell you the number of the very last column?
Uhm yes
that can be the fault
You have to check the same column for lastcol
which row holds your letters?
 
2:33 PM
It will be row 13, I guess it wasn't working with any other rows before, because row 1 was blank.
 
completely blank? then I'm surprised you even came to get some columns hidden
 
So the LastCol checker needs to check 14 as that is where my table header is.
 
thought it was row 13?
check the row where the letters are in
 
It wasn't empty when I was using row 1 to check for "s"
 
you can put a variable for your designated letter row
 
2:36 PM
I understand now. It works.
 
can i put entire code here?
 
Dim desigRow As Integer
desigRow = 13
and then you replace the numbers
like:
LastCol = .Cells(desigRow, .Columns.Count).End(xlToLeft).Column
Set rng = Cells(desigRow, i)
If InStr(1, Cells(desigRow, i), colVal) = 0 Then
think that should work
that way you would only have to change desigRow to whatever value you want
 
I'm using row 14 to check for the last col, and row 1 to check for the "s" as there is always a guarantee of data in for 14.
Or, once I put the other letters in there, they will be populated.
 
you CAN do that, but since you are only checking for these letters, it would be enough to check for column numbers in the same row
hope that helped you
 
2:40 PM
It helped me loads thanks. Now I'm going to work on getting it to work if other values are in the desigRow, like "s,c", "c,s", "s,a,c"...
 
uhm
thats no problem
it will work like that
the "InStr" checks whether the searched string is inside the string. So you can put as much other letters as you want around the wanted one and it will still work
 
Cool, I was thinking I may have to put all the combinations in by running seperate FindCols "different combinations"
 
would be the case if the code was checking for the exact string. But it only has to be contained
 
Nice! Really appreciate your help.
 
feel free to mark my answer as answer. Glad I could help :)
 
2:46 PM
Done. Thanks :)
 
have a good day!
 

last day (14 days later) »