I am trying to create a function that will scan through a column and delete (or replace with nothing) any date of any form. 8/24 or 8/24/16. I have been unable to find an efficient way of doing this.
Check below code. Guess this will do
Sub Change_Dtfld2Blank()
cllRange = 100 ''SpecifyNo of rows in question
clNum = 1 '''Specify Colum Number
sSheetname = "Sheet2" ''Specify Sheet name
For i = 1 To cllRange
If IsDate(Sheets(sSheetname).Cells(i, clNum).Value) Then
Sheets(sSheetname).Cells(i, clNum) = ""
End If
Next i
End Sub
Related
I am new to VBA and I am stuck!
I am trying to write a macro that will search through column A of my worksheet, and then if it finds a "1" then it will hardcode that row.
I have the below which I found online and adapted, but it only works where column A contains hardcoded values of 1 or 0. In reality my column A is a formula the output of which is 1 or 0.
Is there a simple way to fix the below so that the code can react to a formula output of 1 rather than just a hardcoded input of 1?
Sub formulatovalue()
ActiveSheet.Select
Dim c As Range
For Each c In Columns(1).SpecialCells(xlConstants)
If c.Value = "1" Then
c.EntireRow.Value = c.EntireRow.Value
End If
Next c
End Sub
I want to hide all columns of dates which years aren't the last 2 years. For that (I don't know whether it is the correct approach or not) I've been doing some research given that I know nothing about Excel programming, and I've created this VBA script but it does not seem to work:
Function OcultarFechas(Range)
Rg1 = Range ' B2:AA2
Flag = "ok"
For Each c1 In Range(Rg1).Cells
If Year(c1) <> Year(Date) Then
Columns(c1.Column).EntireColumn.Hidden = True
Else
Flag = "notok"
End If
If Flag = "notok" Then Exit For
Next c1
End Function
It would be preferable that the scripts executes when I open the spreadsheet but right now with this code I think I need to call the function on a cell like: "=OcultarFechas(B2:AA2)".
PS. the dates are ordered that's why I exit the for loop when the current year is found, and from that column I need to keep them unhidden
You cannot hide a column using UDF.
Try something like this...
Sub HideColumns()
Dim c As Long, lc As Long
lc = Cells(2, Columns.Count).End(xlToLeft).Column
Columns.Hidden = False
For c = 2 To lc
If Year(Cells(2, c)) < Year(Date) Then
Columns(c).Hidden = True
End If
Next c
End Sub
The code above will find the last column used in Row2 and loop through column 2 to the last column found and check the year condition and hide the column accordingly.
To call this procedure automatically when you open the workbook, place the following code on ThisWorkbook Module.
Private Sub Workbook_Open()
Call HideColumns
End Sub
Disclaimer: I realize that this is easy to solve by just forumlating the TALLY column within the table, but this is more of an exercise in learning VBA in general.
I have table in my worksheet that has multiple columns of "Yes" or No", and I would like to find a way in VBA to tally the No's in a separate column. Here is an example of what I am trying to describe:
I figure it involves a loop somehow which would include Application.WorksheetFunction.CountIf, but short of doing a really messy Union range to reference I cannot figure how to code this effectively. Thank you in advance for any ideas posited.
Something like formula:
=COUNTIF(C:C,K1)+COUNTIF(F:F,K1)+COUNTIF(I:I,K1)
where K1 contains the value in C:C, F:F, and I:I that you want to count.
now if you need to count in ANY column you could just set the all the columns and do countif()
such as =COUNTIF(A:I,K1) but any occurrence within A:I will be counted if Yes/No.
also note this is case insensitive.
I'd do it like this (NOT with Countif):
Sub countCond()
Dim cond As String
Dim i As Long
Dim countCond As Long
Dim column As Variant
cond = "No" 'You can set this to 'Yes' as well
With Sheet1 'Or Worksheets("Sheet1") 'or whatever you have
For i = 1 To .Range("TALLY").Cells.Count
'I suggest naming it (at least the sum column)
'--Edit: Without the header, so just the values.
For Each column In .UsedRange.Columns
'Or maybe instead UsedRange, name the section
'that contains these columns
If column.Cells(1).Value Like "*COMP" Then 'Assuming this is true
If column.Cells(i + 1) = cond Then countCond = countCond + 1
End If
Next
.Range("TALLY").Cells(i, 1).Value = countCond
countCond = 0
Next i
End With
End Sub
This way, your code manually counts the cond variable in every row for every column that looks like *COMP.
Hope it helps.
Thank you all for your input, I believe I found an answer to my problem. Using this code I was able to get the results I wanted:
Sub Test()
Dim Fx() As Variant
Dim i As Integer
Dim tg_row As Integer
Fx() = Array("Table1", "[EXAComp]", "[EXBComp]", "[EXCComp]")
For Each cl In Range("Table1[TALLY]")
For i = 1 To 3
If Range(Fx(0) & Fx(i)).Cells(tg_row, 1).Value = "No" Then
cl.Value = cl.Value + 1
Else: cl.Value = cl.Value + 0
End If
Next
tg_row = tg_row + 1
Next cl
End Sub
I populate an array of numbers with some criteria and then what I am trying to get to is deleted all of the rows that are in this area.
Basically I go through a column and if in that specific row, the cell in this column matches a criteria, I add that row number into an array. After it is done going through all rows I want to delete all of the row numbers.
I'm having trouble figuring out how to delete all rows at once because obviously if I do it one at a time the row numbers change as the one prior or below gets deleted. Because of this I want to select all of the rows together and then just call the Delete command on all rows at once. ANy ideas?
Sub Tester()
Dim arr
arr = Array(3, 5, 7, 9)
ActiveSheet.Range("A" & Join(arr, ",A")).EntireRow.Delete
End Sub
Iterate backwards through your rows.
Something like:
Sub tester()
'setting ScreenUpdating false makes this go faster...
Application.ScreenUpdating = False
Dim i As Integer
'go through all rows starting at last row
For i = Range("A1:E5").Rows.Count To 1 Step -1
'check if you need to delete them (you will want to update this)
If Cells(i, 1).Value = "Delete this row!" Then
Rows(i).Delete
End If
Next i
Application.ScreenUpdating = True
End Sub
Here's a simple one:
If Range("B1") <> "" Then ' Range that bears the array of cell.addresses....
ar = Array(Range(Range("B1").Cells))
For Each a In ar
a.EntireRow.ClearContents
Next
Range("B1").ClearContents
End If
I have a little problem, I occasionally bump into this kind of problem, but I haven’t found a fast solution so far.
So, imagine we have an Excel worksheet and let's suppose that we have a couple of numbers in column ’A’ with some empty cells in it. Altogether (just to make it simple) we have the first 10 cells in column 'A' to observe. For example:
3
(empty cell)
(empty cell)
6
(empty cell)
4
(empty cell)
23
(empty cell)
2
Now in the next step I would like to collect these numbers into another column (for example, column ’B’) using VBA. Obviously I just want to collect those cells which contain a number and I want to ignore the empty cells. So I would like to get a column something like this:
3
6
4
23
2
I have already written the following code, but I’m stuck at this point.
Sub collect()
For i = 1 To 10
if cells(i,1)<>"" then...
Next i
End Sub
Is there an easy way to solve this problem?
Probably the quickest and easiest way is to use Excel's Advanced Filter - the only amendment you'll need to make is it add a field name and criteria. You can even list unique items only:
The VBA equivalent is
Sub test()
With Sheet1
.Range("B1:B8").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range( _
"D1:D2"), CopyToRange:=.Range("F1"), Unique:=False
End With
End Sub
You should be able to use the method in the post int the comments, but you could also use SpecialCells like Range("A:A").SpecialCells(xlCellTypeConstants,xlNumbers).Copy to get all of the filled cells.
Edit: needed constants not formulas.
This will work for any number of rows that you select. It will always output in the next column at the start of your selection e.g. if data starts in B10 it will ooutput in C10
Sub RemoveBlanks()
Dim cl As Range, cnt As Long
cnt = 0
For Each cl In Selection
If Not cl = vbNullString Then
Cells(Selection.Cells(1, 1).Row, Selection.Cells(1, 1).Column).Offset(cnt, 1) = cl
cnt = cnt + 1
End If
Next cl
End Sub
If you wish to loop manually and don't mind specifying the maximum row limit;
Dim i As long, values As long
For i = 1 To 10
If cells(i, 1).Value <> "" Then
values = (values + 1)
' // Adjacent column target
cells(values, 2).value = cells(i, 1).value
End If
Next i