Using Search and Replace with VBA/Excel - vba

Got an annoying Excel/VBA issue that I can't seem to get around - would appreciate any help.
I have a formula in a spreadsheet that says something along the lines of if the cell to its left =1, then the cell itself =on, and if not, ="off".
Is it possible to write some VBA search and replace code that turns "off" to "totally_off" when run?
At the moment it just seems to be editing the formula itself, while I really want it to delete everything in that cell and just replace it with the phrase "totally_off"
Thanks for your help.

The following code should work. Just replace the range B1:B10 with the range that the formulas you want to change are in. The procedure tests the value of the cell immediately to the left of the cells in the formula. If the value is zero, it replaces the formula with the "totally off".
Sub totally_off()
Dim rng As Range
Dim cell As Variant
Set rng = Range("B1:B10")
For Each cell In rng
If cell.Offset(0, -1).Value = 0 Then
cell.Value = "totally off"
End If
Next cell
End Sub
Here's a non-VBA, non-search and replace alternative.
Set up a filter on the formula column with Sort and Filter / Filter on the Home ribbon.
Select "off" from the drop-down menu on the column.
Type "totally off" in the first cell of the filtered column and copy it down to the bottom of the column.
Then remove the filter. The formulas evaluating to "on" will remain intact.

No need for VBA - simply use an AutoFilter:
Select the column with the on/off formula
Apply an AutoFilter (Ctrl-Shift-L
Filter for off
Again, select all values
Simply type in your replacement value totally_offinto the formula bar - but press Ctrl-Enter
Done!

Related

Vlookup vba automatisation

I'm a beginner in VBA programmation, I want to create a button to help me search the price of a product from column A of sheet1 and to search for the price of that product from sheet2, column D of the same workbook.
The vlookup formula I use is:
=VLOOKUP(A2;sheet2!A2:G712;4)
My issue is that I have more than 100000 products and I want to use a button to simplify the process.
Change your top row formula to =VLOOKUP(A2;sheet2!$A$2:$G$712;4), select the cell you enetered the formula into, and then drag the formula down by using the little square in the bottom right hand corner of the cell you entered the formula into.
If you want to do it entirely in VBA, you can autofill using
Dim source As Range("A1")
Dim destination As Range("A1:A10")
source.AutoFill Destination:=destination
This will autofill from A1 to A10, as an example. You can also specify the autofill type with Type:
Dim source As Range("A1")
Dim destination As Range("A1:A10")
source.AutoFill Destination:=destination Type:=xlFillLinearTrend
The default type is xlFillDefault, which tries to find a pattern automatically and use the according fill type. Other types can be looked up here.

Get relative cell value using R1C1 notation

I'm beginner in excel VBA. Sorry it should be something very basic. My question is I need to get the value of a cell relative to the active or selected cell. So what I tried is as follows
ActiveSheet.Range("P2").Select
Dim s As String
s = "=RC[-11]"
What my plan was to get the value at cell, which is 11 columns left to the active cell. By s="=RC[-11]" I know it will print as it is and its not gonna do any operation. But I tried to express that was my final end value. So how can I achieve that.
Something like:
myRange.FormulaR1C1 = "=RC[-11]"
Where myRange is a variable covering the range of cells you want to assign the formula to in column e.g. from start row to end row e.g.
Dim myRange As Range
Set myRange = ActiveSheet.Range("P2:P10")
Just pay attention to the square brackets which determine if using absolute or relative cell references.

Macro to find and delete column based on cell value

I have two sheets in an Excel workbook. I would like a VBA code to search for the content of cell J19 in Sheet2 and delete the column with the matching cell in Sheet1. I have been searching all around Google to look for some VBA codes, but I haven't found anything yet that works.
Does anybody here know how to do this? I have zero experience in VBA.
You could try this code to perform such a task.
Sub FindMatchAndThenDeleteColumn()
FindContent = Worksheets("Sheet2").Range("J19")
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
If Cell.Value = FindContent Then Columns(Cell.Column).Delete
Next
End Sub
Put the code above in your workbook's module. Note that FindContent and Cell are randomly chosen here, you can use any names. I use
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
to loop through every cell that is in use in Sheet1. It will check everything in the range from cell A1 to the last cell with data (the bottom right-most cell). If the content of cell J19 is a text, you can declare the variable FindContent as a String type, i.e. Dim FindContent As String. If it's a number, you can declare it as a Long type or a Single type or any number type that fits the content. Here I don't declare it which means it defaulting to a Variant type. And since you're a beginner in VBA, you may learn it from Excel Easy. Hope this helps.

Excel Macro to Change Active Cell

I have created a form in Excel. Based on how the user completes one cell, changes which cells they fill out next (for example: When filling out A10 if they answer "X" they will move to B1, if they answer "Y" they will move to B3.)
In order to guide the user through the form, I created a complex set of conditional formatting rules which will "highlight"(background fill) the next cell they need to fill out. Once they complete the cell the formatting on that cell goes away and switches to the next cell.
I have the conditional formatting working exactly how I want. My question is: Is there a way to have the active cell follow this same path. Either by setting up the same formula rules that guide the conditional formatting or is there a way to have a macro auto set the active cell to the "highlighted" cell from the conditional formatting?
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Const NEUTRAL = 16777215
Dim r As Range
For Each r In Cells.SpecialCells(xlCellTypeAllFormatConditions)
If r.DisplayFormat.Interior.Color <> NEUTRAL Then
r.Select
Exit For
End If
Next
End Sub
Note: you can edit NEUTRAL at the top if non-highlighted cells have a different color than pure white.
Note: this assumes that the cell you want the selection to jump to is the only cell on the sheet that is highlighted with conditional formatting.

Excel VBA code (assigned to a button) to hide/unhide rows based on cell values across multiple sheets

this is my first post here and additionally I also have completely no knowledge on VBA whatsoever... so please excuse my ignorance ;-)
I'm working on a price list which has a quantity column. The same files has multiple worksheets with multiple currencies. What I need to achieve is to create two buttons on each sheet to hide / unhide all rows where the quantity cell equals zero.
So for example you want to select certain items from the list, so you enter the quantity into appropriate cells (quantity column) and press the button to hide all other rows for which the quantity equals zero.
Now, I found the code for this somewhere already, but it only works on a first sheet and when I copy the sheet (to create another currency) with the buttons and press the button it will still apply the changes (hide / unhide rows) to the first sheet. This code is below:
Public Sub HideRows()
Dim cell As Range
For Each cell In Range("BOQ")
cell.EntireRow.Hidden = (cell.Value = 0 And cell.Value <> "")
Next cell
End Sub
and to unhide:
Public Sub UnhideRows()
Dim cell As Range
For Each cell In Range("BOQ")
If (cell.Value = 0 And cell.Value <> "") Then cell.EntireRow.Hidden = False
Next cell
End Sub
I would be extremely grateful if anyone could propose a proper script to do that separately on multiple sheets. Also to avoid the issue when after a print preview the script runs like a 100 times slower.
Thanks in advance.
Range("BOQ") refers to a range on the first sheet.
So no matter which sheet is selected, the macro will affect that range on sheet 1.
To make the code flexible to the sheet you're on, consider changing it to something like:
Activesheet.Range("A2:A10")