Select only cells in which the formula give numbers and not empty cells - vba

I have an excel sheet on which I applied to all the cells the following formula:
=IF(OR(A2="";B1="");""; IF(ISERR(VLOOKUP($A2&"-"&B$1;Sheet1!$D$1:$D$1000;1;FALSE));0;1))
The formula gives 0 or 1 based on certain conditions and "" (empty cell) if the conditions are not met.
Now, starting from these results, I'd like to select all the cells in the sheet giving as result 0 or 1, for further operations.
How can I do it, knowing that all these cells will not have empty spaces between?

Try,
dim a as long, i as long, rng as range
with cells(1, "A").currentregion
set rng = .specialcells(xlCellTypeFormulas, xlnumbers)
for a=1 to rng.areas.count
for i=1 to rng.areas(a).cells.count
'do something with rng.areas(a).cells(i)
next i
next a
end with
'more simply as,
with cells(1, "A").currentregion
.specialcells(xlCellTypeFormulas, xlnumbers).select
end with
You can also use F5 (GoTo), Special, Formulas, Numbers, OK.

Adapted answer from Barry Carter:
In the Find and Replace dialog box, type "1" in the Find What box.
Click Find All, to see a list of cells with "1"
Select an item in the list, and press Ctrl+A, to select the entire list.
Now you have selected all of the cells with "1"
If you need to select all 0 and 1 you can go to Search & Select again,
Then to "Go To"
Then to "Special"
There if you select "Formulas" and "Numbers" you will end up selecting all of your 0 and 1

Related

Saving number as text, how to automate it

I have a 7702216772 number inside a cell. If I put a ' before the fist digit and click Enter Excel transforms the number to a text and puts a green triangle at the left top of the cell:
I have many rows of similar numbers all of which need to be transformed into text. However clicking each and adding ' before the first symbol and clicking Enter would take a lot of time. Is there any way to do it programatically?
I tried using formula: ="'"&H4 but it doesn't do what's expected - the green triangle never appears on the result cell.
I also tried setting cell format to Text, but the green triangle doesn't appear in that case too.
I need the green triangle to appear at the upper left corner, just like at the picture!
If all your number are in a single column, the following code will do it:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 1 To LastRow 'loop from row 1 to last
ws.Cells(i, "A").Value = "'" & ws.Cells(i, "A").Value 'add the ' before the number
Next i
End Sub
Change the "A" to whichever column you are using.
Just Select the cells you wish to process and run this short macro:
Sub Textify()
Dim rng As Range, r As Range
Set rng = Selection.Cells.SpecialCells(2, 1)
For Each r In rng
r.Value = "'" & r.Value
Next r
End Sub
Non VBA answer; I'm using Column G in this answer but it depends on where your numbers are. You'll have to change the cell but I think you will be ok with this.
In an empty cell, enter formula: ="'"&G4
Use the fill handle or Ctrl+D to fill it down to the length of Column G's values.
Select the whole of Column G's values and copy them to the clipboard
Select the same range in Column G, right-click, select Paste Special and choose Values
I have tested it now for several times and it worked always
Cells(xx, xx).FormulaR1C1 = "'" & Cells(xx, xx).Value
Same would work for ActiveCell or whatever you like.

copy conditional formatting format but not rules/numbers

need some help thinking through how to do this.
ultimately, what i want to achieve is to sum together cells based on if another corresponding cell is 0 or 1. it's a bit convoluted but i'll try my best to explain.
sheet 1 has data that shows, by month, if actuals have come in for the month. a column will display a 0 for accounts/month that need to be added.
sheet 2 has two tables. table 1 pulls in the 0 & 1 from sheet 1 and uses conditional formatting to highlight cells that need to be added. the only thing in this table is 0 & 1. table 2 is the exact same setup as table 1, just with the actual numbers.
my original thought was to just copy the highlighting format from table 1 onto table 2 then use a macro to sum highlighted cells. obviously, i have found that that is not possible.
i tried looking around and haven't found anything that lets me just copy the highlighting format without overwriting the numbers in table 2.
is this possible?
Try this. I'm not sure how your data is laid out, but if it's in a row, you can run this on a new cell at the end of that row (meaning, the next empty column):
Function sumHighlighted(ByVal myRng As Range)
Dim cel As Range, iRow As Range
Dim fSum As Long, totalRows As Long
fSum = 0
For Each cel In Range(Cells(myRng.Row, 1), Cells(myRng.Row, myRng.Column))
If cel.Interior.ColorIndex <> -4142 Then
fSum = fSum + cel.Value
End If
Next cel
Debug.Print "The total in this row is: " & fSum
sumHighlighted = fSum
End Function
Steps:
1. Copy the Cell with conditional Format.
2. Paste to cell where you want (Hold ALT + press E + S + T).
To generate a code, click "Record Macro" first and do the 2 steps above and stop the recording macro once you're done.

How to skip through selected cells in EXCEL, using VBA,

I have a VBA macro which selects several cells based on if it contains conditional formatting. These cells won't all be in the same place on each sheet.
What I am looking for is a command to skip the activecell to the next cell in the range.
The same as pressing TAB on a highlighted range
At the moment I am using sendkeys, as below, however this is messy, and keeps adding Tab spaces in the next line of the vba code (hence the "____Loop")
ActiveCell.SpecialCells(xlCellTypeAllFormatConditions).Select
Do Until Recount = Count
Recount = Recount + 1
Application.SendKeys "{TAB}", True
Loop
Any advice would be appreciated
Here's how you can loop over the range:
Dim rng As Range, c As Range
Set rng = ActiveSheet.UsedRange.SpecialCells(xlCellTypeAllFormatConditions)
For Each c In rng
c.Select
Next c
It's not clear what the aim of your code is though. What are Count and Recount?
Get a list of selected cells and loop through them
Sub loopThroughCells()
Dim r as Range
Set r = Application.Selection
For i = 0 to r.length
MsgBox(r.value)
Next i
End Sub
Suppose three cells with values 1, 2 and 3 are selected. On running the above macro, you will get message boxes with the values 1, 2 and 3 respectively.
If you only need the command for the tab button, just use the .offset(#of rows you want to offset, #of columns you want to offset). So once you know how to locate the cells you need, which you seem to already have, then you can just put.offset(0,1) to move one cell to the right.

VBA Go to last empty row

I have a project on excel macro, I need to highlight the next last row that has an empty value. example cell A1:A100 have data and the next cell is A101 is empty.
when user click a button it should highlight the cell A101...
If you are certain that you only need column A, then you can use an End function in VBA to get that result.
If all the cells A1:A100 are filled, then to select the next empty cell use:
Range("A1").End(xlDown).Offset(1, 0).Select
Here, End(xlDown) is the equivalent of selecting A1 and pressing Ctrl + Down Arrow.
If there are blank cells in A1:A100, then you need to start at the bottom and work your way up. You can do this by combining the use of Rows.Count and End(xlUp), like so:
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
Going on even further, this can be generalized to selecting a range of cells, starting at a point of your choice (not just in column A). In the following code, assume you have values in cells C10:C100, with blank cells interspersed in between. You wish to select all the cells C10:C100, not knowing that the column ends at row 100, starting by manually selecting C10.
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlUp)).Select
The above line is perhaps one of the more important lines to know as a VBA programmer, as it allows you to dynamically select ranges based on very few criteria, and not be bothered with blank cells in the middle.
try this:
Sub test()
With Application.WorksheetFunction
Cells(.CountA(Columns("A:A")) + 1, 1).Select
End With
End Sub
Hope this works for you.
This does it:
Do
c = c + 1
Loop While Cells(c, "A").Value <> ""
'prints the last empty row
Debug.Print c

How to delete row based on cell value

I have a worksheet, I need to delete rows based on cell value ..
Cells to check are in Column A ..
If cell contains "-" .. Delete Row
I can't find a way to do this .. I open a workbook, copy all contents to another workbook, then delete entire rows and columns, but there are specific rows that has to be removed based on cell value.
Need Help Here.
UPDATE
Sample of Data I have
The easiest way to do this would be to use a filter.
You can either filter for any cells in column A that don't have a "-" and copy / paste, or (my more preferred method) filter for all cells that do have a "-" and then select all and delete - Once you remove the filter, you're left with what you need.
Hope this helps.
The screenshot was very helpful - the following code will do the job (assuming data is located in column A starting A1):
Sub RemoveRows()
Dim i As Long
i = 1
Do While i <= ThisWorkbook.ActiveSheet.Range("A1").CurrentRegion.Rows.Count
If InStr(1, ThisWorkbook.ActiveSheet.Cells(i, 1).Text, "-", vbTextCompare) > 0 Then
ThisWorkbook.ActiveSheet.Cells(i, 1).EntireRow.Delete
Else
i = i + 1
End If
Loop
End Sub
Sample file is shared: https://www.dropbox.com/s/2vhq6vw7ov7ssya/RemoweDashRows.xlsm
You could copy down a formula like the following in a new column...
=IF(ISNUMBER(FIND("-",A1)),1,0)
... then sort on that column, highlight all the rows where the value is 1 and delete them.
if you want to delete rows based on some specific cell value.
let suppose we have a file containing 10000 rows, and a fields having value of NULL.
and based on that null value want to delete all those rows and records.
here are some simple tip.
First open up Find Replace dialog, and on Replace tab, make all those cell containing NULL values with Blank.
then press F5 and select the Blank option, now right click on the active sheet, and select delete, then option for Entire row.
it will delete all those rows based on cell value of containing word NULL.
If you're file isn't too big you can always sort by the column that has the - and once they're all together just highlight and delete. Then re-sort back to what you want.
You can loop through each the cells in your range and use the InStr function to check if a cell contains a string, in your case; a hyphen.
Sub DeleteRowsWithHyphen()
Dim rng As Range
For Each rng In Range("A2:A10") 'Range of values to loop through
If InStr(1, rng.Value, "-") > 0 Then 'InStr returns an integer of the position, if above 0 - It contains the string
rng.Delete
End If
Next rng
End Sub
This is the autofilter macro you could base a function off of:
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$10").AutoFilter Field:=1, Criteria1:="=*-*", Operator:=xlAnd
Selection.AutoFilter
I use this autofilter function to delete matching rows:
Public Sub FindDelete(sCol As String, vSearch As Variant)
'Simple find and Delete
Dim lLastRow As Integer
Dim rng As Range
Dim rngDelete As Range
Range(sCol & 1).Select
[2:2].Insert
Range(sCol & 2) = "temp"
With ActiveSheet
.usedrange
lLastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
Set rng = Range(sCol & 2, Cells(lLastRow, sCol))
rng.AutoFilter Field:=1, Criteria1:=vSearch, Operator:=xlAnd
Set rngDelete = rng.SpecialCells(xlCellTypeVisible)
rng.AutoFilter
rngDelete.EntireRow.Delete
.usedrange
End With
End Sub
call it like:
call FindDelete "A", "=*-*"
It's saved me a lot of work. Good luck!