Data validation remove blank cell that contains formula - excel

I have a list for data validation that is dynamically pulled from another workbook. Then I want to use data validation on this list -- but even a cell with an empty value cell contains a formula, so the cell is not empty.
In other words, I want to ignore each blank cell, even if it contains a formula if the formula returns an empty value. For example: " ='[Master.xlsm]2019'!$A1" (for the first cell).
I use this:
=OFFSET(REFERANS!$A$1;0;0;COUNTIF(REFERANS!$A:$A;"<>"&"");1)
cell a1:a100 ='[Master.xlsm]2019'!$A1 ...

I choose :
Sub WorkShts()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
With sh.Range("xxx:yyy")
.Value = .Value
End With
Next sh
End Sub
then all the problems have gone

Related

find blank cells in columns and replace them with 0, repeat for next row if next row has data

I am using excel userform to input the data. I want for Columns C2:F2, if the user has not inputted any number in it, excel should convert these blanks into 0. And I want it to repeat for the next row when the data for next row is inputted. I tried some simple coding but it didn't work.
Public Sub BlankCells()
Dim rng As Range
rng = Range("C2:C1000", "D2:D1000", "E2:E1000", "F2:F1000")
For Each cell In rng
If cell = "" Then cell.Value = "0"
Next cell
Find
End Sub
It works if use coding for single row only i.e. "C2:F2", but i want it repeat for the next rows as well when the next row gets the data.
When you create a variable for sheet or range, you need to use the Set keyword like this:
Set rng = Range("C2:F1000")
If you'd like to refer to multiple ranges, you can add all the ranges as one comma separated string

Filter on Cell Value Inside a Table

I have a table from A2 to A7. The cell A1 doesn't belong to that table.
As long as the Cell A1 is empty the below macro works properly, but when I enter any value in cell A1 the macro stops working. The VBA debugger says: The AutoFilter Method of the Rang cannot be run (ErrorCode 1004).
Public Sub FilterOnCellValue()
Dim nField As Long
With ActiveCell
nField = .Column - .CurrentRegion.Cells(1).Column + 1
.CurrentRegion.AutoFilter Field:=nField, Criteria1:=.Value
End With
End Sub
The Question: How can I limit my macro to filter only inside the table?
Update:
I want to use this macro in all tables and all files and not in a certain table. Is there any solution?
just pick th one your heart prefers
Give a name to your table and use the table.range.filter, instead of CurrentRegion.
ActiveSheet.ListObjects("mytable").Range.AutoFilter
insert a blank and hidden row between A1 and your table

vba excel looping thru an array

I'm a rookie at vba.
I created a solution that works on a micro basis, but can't get the same code to run when I add additional parameters to that code.
I created a multiple choice quiz with answer cells which test against the answers on a separate (hidden) sheet. An adjacent cell shows Yes or No response. That functionality which I researched on the web works well. I'm trying to clear.contents for all the cells so that the user can complete the quiz, clear all responses to give it another go.
I have 395 questions which I created code to clear "Range("B2").ClearContents"
My subroutine has 395 such commands and takes 45-60 seconds. I'm looking for a more efficient solution. I don't want to clear the entire column only the 395 non-adjacent cells which have user input.
Sub Test_Clear()
Range("B2,B5,B7,B9,B11").ClearContents
End Sub
My list of cells is on Sheet2 D1:D395 Each cell in D1:D395 lists a cell on Sheet1 which I want to clear (i.e. B6, B11, B17, B22, B35 etc.) How can I reference those cells on Sheet2 and perform the ClearContents?
While you could just hard code your list in VBA instead of your Sheet2 range, here is how you could reference that list of cells in sheet2 and clear the contents all at once:
Sub clearall()
Dim rngCell, listCells As String
For Each rngCell In Sheet2.Range("D1:D395").Cells
If listCell <> "" Then listCell = listCell & "," & rngCell.Value Else listCell = rngCell.Value
Next
Sheet2.Range(listCell).ClearContents
End Sub
If you just want to clear the cells in column A which have something in them you could use this
Sheet1.Range("A:A").SpecialCells(xlCellTypeConstants).ClearContents
'or
Sheet1.Range("A1:A395").SpecialCells(xlCellTypeConstants).ClearContents
The VBA Join function can be used to join values into string, and the Excel Transpose function is needed to "flip" the column values 2D array to a 1D row values array:
stringAdress = Join([Transpose(Sheet2!D1:D395)], ",")
ThisWorkbook.Worksheets("Sheet1").Range(stringAdress).ClearContents
If any of the cells in the Sheet2!D1:D395 range are blank, the above will result in error.
In Excel 2016, the TextJoin function can be used to ignore empty cells (not tested):
stringAdress = [TextJoin(",", True, Sheet2!D1:D395)]
ThisWorkbook.Worksheets("Sheet1").Range(stringAdress).ClearContents

VBA to Sum a Column from a fixed cell up until the first blank cell

I have a column of numeric data, where I need to sum the values from a specified cell, down to the last value before the first blank cell in the Column. I have used a Range function previously to complete this, but on this occasion the number of rows before the blank cell is unknown and cannot be defined in a range. the simple explanation is I need the result in cell A1 to be
=sum (A6:AX)
where X is one before the first blank cell.
I will then write this into my VBA loop to complete it for nth columns over nth sheets.
Use the following sub:
Sub SumTillBlankCell()
Dim BeforeFirstBlankCell
BeforeFirstBlankCell = Range("A6").End(xlDown).Row
Range("A1").Value = Application.Sum(Range("A6:A" & BeforeFirstBlankCell))
End Sub

Creating a macro to delete rows in Excel using VB

I need a code that will delete the enitre row when a specific name is typed into column A.
So, for each row that has "Surgery" in column A, it needs to be deleted. Thanks.
This should work. All you need to do is change the value of areaToSearch to fit your workbook. Also watch the case on the keyword, "Surgery" and "surgery" are not the same! I tested this and it worked on a sheet I made up.
Option Explicit
Sub DeleteSurgery()
Dim keyWord As String
Dim cell As Range, areaToSearch As Range
keyWord = "Surgery"
Set areaToSearch = Sheet1.Range("A1:A10")
For Each cell In areaToSearch
If cell.Value = keyWord Then
cell.EntireRow.Delete
End If
Next cell
End Sub

Resources