I have very little experience with VBA and I'm now stumped by what I'm trying to accomplish with a macro. Excel 2010.
I have 3 relevant columns. B, C, and AD. (Columns 2, 3, and 30) My data is filtered down to about 30 rows, almost none of which are contiguous (about 500 rows total).
I want the following to happen:
A formula is entered in ONLY THE VISIBLE ROWS in column AD, which will look at the value in column B of that same row and check for that value in all of the VISIBLE CELLS in column C. It cannot look at all of the cells in column C, only the visible ones.
If the value from column B in that row is found anywhere in the VISIBLE CELLS in column C, then "True" should be returned in column AD. I don't care about what is returned when the value is not found, as I will be filtering for the "True" values only. As an added requirement, if the first 3 characters of the value in column B are "010" I need it to return a value of "True" in column AD. I then need this formula copied down column AD for each VISIBLE row.
Right now, I have a formula that will conduct the search in column C for the value in column B. (found on stackoverflow)
=NOT(ISNA(VLOOKUP(B4,C:C,1,0))))
This provides a "True" in column AD when the value from column B is found somewhere in column C. With the "010" constraint, the formula looks like this:
=IF(LEFT(B4,3)="010","True",NOT(ISNA(VLOOKUP(B4,C:C,1,0))))
I am having a problem in that this looks at even the hidden (filtered out) rows. Every one of my values in column B will appear in column C at some point, so I'm only getting "True" for all my entries.
I think there must be a better way to do this than just having a macro paste the formula down (even considering I can't get the formula to work). So, 2 questions:
You're almost there. On its own, COUNTIF
does not have this capability, but if you throw user-defined functions (UDF functions) into the mix, you have an elegant solution.
Add the following code to a module in your code-behind.
Function Vis(Rin As Range) As Range
'Returns the subset of Rin that is visible
Dim Cell As Range
Application.Volatile
Set Vis = Nothing
For Each Cell In Rin
If Not (Cell.EntireRow.Hidden Or Cell.EntireColumn.Hidden) Then
If Vis Is Nothing Then
Set Vis = Cell
Else
Set Vis = Union(Vis, Cell)
End If
End If
Next Cell
End Function
Function COUNTIFv(Rin As Range, Condition As Variant) As Long
'Same as Excel COUNTIF worksheet function, except does not count
'cells that are hidden
Dim A As Range
Dim Csum As Long
Csum = 0
For Each A In Vis(Rin).Areas
Csum = Csum + WorksheetFunction.CountIf(A, Condition)
Next A
COUNTIFv = Csum
End Function
Now you can use the new COUNTIFv()
function to do the same as count, but only include visible cells. This code example was sampled from Damon Ostrander's answer to a similar question , so you will probably need to tweak it slightly. You can either use the COUNTIFv
function in the macro itself, or modify the VLOOKUP
function in a similar fashion to use the worksheet function example you have already. Neither method is really better than the other, so either should work for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.