Consolidating finding non-blank range into 1 line - vba

I am trying to count the number of non-blank cells in a column B. I know the syntax on this is off but I am not sure how to make it work correctly. I am trying to avoid doing a loop and and make it as simple as possible. Any thoughts would be greatly appreciated.
With Dashboard
Total_Emails = Dashboard.Cells(Dashboard.Rows.Count, "B").End(xlUp).Row - WorksheetFunction.CountBlank(Range("B1:B&Dashboard.Cells(Dashboard.Rows.Count, "B").End(xlUp).Row))
End With

Use CountA to count all the non-blanks in a range.
Total_Emails = WorksheetFunction.CountA(Columns("B"))

I figured that it is actually easier to split it up into several different expressions. This works correctly.
Dim CountLast As Long
CountLast = Dashboard.Cells(Dashboard.Rows.Count, "B").End(xlUp).Row
Dim Answer As Long
Answer = WorksheetFunction.CountA(Range("B1:B" & CountLast))
With Dashboard
Total_Emails = CountLast - Answer
End With

Related

Increment the value of a cell by 1 after checking values in a range of cells

I know this is as trivial and easy as it gets, but I can't figure it out for the life of me. I am completely clueless when it comes to excel formulas.
[2
I would like the cell U7 to increment by 1 for each "D" it finds in the selected range of cells (E7:S7), same with the cell V7, increment by 1 but for each "N" it finds in the same range of cells.
I was trying to use multiple if statements like:
=IF(E7:S7="D";1;0)
But that doesn't seem to work at all. Any help would be greatly appreciated.
This is what you are after. Use COUNTIF to count the number of times D or N occur in your range.
=COUNTIF(E7:S7,"D")
You want to use the COUNTIF()-Function:
=COUNTIF(E7:S7, "D")
For more reference, see the Microsoft Office support site.
You can use a =COUNTIF() Function.
You referred to VBA, so I assume you want to run this in a program possibly.
The code below will run through the used rows starting in row 7 and then insert the number of 'D" letters seen in the columns E to S.
Sub CountNumberOfDLetters()
Dim LastRow As Long
With ActiveSheet.UsedRange
LastRow = .Rows(.Rows.Count).Row
End With
Dim RowReference As Long
Dim DataRange As Range
Dim NumberOfD As Double
For RowReference = 7 To LastRow
Set DataRange = Range(Cells(RowReference, "E"), Cells(RowReference, "S"))
NumberOfD = WorksheetFunction.CountIf(DataRange, "D")
Cells(RowReference, "U").Value = NumberOfD
Next RowReference
End Sub

Match Function in Specific Column Excel VBA

I'm trying to write a program in VBA for Excel 2011 that can search a column (which column that is is determined by another variable) for the number 1 so that it knows where to start an iteration.
Say that the number of the column is given by colnumvar. The only way I can think of is the Match function, which led me to write the following:
Dim rowvar As Integer
rowvar = WorksheetFunction.Match(1,Range(Cells(1,colnumvar),Cells(1000,colnumvar)),0)
This gave me an error, however. After playing around with it some more, I realized that it must not accept the Cells([row],[col]) way of doing it, but rather wants something like Range("A1:A100"). Unfortunately, I can't do it that way, since the program is figuring out what column to look in. Any help for figuring out how to get past this would be greatly appreciated!
What you mean to do is better served with Range.Find.
Dim rngtrg As Range, rngsrc As Range
Dim ws As Worksheet
Set ws = ActiveSheet
Set rngsrc = ws.Range(ws.Cells(1,colnumvar),ws.Cells(1000,colnumvar))
Set rngtrg = rngsrc.Find(1,...)
rowvar = rngtrg.Row
this easy function retreive the positoin of that you find
Function rowvar(ByRef c As Integer) As Integer
Dim keySrc As Integer
keySrc = 22 'wath you want to find
rowvar = WorksheetFunction.Match(keySrc, Range(Cells(1, c), Cells(1000, c)), 0)
End Function
use with rowvar(x)

excel vba moving non-contiguous range selection to an array

In the situation where the user select two non-contiguous column ranges i wrote the following:
Dim count long
Dim points variant
Dim i long
Set user_range = ActiveWindow.RangeSelection
count = user_range.count / 2
ReDim points(1 To count, 1 To 2)
For i = 1 To count
MsgBox "value is" & user_range.Areas.Item(1).Value(i,1)
points(i, 1) = user_range.Areas.Item(1).Value(i,1)
points(i, 2) = user_range.Areas.Item(2).Value(i,1)
Next i
But i get an object error when i try this. Am i indexing Value wrong?
This should work right? Is there an easier way to do this?
Any help is greatly appreciated!
Thanks,
Russ
I'm afraid your code does not compile. First of all, you need to declare your variables correctly. You should also use Option Explicit.
Option Explicit
Dim count As Long
Dim points As Variant
Dim i As Long
Dim user_range As Range
The count and ReDim lines are OK, but you are assuming that the two selections are both the same size. Will that always be the case?
Then I'm not sure what it is you want to do, but I'm guessing you just want to save the values in user_range into points.
You need to adress them a bit different:
points(i, 1) = user_range.Areas(1).Cells(i, 1).Value 'Selection 1
points(i, 2) = user_range.Areas(2).Cells(i, 1).Value 'Selection 2

UsedRange.Count counting wrong

Summary: I'm taking a row of data from one sheet and pasting it into another, however the sheet would be a daily use kind of thing where new data is just entered below old data.
Problem: On each new run, 7 is consistently added to the UsedRange.Count. For example: on one run the UsedRange.Count will be 7; the next time I run through the function the count will be 14.
What I'm Looking For: Why is this the case and is there a way to help UsedRange be more accurate
-I've included the entire Function for references' sake.
Function eftGrabber()
Dim usedRows As Integer
Dim i As Integer
ChDir "\\..."
Workbooks.Open Filename:= _
"\\...\eftGrabber.xlsm"
usedRows = Sheets("EFT").UsedRange.Count
Windows("Data").Activate
Sheets("DataSheet").Range("A11").EntireRow.Copy
Windows("eftGrabber").Activate
Sheets("EFT").Range("A" & usedRows + 1).Select
ActiveSheet.Paste
i = usedRows
Do 'THIS LOOP DELETES BLANKS AFTER POSTING NEW LINES
Range("A" & i).Select
If Range("A" & i) = "" Then
ActiveCell.EntireRow.Delete
End If
i = i - 1
Loop Until i = 1
Windows("eftGrabber").Activate
ActiveWorkbook.Save
Windows("eftGrabber").Close
End Function
Let me know if I've left out any important details. Thanks in advance!
Change: usedRows = Sheets("EFT").UsedRange.Count
To: usedRows = Sheets("EFT").Range("A" & Sheets("EFT").Rows.Count).End(xlUp).Row
Where "A" can be changed to whichever row you wish to count the total number of columns.
There is a danger in using UsedRange because it factors in such things and formatted cells with no data and other things that can give you unexpected results, like if you are expecting your data to start in Range("A1"), but it really starts in another range!
I will say, however, that If you really wish to use UsedRange, your code above is still wrong to get the rows. Use this instead UsedRange.Rows.Count or to get the last absolute cell of the UsedRange, use UsedRange.SpecialCells(xlCellTypeLastCell).Row
This two line do the magic
usedCol = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
usedRow = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
For more info visit Microsoft's site
http://msdn.microsoft.com/en-us/library/office/ff196157.aspx
Thanks for the discussion...
.UsedRange.Rows.Count and .UsedRange.Columns.Count work fine provided there is something in cell A1. Otherwise need to use the SpecialCells solution.
Hope this is helpful.
“UsedRange” works if you use it like this >>
x := Sheet.UsedRange.Row + Sheet.UsedRange.Rows.Count - 1;
y := Sheet.UsedRange.Column + Sheet.UsedRange.Columns.Count - 1;
Problem with SpecialCells is that you can't use it on a Protected Sheet.
Assuming you have contiguous sheet (i.e. no blank cells), and you sheet starts in A1, then I have found that
Range("A1").CurrentRegion.Rows.Count
gives the most reliable results.

Trying to find the number of rows in a particular sheet

Update: I figured out my error - the variable sheetRange needed the Sheets("Schedule"). added to it as well
Sorry for the inconvenience.
I have a relatively simple problem in that I am trying to use VBA to find the number of rows in a particular sheet. I am getting a pop up box that just says 400 and am not really sure where my syntax is off.
Sub PhxCheck()
Dim i As Integer, x As Integer, numofRows As Integer
Dim top As Range, bottom As Range, sheetRange As Range
Dim phxContract As String, contractID As String
Set top = Sheets("Schedule").Range("A3")
Set bottom = Sheets("Schedule").Range("A65536").End(xlUp)
Set sheetRange = Range(top, bottom)
numofRows = sheetRange.Rows.Count
Cells(30, 1).Value = numofRows
End Sub
The error happens when I add Sheets("Schedule"). to the top and bottom ranges.
Thanks for your help!
You can simplify your code a lot and avoid using redundant variables which will limit mistakes.
All you need is:
With Sheets("Schedule")
Cells(30, 1).Value = .Range("A3", .Cells(Rows.Count, "A").End(xlUp)).Rows.Count
End With
I understand you solved your problem yourself, but perhaps the above will help you or someone else searching for a similar problem.