In a spreadsheet, I am trying to somehow make it so any new numerical value that gets inputted into a cell in column D to multiply by 85%. Basically, if I go to any cell 2-100,000 in column D and input a numerical value, I want it to automatically show 85% of it.
If I input '100' into D5, I want it to show '85'.
If I input '200' into D317, I want it to show '170'.
Is this possible to do in any way?
Manually multiplying by another cell or by 0.85 can't be used.
Thank you so much!
Worksheet Change: Modify Any Input in a Column
Sheet Module e.g. Sheet1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim irg As Range
With Range("D2") ' from 'D2' to the bottom-most row:
Set irg = Intersect(.Resize(Rows.Count - .Row + 1), Target)
End With
If irg Is Nothing Then Exit Sub
Application.EnableEvents = False
Dim iCell As Range
For Each iCell In irg.Cells
If IsNumeric(iCell.Value) Then
iCell.Value = 0.85 * iCell.Value
End If
Next iCell
Application.EnableEvents = True
End Sub
Related
Goodnight,
I have this code that identifies the ActiveCell and sends the value of that cell to "I6". With the change of the values that appear in "I6", the corresponding photographs of the students will change.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("I6").Value = ActiveCell.Value
End Sub
With this code, I get the value of the cell to appear in "I6" whenever I click on a cell in the table. As long as i click on the "I" column where the student numbers are, everything is fine.
The problem appears when I click on any other table cell ("I14:X43") outside this "I" column.
I needed the code to always identify column "I" (I4:I43) of the active cell row.
Thus, whenever you are entering a record in any cell of the table ("I14:X43"), the code will identify the column "I" of that line and its value (student number). Identifying the value found in column "I" of the active cell row, it will appear in "I6", as it is in this formula and was changing the students' photographs.
At this moment, when I write a value inside the table, it will be this value that will appear in "I6" carrying a photograph that corresponds to the value that was registered/written and not to the number of the student of the line where I am.
However, a friend helped me with this code which works for what I intended but only up to column "Z".
Sub_ Selectionchange
Dim x As String
Dim and As String
x = ActiveCell.Address(0, 0)
y = WorksheetFunction.Replace(x, 1, 1, "=I")
Range("I6").Value = y
End Sub
Clicking after that "Z" column, the code stops working and 0 appears in the "I6" column.
Can anyone help, please?
Try to getting a vba code to send the column "I" value of the ActiveCell row to cell "I6"
A Worksheet Selection Change: Trigger Worksheet Change
Option Explicit
Private Const ID_CELL As String = "I6"
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Me.Range(ID_CELL), Target) Is Nothing Then Exit Sub
' Your code for showing the student's image.
MsgBox "Like showing student's image for ID number " _
& CStr(Me.Range(ID_CELL).Value) & ".", vbInformation
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const ID_RANGE As String = "I14:I43"
Dim srg As Range: Set srg = Me.Range(ID_RANGE)
Dim rrg As Range: Set rrg = srg.EntireRow ' srg.EntireRow.Columns("I:X")
Dim irg As Range: Set irg = Intersect(rrg, Target)
If irg Is Nothing Then Exit Sub
Dim sCell As Range: Set sCell = Intersect(srg, irg.EntireRow).Cells(1)
Dim dCell As Range: Set dCell = Me.Range(ID_CELL)
' If not equal, write, triggering the Worksheet Change event.
If sCell.Value <> dCell.Value Then dCell.Value = sCell.Value
End Sub
Use the ActiveCell row and always use 9 => "I" column with Cells to get the value in column I in the same row of ActiveCell
Sub_ Selectionchange
Dim x As String
Dim and As String
Dim row as long
row = ActiveCell.row
If row >12 and row <44 Then
Range("I6").Value = cells(row,9).value
End If
End Sub
How do I automatically format a cell as I enter a value?
I divided the numbers into 3 categories: percentages, small numbers (-1000 - 1000), and large numbers.
I want percentages to be displayed with 2 decimals and the % sign.
Small numbers with 2 decimals as well.
And large numbers rounded to nearest integer, with thousands separators.
I want the code to reformat the cell if the cell value changes. For instance, if I change a cell with value "50,000", to 60%, then, it should be displayed as "60.00%".
Code I have so far applies formatting on existing cell values.
Sub myNumberFormat()
Dim cel As Range
Dim selectedRange As Range
Set selectedRange = Selection
For Each cel In selectedRange.Cells
If Not CStr(cel.Text) Like "*%*" Then
If Not IsEmpty(cel) Then
If cel.Value < 1000 And cel.Value > -1000 Then
cel.NumberFormat = "_(#,##0.00_);_(-#,##0.00_);_(""-""??_)"
Else
cel.NumberFormat = "_(#,##0_);_((#,##0);_(""-""??_)"
End If
End If
Else
cel.NumberFormat = "0.00%"
End If
Next cel
End Sub
A Worksheet Change: Apply Cell Formatting
This is a simple example for a single column, in this particular case, the range from A2 to the bottom-most cell in column A. But you can choose any reasonable cell address. In whatever cell you manually change (enter, copy/paste, or write using VBA) its value, the cell will be formatted according to your 'rearranged' procedure ApplyMySpecialNumberFormat.
Note that this works automatically, just copy the codes to the appropriate modules and start entering data into the cells of the range to see it work.
Sheet Module e.g. Sheet1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Define constants.
Const FirstCellAddress As String = "A2" ' adjust to any reasonable cell
' Reference the source column range e.g. 'A2:ALastWorksheetRow'.
Dim srg As Range '
With Me.Range(FirstCellAddress)
Set srg = .Resize(Me.Rows.Count - .Row + 1)
End With
' Reference the cells of the source range that have changed.
Dim irg As Range: Set irg = Intersect(srg, Target)
If irg Is Nothing Then Exit Sub ' no source range cells changed; exit
' At least one cell was changed:
ApplyMySpecialNumberFormat irg
End Sub
Standard Module e.g. Module1
Option Explicit
Sub ApplyMySpecialNumberFormat(ByVal rg As Range)
Dim cel As Range
For Each cel In rg.Cells
If CStr(cel.Text) Like "*%*" Then
cel.NumberFormat = "0.00%"
Else
If VarType(cel) = vbDouble Then ' is a number
If cel.Value < 1000 And cel.Value > -1000 Then
cel.NumberFormat = "_(#,##0.00_);_(-#,##0.00_);_(""-""??_)"
Else
cel.NumberFormat = "_(#,##0_);_((#,##0);_(""-""??_)"
End If
End If
End If
Next cel
End Sub
I'm working with some data in excel spanning B9:AJ1108 - so multiple rows and columns. I am looking to hide all rows except where the value in column B matches the number in cell C5.
I've tried multiple and can only just about get everything to hide but the unhiding is the issue. I understand how to hide all and how to unhide all. What I need help with is how to hide all and then unhide if something matches the value in C5.
Code so far:
Private Sub CommandButton2_Click()
Worksheets("Employee information").Range("B9:B1108").Rows.Hidden = False
End Sub
Private Sub CommandButton1_Click()
Worksheets("Employee information").Range("B9:B1108").Rows.Hidden = True
'Need to put in the argument to search for C5 value
End Sub
I would also like this to be button controlled but I don't know if that is a case of creating a module or just code within the sheet?
For unhiding the rows you can use "Rows.EntireRow.Hidden = False"
If you want to use a button for the macro to get executed, create a button and excel will ask you which macro you want to get when you click the button.
value= Worksheets("Employee information").cells(5,3).value
That will give you the value of the cell C5, now you need to go through the rows and look for this value.
Hide Rows Not Containing Criteria in Column
Private Sub CommandButton1_Click()
With Worksheets("Employee information")
' Define Criteria (restrict to numbers).
Dim Criteria As Variant
Criteria = .Range("C5").Value
If Not IsNumeric(Criteria) Then
Exit Sub
End If
' Define Criteria Range.
Dim rng As Range
Set rng = .Range("B9:B1108")
End With
' Declare additional variables.
Dim hRng As Range ' Hide Range
Dim cel As Range ' Current Cell (in Source Range)
Dim CurVal As Variant ' Current Value (of Current Cell in Source Range)
' Create a union (Hide Range) of all the cell ranges
' that do not contain Criteria.
For Each cel In rng.Cells
' Evaluate Current Value.
CurVal = cel.Value
If IsNumeric(CurVal) Then
If CurVal = Criteria Then
GoTo NextCell ' Match found: do nothing.
End If
End If
' Match not found: add Current Cell to Hide Range.
If Not hRng Is Nothing Then
Set hRng = Union(hRng, cel)
Else
Set hRng = cel
End If
NextCell:
Next cel
' Hide rows of Hide Range.
If Not hRng Is Nothing Then
hRng.Rows.Hidden = True
End If
End Sub
I need to create an Excel document with updating totals in the A column, based on numbers entered in the B column. Each respective cell in row A should update based on its equivalent B cell value whenever a new value is added, and then the value entered into B is cleared once added to A.
I have gotten things working for one single row but don't have knowledge or understanding on how to best make this work for EACH cell pair in the entire column. I really don't want to copy and paste this 10,000 times and update the cells to reference the correct pair. Code for single cell:
Private bIgnoreEvent As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If bIgnoreEvent Then Exit Sub
bIgnoreEvent = True
Cells(1, 2) = Cells(1, 2) + Cells(1, 1)
Cells(1, 1) = ""
bIgnoreEvent = False
End Sub
I am hoping this can be achieved with a loop function, or a range of some sort.
This should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
'any updates in ColB?
Set rng = Application.Intersect(Target, Me.Columns(2))
If Not rng Is Nothing Then
Application.EnableEvents = False '<< prevent re-triggering of event
'Process each updated cell
For Each c In rng
If IsNumeric(c.Value) Then
With c.Offset(0, -1)
.Value = .Value + c.Value
End With
c.ClearContents
End If
Next c
Application.EnableEvents = True '<< re-enable event
End If
End Sub
I'm looking for some help please with some VBA.
Let's say I have a range of cells (B4:B12), so if I input data in a cell within the range I would like to clear all cells in the same range except for the cell in which I inputed the data. So that I can only have 1 cell with data in the range.
So if B5 had data and I inputed data in B7 then B5 would clear, then if i entered data in B10 then B7 would clear...
I hope there is a solution as I have been trying to find an answer for the past couple of hours.
I would do it this way:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Set myRange = Sh.Range("B4:B12")
'set the current cell/address/value so we don't lose them when the range is cleared
Set cell = Sh.Range(Target.address)
value = Target
'disable/enable so this isn't called every time a cell is cleared
'clear the range then reset the to entered value
If Not Intersect(Target, myRange) Is Nothing Then
Application.EnableEvents = False
myRange.Clear
cell.value = value
Application.EnableEvents = True
End If
End Sub
Or you could use worksheet event to bevplaced in the relevant worksheet code pane
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myVal As Variant
With Range("B4:B12") ‘reference your range
If Not Intersect(Target, .Cells) Is Nothing And Target.Count = 1 Then ‘ if changed range is one cell within the referenced one
myVal = Target.Value ‘store changed cell value
Application.EnableEvents = False
.ClearContents ‘ clear the content of referenced range
Target.Value = myVal ‘ write changed cell value back
Application.EnableEvents = True
End If
End With
End Sub