So I have a range that will be selected by a user. It will be two columns, usually not next to each other. I need to count the number of cells in each column of the selected range to determine if the number of cells in each column is equal. (If it's not I will need to adjust the range.)
For example, a User may select B5:B10 and D6:D9. So I need the code to return 6 and 4 respectively.
I've tried:
Set rng = Selection
rng.Columns(1).Count
this returns 1, which obviously isn't the number I need.
Thanks in advance!
You can use the Areas method of the Range object to get the areas of the range. Areas are groups of contiguous ranges within a non-contiguous range.
Set rng = Selection
For i = 1 to rng.Areas.Count
debug.print rng.Areas(i).Cells.Count
Next
There is caveat here that you may need to test for, and that is if the user selects, for example, A1:B10, with one mouse drag. Since this is a contiguous range, it will only have one Area and you will not get two distinct numbers. If you need to test for this, you can do something like the below.
Set rng = Selection
'non-contiguous ranges will always return one column, if there are mutiple columns both cell counts are equal by default
If rng.Columns.Count = 1 Then
For i = 1 to rng.Areas.Count
debug.print rng.Areas(i).Cells.Count
Next
End If
Dammit #Scott - just beat me to it.
Although I'm using the Rows property - if the user selects A1:B19 it will return 19 in a single element of the array, if they select A1:A19 and then B1:B19 it will return 19 in two elements of the array.
Using Cells it will return 38 in a single element, or 19 in two elements.
Sub Test()
Dim rRange As Range
Dim lRows() As Long
Dim x As Long
Set rRange = Selection
With rRange
ReDim lRows(1 To .Areas.Count)
For x = 1 To .Areas.Count
lRows(x) = .Areas(x).Rows.Count
Next x
End With
End Sub
Related
I regularly work with data spanning multiple columns and need a convenient way to highlight multiple rows that contain the same value in a specific column, but I need to alternate between highlighted and non-highlighted.
For example, I'll have several rows with data in Column A like:
700105862
700105862
700105862
700103235
700103235
700108783
700108783
700108783
And what I'd want to do is highlight the first three rows (700105862), then not highlight 700103235, then again, highlight 700108783.
I was wondering if there was a conditional formatting formula that'd make this possible.
Any help would be greatly appreciated!
Thank you,
if your numbers are divided into chunks of always different numbers repetitions, then you could use this VBA code:
Sub main()
Dim item As Variant
Dim startRow As Long
Dim okHighlight As Boolean
With Range("A1", Cells(Rows.count, 1).End(xlUp))
For Each item In GetUniqueValues(.Cells).Items
If okHighlight Then .Range(.Cells(startRow, 1), .Cells(item, 1)).Interior.ColorIndex = 48
startRow = item + 1
okHighlight = Not okHighlight
Next
End With
End Sub
Function GetUniqueValues(rng As Range) As Dictionary
Dim cell As Range
Dim dict As Dictionary
Set dict = New Dictionary
With dict
For Each cell In rng
.item(cell.Value) = cell.row - rng.Rows(1).row + 1
Next
End With
Set GetUniqueValues = dict
End Function
a Conditional formatting approach is possible on with a helper column
assuming:
your data are in column A and begin from row 2
column B is free
then:
write the following formula in helper column B cells:
=IF(A2<>A1,B1+1,0)
apply conditional formatting to column A with the following formula:
=INT(B2/2)=B2/2
and choosing the format you like to highlight cells
Sure, if you know what ranges you want to highlight you'd simply set the conditional formatting to be between x and y values. Comment on this question with what you dont get and I'll amend the answer accordingly.
I have a list of values mentioned in a column. i want to use vba to pick x number of values from that list.the number x is mentioned in another cell. can you help me?
i have tried various formulas but no solution.
assuming myColumnRng is your range of cells in a columns containing your list of values, you can get its first N elements in a Variant array as follows:
myElements = Application.Transpose(myColumnRng .Resize(nElements).Value)
to give a little bit more of context:
Option Explicit
Sub main()
Dim myColumnRng As Range
Dim myElements As Variant
Dim iEl As Long
Set myColumnRng = Range("A1", Cells(Rows.count, "A").End(xlUp)) '<--| set your range as the column "A" one from row 1 down to last not empty row
myElements = Application.Transpose(myColumnRng .Resize(nElements).Value)
For iEl = 1 To UBound(myElements)
Debug.Print myElements(iEl)
Next
End Sub
Pick x values on random, or pick first x values in the list?
I have some sample data in a sheet, I want to get headers of each column in used range, Here is the sample
I never know the exact number of columns used, I need to get all headers (Customers, Managers, Managers-2....)from this sheet to show it to user.
I can loop through each cell in a used range like
For Each cell In ActiveWorkbook.Worksheets(SheetName).UsedRange
Next
but how can I loop though each column, and only read values from their 1st cell? as number of these lists can be one or more than one.
Replace your code by this one iterate over the first row of used range:
For Each cell In ActiveWorkbook.Worksheets(SheetName).UsedRange.Rows(1).Cells
...
Next
'Get a 2D array of header values
Dim aHeaders() As Variant
aHeaders = Sheet1.UsedRange.Rows(1).Value
It all depends on your data quality. Come across this numerous times.
If your headers are consistent, then you just need to resize to the first row. Something like:
Dim r as range, r1 as range
Set r = Range("A1").CurrentRegion.Resize(1)
Then do a for each for the r range.
For Each r1 in r
Next
You could also use an integer variable....
Dim i as integer
For I = 1 to r.Count
Next
32bit Excel 2013 / Win 7 64 bit
UDF asks user for two range inputs from the same table and a lookup value ie:
Public Function FindBfromA(A as Range,B as Range, IDValue as Integer)
For IDCheck = 1 to A.Count
IF A(IDCheck) = IDValue then
IDNum = IDCheck
Exit For
End if
Next IDCheck
FindBfromA = B(IDNum)
End Function
Formula is added into another column of the table, for example
=FindBfromA([A],[B],[#C])
'Where C is calculated via something
My issues is Ranges A & B become disjointed. Where A(IDCheck) and B(IDCheck) should belong to corresponding columns in the same table row, based on where my cursor is when calculating begins Range [B] will re-key
This then causes the formula to return the wrong value from the FindBfromA=B(IDNum) as A(IDNum){Row} <> B(IDNum){Row}
I couldn't reproduce the error in the workbook I created with the false data - in my company (private) workbook the function operates essentially the same way, but captures two 'B' values given two IDs and passes them to another function.
It's difficult to be sure without seeing your range selections, but the unreliable element of your code is the cell references. By using a single integer index, you are basically selecting the nth cell in the range rather than cell on row n. My suspicion is that range B is offset from range A by a number of rows. Let's say your two selections were A = "A1:A10" and B = "B2:B11" then A(3), for example, would be on row 3 but B(3) would be on row 4. The same would apply if Range A had more than one column.
To eliminate that risk, refer to the ranges by the row and column indexes, as in the code below. You'll note I've also change the data type of the IDValue to a variant as this prevent an error being thrown in your IDValue should ever be something like a String or Long. I've also looped through range A with a For Each loop on each cell to cater for the case that range A has more than one column.
Public Function FindBfromA(A As Range, B As Range, IDValue As Variant) As Variant
Dim cell As Range
For Each cell In A.Cells
If cell.Value2 = IDValue Then
FindBfromA = B.Cells(cell.Row, 1).Value2
Exit Function
End If
Next
End Function
I want to count the unique values of a column therefor I first used a autofilter to get the correct rows that I want to advanced filter.
The result I've assigned to a range using
intersect(Used.range,range.specialcells(xlCellTypeVisible))
This gave me a range say (B10:B20, B75) so it's actually 2 ranges B10:B20 and B75. Therefor I cannot directly apply a advancedfilter on it. Does someone know how to do achieve this?
I've thought of splitting the range into two seperates but then I cannot count the unique values correctly, I'll get a count of one range and a count of the other but it's possible that they have the same values. I've also thought to put the values in a array, but it's not a very efficient way to do it I think.
Thanks.
Advanced Filter is not required if you only need a count of the filtered uniques.
Consider:
Sub Xepos()
Dim N As Long, c As Collection
Dim colid As String, r As Range
colid = "A"
Set c = New Collection
N = Cells(Rows.Count, colid).End(xlUp).Row
On Error Resume Next
For i = 2 To N
Set r = Cells(i, colid)
If r.EntireRow.Hidden = False Then
v = r.Text
c.Add v, CStr(v)
End If
Next i
MsgBox c.Count
End Sub
This assumes that the column is column A and there is a header cell in A1