I'm trying to clarify logs where there are comments in some of the entries and in some there aren't. So what i would like to be able to do is to delete entire rows where there are comments in a column such as NULL and so that it wouldn't leave a blank row behind it because it messes up my other conditional formatting rules.
I'm guessing this kind of automation requires VBA but since i'm not a full time coder, I have no idea how to even begin solving this.
Thank you for all responses in advance!
This isn't a very sophisticated answer, but neither is your question so hopefully it'll get you going. This assumes your evaluating is in column A. You can obviously change that. If you've got tens of thousands of rows, you might want to turn off some functions like screen updating, but that's a different type of question.
Sub ClearThatOut()
Dim rCell As Range
restart:
For Each rCell In Intersect(Columns("A"), ActiveSheet.UsedRange).Cells
If UCase(rCell.Value2) = "NULL" Then
rCell.EntireRow.Delete
GoTo restart
End If
'Example of secondary text, you could put as many of these as needed.
If UCase(rCell.Value2) = "OH NO!" Then
rCell.EntireRow.Delete
GoTo restart
End If
Next rCell
End Sub
Related
I need to use my active column in my range statements. How to write down this:
Range((Activecolumn)2)
So I need a column value to be dynamic because I am moving left and right by pressing a userform button, but the row number always stays the same. At first I am working with column "C" and select it with
Columns("C").Select
and I navigate the selected column with
Private Sub Next_Format_Button_Click()
ActiveCell.Offset(, 1).EntireColumn.Select
End Sub
So when I navigate to the next column I need all my other range statements to go along with it. So basically the column letter in my range statements should be something like "current" or "active" column.
Could someone, please, help me?
Range(ActiveCell.address).entireColumn.copy
Is what you are looking for I think?
I'd also recommend avoiding ".Select" unless absolutely necessary. It is adding unnecessary lines to your code and leaves the program at greater risk of inadvertent user corruption by selecting another range via mouse before the copy (or whatever) operation completes. Better to go straight to whatever operation you intend to do, or by assigning the range to a variable.
edit: Ah, sorry, I see now your only looking row 2, then its Cells(2, ActiveCell.Column) as pointed out above.
If you want to get the Number of the column where you selected something:
ActiveCell.Column
If you need the letter of the column your selection is in:
Split(ActiveCell.Address, "$")(1)
I want to loop through cells $C$29 to $G$29 and if the cells contain an empty space I want to clear the contents of the 8 cells below. For example if $G$29 is empty, I want to clear the contents in $G$30:$G$37. I have attached code below. But I believe there is something wrong with the .ClearContents line, something to do with the way I am attempting to reference relatively. Thank you!
Best,
M
Sub Hide_Financing()
Dim r As Range
For Each r In Sheet2.Range("$C$29:$G$29")
If r.Value = "" Then
Sheet2.Range(r.Address & ":" & r.Offset(rowOffset:=8).Address).ClearContents
End If
Next r
End Sub
It works fine for me. In your code, nowhere you've set the value of "sheet2". Have you set it correctly? The rest of your code is right.
Another question, are you sure that you have cleared at least one cell in the range "$C$29:$G$29" ?
I've written this to provide more of an answer, as the current one is more of a comment and doesn't really provide a direct solution (it's ambiguous and implied that the Sheet2 isn't declared anywhere which is your problem (which is not necessarily correct as Sheet2 is the code name for Worksheets("Sheet2") by default, so you should avoid variables with these types of name anyway).
When my code doesn't do anything you should take basic debugging steps (that's just one of many places on the web explaining how to debug - use your search engine to your advantage!).
From looking at your code, there is no syntax problems and it all seems like it should work, so, read it out loud to yourself.
You determined you were referencing cells on Sheet2 instead of Sheet1 which is something that really only you can establish as you are the only one that knows where you want to manipulate your data.
To summarize the key points here:
Use the Rubber Duck debugging method
Debug your code.
You'd be surprised how many problems you can resolve by reading your code out loud to someone or something.
To debug your code, you can do things like use the Debug.Print statement.
I'd add it to your code like so:
For Each r In Sheet2.Range("$C$29:$G$29")
If r.Value = "" Then
Debug.Print r.Address
Debug.Print r.Offset(rowOffset:=8).Address
End If
Next r
Now for your applciation, you'd expect something like this in the immediate window:
$C$29
$C$37
But depending on your values on Sheet2 if any, you'd likely see this:
That is blank on purpose, because you were referencing the wrong sheet - Or you might have seen some other unexpected cell references of course depending on your sheet2 values.
I'd assume, you would have realised pretty quickly that you should be checking Sheet1 instead of Sheet2 by doing both of these - of course it's not a guaranteed solution every time - some things just don't make sense and that's what communities like SO are for! - but it works more than you'd think it would for these kinds of things.
I'm trying to find a way to automate user made filters that are generated for day to day operations. Having only started really learning VBA 24 hours ago, i used a combination of Macro recording as well as a mix of different solutions I found online. The code below is the closest I came to what I needed, but there are some oddities I don't quite understand the circumstances of why they're happening much less any way of fixing them.
I've tried offsetting the rows along with adding or removing one from the bottom with minimal success as it always states a section of it is invalid (i'm guessing because it needs to be defined somewhere but not entirely sure in what format).
What seemed to work the best was a combination of Error handling and specialCells where the script should ignore the value appending portion if nothing is populated in the 2nd row. If there is something populated there, then it would proceed to append a value on the last column but only for empty cells.
Columns("A:H").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$H$800000").AutoFilter Field:=2, Criteria1:="=*",
_Operator:=xlAnd
ActiveSheet.Range("$A$1:$H$800000").AutoFilter Field:=8,
Criteria1:="=*In*", _Operator:=xlAnd
This is supposed to filter out what I need per tab on the excel. I'm looking
for any documents that have a value in the 2nd column (B) and any documents
that contain "In" on the 8th column (H). The reports generated can vary wildly
in length so i designated 800k as a good threshold.
On Error GoTo NoBlanks01
If Range("$A$2:$H$2").SpecialCells(xlCellTypeVisible).Count > 0 Then
Columns("I:I").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "InsertValueHere"
Skip01:
On Error Resume NextEnd If
I'm not 100% sure if having them this way is redundant but my thinking here
was that if there's anything in the second row, proceed. If not, an error will
be produced and will follow to the bottom of the script then go to skip01 and
proceed from there. Essentially bypassing the value appending where it'll
select all empty cells in row I and append "Insert Value Here". This portion
generally works, but there are some tabs in the excel when there will be items
on the second row, yet the script won't recognize them and proceed through the
error handling. That's the part that I don't understand.
NoBlanks01:
Resume Skip01
'I have 11 "NoBlanks", "No Skips" and "Resumes" in the same sub. All according
to different tabs. Again, not sure if that matters but figured i'd state that
in case there's some order of operations I missed while researching this.
I expect that the script should filter according to the specifications given, then run through a query where it checks to see if the second row contains any items.
If it does: then it should proceed to select the last column in that tab, select only empty cells within that tab, then code them a value.
If it doesn't: then it should Skip any value appending and move straight to it's relevant "NoBlanks" Followed by it's relevant "Skip".
As it stands, it does complete the logic on some tabs but not on others. I have zero clue why when the second row is clearly populated. I realize that having some of these reports would come in handy so if needed, i can provide it.
Try something like this:
Dim sht As Worksheet, rng As Range, rngVis As Range
Set sht = ActiveSheet
Set rng = sht.Range("a1").CurrentRegion '<< the range with data and headers
If Application.CountA(rng) = 0 Then Exit Sub '<< exit if have no data...
rng.AutoFilter
rng.AutoFilter field:=2, Criteria1:="=*"
rng.AutoFilter field:=8, Criteria1:="=*In*"
'Next line should not throw an error even if all data
' rows are filtered, since there's always the header row visible
Set rngVis = rng.Columns("I").SpecialCells(xlCellTypeVisible)
If rngVis.Count > 1 Then '<< ignore if only the header row...
rngVis.SpecialCells(xlCellTypeBlanks).Value = "InsertValueHere"
End If
I've got a somewhat specific task I'm trying to accomplish in Excel that appears to be beyond my depth. I've got a dataset with two five-word lists per each row/observation, like this example:
What I'm hoping to accomplish is to highlight all of the words that are in both 5-word lists within a single row. The amount of overlap between these two five-words lists varies from one row to the next. And I'm not concerned with identifying any duplicate entries across different rows. That is, it'd be great if it's possible to create a macro that would give this:
I've searched quite a bit on this site and using google to figure out how to create a macro to do this. I've found a number of similar macros, but every one that I've found is geared towards identifying all of the duplicates between two entire sheets, or two entire columns, or something similar, which doesn't quite match what I'm trying to do. Based on the macros I have been able to find, I get the sense that doing what I want should be possible, but I don't know enough about visual basic to edit the macros I've found to suit my needs.
If it's possible to program a macro to do this, it'd save me quite a bit of time, since otherwise I'm looking at doing this duplicate-identification manually for two current datasets that I have (each with 150-200 observations), plus I plan on collecting data in the future that would require this same procedure. Beyond that, any macro that's capable of helping me here may be able to help others with similar needs.
Thanks in advance for any help you're able to provide!
Try conditional formatting.
Select "table 2" (e.g. I2:Mn where n is the last row) with I2 (upper left corner) being the active cell. Then use this formula:
=OR(I2=$C2:$G2)
I think using Conditional Formatting is maybe a simpler solution but one way to do this using VBA is as follows:
Sub HighlightDuplicates()
Dim masterList(), highlightList(), rw As Long, col As Long
masterList = Range("C1:G150")
highlightList = Range("I1:M150")
For rw = 1 To UBound(masterList, 1)
For col = 1 To UBound(masterList, 2)
If highlightList(rw, col) = masterList(rw, col) Then
Cells(rw, col).Offset(0, 8).Interior.Color = vbRed
End If
Next col
Next rw
End Sub
Here we read both lists in as arrays and then iterate over them to check for matches.
The Offset(0, 8) is a bit of a magic number (yikes!) which gets the correct cell highlighted based on your layout.
I tried searching for this, but I kept coming across questions of how to make a cell show as blank instead of false. I want to clear the formula out.
I am working from an access database (that i can't do much to), that contains data on sessions on our computers. What the database will give me currently is basically "Section 1, 9-10", "Section 1, 10-11", etc. There are a total of 5 sections like this for 11 hours. In the past, I've had to copy from those queries into excel sheets. What I've gotten to at this point is I can create a query for each section that just gives me the information I need, start time and duration, which I can paste into a workbook I made.
What this workbook does is take the data from those columns and moves it to page 2 sorted by hour with if statements. the problem with that is I need those if statements to go all the way across, and far enough down to be sure to include the highest number of sessions we've had, plus a comfortable buffer, so it's 7000 rows down.
After that sorting is done, I can take all the data from that second sheet and paste it into monthly records. the problem is, with there being formulas in each of those cells, it adds approximately 7-10 Mb to the file size.
Is there someway so that cells that evaluate to false have their formula removed? Or, probably more likely, a better way to go about this without much additional software?
I'm guessing you want to use an if formula and if it evaluates to False then display nothing. You can do this by setting the value for the false result to "".
=if(expression,value if true, "")
The alternative is to use a macro to loop through each cell and clear it if the value is false.
Sub falseToClear()
' Change to the range you need to parse
Set Rng = Sheets(1).UsedRange
For Each cell In Rng
If cell.Value = False Then cell.ClearContents
Next cell
End Sub