Excel macro to delete columns containing no positive integers - excel

I have an Excel spreadsheet which has several columns. Many of those columns have all zeroes in them. I'd like to be able to hide or delete all the columns that do not contain a value >= 1. Using Excel 2016 but I also use Excel 2013 and 2010 on different machines, just in case that makes a difference.
I'm new to this so imagine you're explaining it to a 2-year-old; An intelligent 2-year-old, but a toddler nonetheless. Thanks in advance for the help.

In the active range (range being the column you search for negatives or blanks in) eg Column A set your range, it will use a loop to go through each cell in the column range and hide anything blank or <= 0
Change the letter in ("A1:A5") to the column you need to search through.
Sub Hide_blank_rows()
Dim C As Range
For Each C In ActiveSheet.Range("A1:A5")
If C.Value = "" Or C.Value <= 0 Then
C.EntireRow.Hidden = True
Else
C.EntireRow.Hidden = False
End If
Next C
End Sub
Please note that you will have to post some code in future.

Related

How to hide columns which has all values less than a number in excel sheet?

I am newbie to the excel formulas.I have an excel sheet which has lets say 100 rows and 100 columns. columns have different values from 0 to 20. I want to hide the columns if all values of the column is less than a given number. Or in different way I want to display only those columns which any value is greater than a given number.
Throw the below code into a new module within your workbook. If you're not sure how to do that, then Google "How do I create a new module in my Excel VBA project" ...
Public Sub HideColumnsBasedOnCriteria()
Dim rngCells As Range, lngCol As Long, lngRow As Long, lngThreshold As Long
Dim bBelowThreshold As Boolean
Set rngCells = Selection
lngThreshold = InputBox("Enter a threshold amount ...", "Threshold Amount", 10)
Application.EnableEvents = False
Application.ScreenUpdating = False
With rngCells
For lngCol = 1 To .Columns.Count
bBelowThreshold = True
For lngRow = 1 To .Rows.Count
If .Cells(lngRow, lngCol) >= lngThreshold Then
bBelowThreshold = False
Exit For
End If
Next
If .Columns(lngCol).Hidden <> bBelowThreshold Then
.Columns(lngCol).Hidden = bBelowThreshold
End If
Next
End With
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
... then select your cells (not the columns, but the range of data like shown below) and then run the macro.
If you don't know how to run the macro then Google "How do I execute a macro in Excel". If you don't have the developer tab, Google "How do I make the developer tab visible in Excel".
The columns with the number less than what you provide will then be hidden.
This is without error checking and the like but it will get you going. If you need to take the threshold amount from a cell on the worksheet, that is an easy addition.
I hope it works for you.
What you're asking is impossible: the result of an Excel-formula is a number of a piece of text, not the command for hiding a cell or a range of cells.
There is one interesting this, and that's conditional formatting: it does not allow you to hide an entire column, but it can, as mentioned by the name, modify the formatting of your cell, based on some condition. As a formatting, you might choose the background colour of your cells.
The difficult part here seems: how will you check if the values of an entire column are smaller than one value? This can be checked using a simple idea: every value is smaller than one particular value if the maximum of those values is smaller than that particular value, and here you have your conditional formatting, based on a formula: just base it on the formula =MAX(F1:F100)<=5, apply this conditional formatting on every cell of column F and apply a background colour accordingly.

How to massively change the column reference in an excel sheet's formulas?

How to massively change the column reference in an excel sheet's formulas?
For example there is this column that has formulas that include REFERENCES of column G (G2, G23, G30, etc) and I want that massively change to column H (H2, H23, H33, etc).
Did not find any existing relevant q&a, maybe did not search enough, sorry and thanks in advance!
Click on column A and perform:
Insert > Column
Column G will become column H and any formulas in the worksheet that referred to cells in column G will now refer to cells in column H:
Before:
and after:
Thanks guys, it's ok I 've found it and it's simple. I just switched to formula view (Formulas menu), selected the column that give me the results and replaced (Ctrl+H) the g with h. And it's all done. Have a good evening.
Quickly put this together, the replacement function leaves a lot to be desired! (very lazy, you'd need to improve it probably)
R should be set to the column with the formula you want to change the reference in.
Just a proof of concept really. Make sure you save a copy of your workbook if you test it.
Sub Main()
Dim R As Range: Set R = ThisWorkbook.Worksheets("Sheet1").Range("$E$1:$E$2")
Dim Cell As Range
Dim CellPrecedent As Range
Dim CellPrecedents As Range
Dim Retry As Boolean: Retry = False
For Each Cell In R.Cells
On Error Resume Next
Set CellPrecedents = Cell.Precedents
On Error GoTo 0
If CellPrecedents Is Nothing Then
' skip
Else
For Each CellPrecedent In CellPrecedents
If CellPrecedent.Column = 7 Then ' 7 == G
Cell.Formula = Replace(Cell.Formula, "G", "H")
GoTo Breakout
End If
Next CellPrecedent
End If
Breakout:
Next Cell
If Retry Then Main
End Sub

Link selective rows from one sheet to the next

So I've searched everywhere... I have an office 2007 excel spreadsheet with two pages, one labeled "i" and the other "t." I need to display selective rows (those rows that have a value in column A--not all do) from "i" in "t." I also need the rows in the "t" page to be in numerical order. I figured out how to do it across all rows, but not how to selectively add rows with values only in column A. Further, when I add new rows to "i," "t" doesn't automatically update. Any advice on how to accomplish this would be of immense help!
I have access to office 2010. I don't know if that makes the coding easier?
Thank you!
Jason
You could try a user-defined function like this:
Function NonBlank(Selection As Range, Index As Integer) As Variant
Dim Count As Integer
Count = 0
For Each cell In Selection
If Len(cell.Value) > 0 Then Count = Count + 1
If Index = Count Then
NonBlank = cell.Value
Exit For
End If
Next
End Function
Then on Sheet t, you can put =NonBlank(i!A:A,ROW(A1)) in the first cell where you want the first non-blank value of Sheet i, and then copy the formulas down.

Verifying multiple text sub-strings in numerous cells

First time poster and new to VB but fairly fluent in Excel.
I have a question similar to what's asked in this post (excel: how can I identify rows containing text keywords taken from a list of keywords) however I need to read strings of text in both columns and compare them. If I find 2 or more matching strings I want the value from my second column to be used for my output in my 3rd column.
So in column A I have:
7-zip beta
Adobe Acrobat
ActivePerl
Apache Tomcat 7.9.0
Excel Chart Utilities
Microsoft Office Ultimate 2007
In column B I have:
7zip
Adobe Acrobat Reader
Apache Tomcat
Excel Chart
In column C I want the official software entry from column B if 2 or more text sub-string match.
Problems: my column A still contains the version number for the software I want to compare and is not necessary entered into my spreadsheet the same way as my official software list should be (column B). My column B contains only 600 rows while my column A contains close to 8000 rows. In addition, all duplicates in the respective columns have already been removed.
Any help would greatly be appreciated.
Try following VBA - quite general solution - it might help, eventually adapted to your exact needs:
Private Sub Test()
Dim a As Range, b As Range, cel As Range, match As Boolean
Set a = ActiveSheet.Range("A:A") ' can be set eventually upgraded to reflect only non-blank cells
Set b = ActiveSheet.Range("B:B") ' same as above and you can eliminage the Exit Sub statement
For Each cel In b.Cells
If cel.Value = "" Then Exit Sub ' this can be eliminated
match = False
match = InStr(1, UCase(cel.Offset(0, -1).Value), UCase(cel.Value), vbTextCompare) > 0
If match = True Then
cel.Offset(0, 1).Value = cel.Value
Else
cel.Offset(0, 1).Value = cel.Offset(0, -1).Value
End If
Next
End Sub

Hide columns using VBA in Excel 2010

Based on the value stored in a specific cell such as A1 of a worksheet I would like to hide a number of columns in the worksheet starting with column B.
Examples of what I am trying to do:
If the value of cell A1 = 10, then hide column B plus 10 columns after B
If the value of cell A2 = 11, then hide column B plus 11 columns after B
The difficulty is actually the way Excel (or least my Excel files) uses the alphabet (A, B, ...) for the name of the columns. I have done this on rows before using code like rows("2:" & range("A1").value) and set .hide = true
I wanted to add a comment to Glenn's answer above but don't have enough reputation. What I was going to add was that you don't need to activate a sheet or select the columns, you can simply go ahead and hide the columns:
Worksheets("TheSheet").Columns(2).Resize(, numColumnsToHide).EntireColumn.Hidden = True
You can reference columns by their index number as such: Columns(indexnumber) and you can use Resize() to set the number of columns you want to select like so:
Sub HideColumns()
Dim numColumnsToHide
numColumnsToHide = Cells(1, 1).Value
Columns(2).Resize(, numColumnsToHide).Select
Selection.EntireColumn.Hidden = True
End Sub
Obviously, this code doesn't have any validation of value in A1 so if someone runs HideColumns() without an integer in A1, bad things are going to happen. This also doesn't unhide any hidden columns.
Please to not complicate just do it
Sub ocultaColunas()
Range("D:E").EntireColumn.Hidden = True
End Sub

Resources