Excel VBA select entire sheet using Range - vba

I am using the following code to search through a sheet to find out how many times does the word 'example' appear.
count = Application.WorksheetFunction.CountIf(Range("A1:A10"), "example")
I can't seem to figure out how to iterate through the entire sheet using the Range function.

Why do you need to iterate through the entire sheet? You can just change the extend the range?
A1:A10
count = Application.WorksheetFunction.CountIf(Range("A1:A10"), "example")
A1:E10
count = Application.WorksheetFunction.CountIf(Range("A1:E10"), "example")
Entire sheet
count = Application.WorksheetFunction.CountIf(ActiveSheet.Cells, "example")

Try below to look for string "example" in entire sheet.
Count = Application.WorksheetFunction.CountIf(Cells, "example")
using wildcards
Count = Application.WorksheetFunction.CountIf(Cells, "*example*")

Related

List visible pivot items with Excel formula

In an Excel pivot table, when I select multiple items in a report filter, Excel just displays that I have selected multiple items.
Data
I can select multiple items:
However, once the selection is done, I don't see which elements I have selected.
I want to display which items are selected, e.g. when the report is printed. I found a way to do this using VBA and a user defined function.
Function GetVisibleItems(FieldName As String) As String
Dim PivotTable As PivotTable
Set PivotTable = ActiveSheet.PivotTables(1)
Dim PivotField As PivotField
Set PivotField = PivotTable.PivotFields(FieldName)
Dim PivotItem As PivotItem
Dim Result As String
For Each PivotItem In PivotField.PivotItems()
If PivotItem.Name <> "(blank)" Then
If PivotItem.Visible Then
If Len(Result) > 0 Then Result = Result & ", "
Result = Result & PivotItem.Name
End If
End If
Next
GetVisibleItems = Result
End Function
Is it possible to get the same result using just Excel formulae, not VBA?
Martin - This probably isn't as straight forward as you are looking for, but using workbook functions to build the string you are looking for ...
Below is a simple table of information ...
... and a pivot table created from it ...
In the pivot, you can see that Person 2 and Person 6 are filtered out.
Back at the table, a column was added using this formula (filled down) ...
=IF(ISERROR(GETPIVOTDATA("Grade",Sheet5!$A$3,$A$1,A2)),"",A2)
Note: Sheet5 contains the Pivot table. A3 is the top left corner of the pivot.
Providing this result ...
Where there are blanks instead of Person 2 and Person 6.
The string was generated using Chip Pearson's StringConcat UDF because it's more compact than all the typing needed with & or CONCATENATE ...
What I was looking for is provided by Slicers. A slicer shows the range that has been selected and is printable. Writing the list of visible Pivot Elements into a cell is possible, maybe even with a tricky Excel formula, but seems like over-engineering it.
The TEXTJOIN function can do this. Excel 2016 onwards.
The TEXTJOIN function combines the text from multiple ranges and/or strings, and includes a delimiter you specify between each text value that will be combined. If the delimiter is an empty text string, this function will effectively concatenate the ranges.

Removing a row in a table if it doesn't contain keyword

Right now I have a really long table in a Word doc which I populated from an Excel worksheet. It has 6 columns and I'm trying to code something in Word VBA that will go through all the rows in the table and delete the entire row if the cell in the first column DOES NOT start with an equal sign ("=").
For example, I'm only trying to keep the rows that has texts like,
"=1+S -03F7", "=1+M -06M1", etc. etc.
How would I code this? I can't give the code anything specific to look for since the parts after the equal sign will be different for every row.
So this wouldn't work, right?:
If Not ActiveDocument.Tables(83).Columns(1).Range.Text = "=" Then
EntireRow.Select
Selection.Delete
I guess I should reference to cells in column 1, not the column itself... Also, it doesn't work because it's only looking for things with just the equal sign... And I don't know how I can get it to select the row if it find the cell without the equal sign. I don't know how to match by case in the cell of the first column.
You can loop through the rows in the table using the Rows property. You can then find the first cell in that Row using the Cells property. You can then check just the first character of the Range:
Sub DeleteUnwantedRows()
Dim t As Table
Dim r As Row
Set t = ActiveDocument.Tables(1)
For Each r In t.Rows
If r.Cells(1).Range.Characters(1) <> "=" Then r.Delete
Next r
End Sub

Best way to return data from multiple columns into one row?

I have a sheet with just order numbers and another with order numbers and all of the data associated with those order numbers. I want to match the order numbers and transfer all of the available data into the other sheet. I've been trying to use loops and VLOOKUP but I'm having problems (plus I have 116 columns I want to transfer data from so my vlookup expression doesn't look very nice). Any advice would be appreciated!
this is what I have so far and I'm getting an object error.
I don't think it's the right way to go about it in general though.
Dim LookUpRange As Range
Dim row As Range
Set LookUpRange = Worksheets("batches").Range("B4:B1384")
Set row = Worksheets("batches").Range("C:DL")
For Each row In LookUpRange
row.Select
Selection.FormulaArray ="=VLOOKUP(RC[-1],OrderLvl!RC[-1]:R[1380]C[113],{2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,207,108,109,110,111,112,113,114,115},FALSE)"
Next row
End Sub
Please consider this VBA script to resolve your inquiry:
Sub LookupOuput()
Dim OrderNumberColumn As Range
Set OrderNumberColumn = Worksheets("batches").Range("B2:B1384")
Dim LookUpRange As Range
Set LookUpRange = Worksheets("OrderLvl").Range("C:DL")
Dim cell As Range
Dim FindResult As Range
For Each cell In OrderNumberColumn
If Not cell.Value2 = Empty Then
Set FindResult = LookUpRange.Find(what:=cell.Value2)
If Not FindResult Is Nothing Then
cell.Range("A1:DJ1").Value2 = LookUpRange.Rows(FindResult.row).Value2
End If
End If
Next cell
End Sub
Basically searches for each Order Number in the first sheet on the second sheet. This outputs (if search term exists) the cell that that string is found which we later refer to its row number to output the whole row to the first sheet. Cheers,
A regular VLOOKUP may be able to give you what you need, if you use a small trick...
Insert a row above the data table, and put sequential numbers in
each cell of that row. (ie, A1 = 1, B1 = 2, C1 = 3, etc...)
Do the same thing on your blank table.
Assuming that your first order number is in cell A2, put the following formula into B2: =VLOOKUP($A2,[other sheet name]!$A$1:$DZ$5000,B$1,0)
Drag this formula across all 116 columns, then down all however many rows you've got.
You'll need to adjust the ranges, obviously, but make sure that your lookup array starts in column A. (or alternatively, that your numbers start in the same column as the first column in your array.) Adding the numbers along the top allows you to change what column of the array you're referencing, just by dragging the cell formula.

Find and count the number of occurrences in a sheet

I need to find out how many times a sequence of numbers occurs in a sheet using VBA. For example:
201-1-55-8799
301-5-55-8799
202-1-55-8799
201-1-55-8799
999-5-55-8799
001-2-55-8799
I want to find out how many times 201-1 occur in this sheet. When you do FindAll in Excel it tells you at the bottom how many cells found.
I have experimented with CountIf but that only works if the cell just contains exactly 201-1.
The answer to the above search should be 2 instances of 201-1 found.
Then I need to write the number of occurances in a differnt sheet.
Thanks
Use the COUNTIF formula
=COUNTIF(A2:A12,"201-1*")
You could do this, using VBA
Dim tab_input as Variant
tab_input = ~your range~
specific_counter = 0
For i = 1 to Ubound(tab_input)
If Left(tab_input(i,1),5) = "201-1" Then
specific_counter = specific_counter + 1
End If
Next
msgbox specific_counter
That will count how many cells that have left text starting with 201-1 and and show in a box the amount.
I would recommend using Excel's own find function programmatically. Something like this:
http://www.ozgrid.com/VBA/find-method.htm

VBA Count cells in column containing specified value

I need to write a macro that searches a specified column and counts all the cells that contain a specified string, such as "19/12/11" or "Green" then associate this number with a variable,
Does anyone have any ideas?
Do you mean you want to use a formula in VBA? Something like:
Dim iVal As Integer
iVal = Application.WorksheetFunction.COUNTIF(Range("A1:A10"),"Green")
should work.
This isn't exactly what you are looking for but here is how I've approached this problem in the past;
You can enter a formula like;
=COUNTIF(A1:A10,"Green")
...into a cell. This will count the Number of cells between A1 and A10 that contain the text "Green". You can then select this cell value in a VBA Macro and assign it to a variable as normal.
one way;
var = count("find me", Range("A1:A100"))
function count(find as string, lookin as range) As Long
dim cell As Range
for each cell in lookin
if (cell.Value = find) then count = count + 1 '//case sens
next
end function
If you're looking to match non-blank values or empty cells and having difficulty with wildcard character, I found the solution below from here.
Dim n as Integer
n = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
Not what you asked but may be useful nevertheless.
Of course you can do the same thing with matrix formulas.
Just read the result of the cell that contains:
Cell A1="Text to search"
Cells A2:C20=Range to search for
=COUNT(SEARCH(A1;A2:C20;1))
Remember that entering matrix formulas needs CTRL+SHIFT+ENTER, not just ENTER.
After, it should look like :
{=COUNT(SEARCH(A1;A2:C20;1))}