I have a range of values; I would like to click on a subset of this range and have the sum be displayed in another cell outside of the range.
Right now, I'm able to transfer the value of the clicked cell to another cell
but I'm unable to display the summation of the values when I click multiple cells.
This is my current code; I also have the clicked cell turn yellow to indicate that it's been selected.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B7:B8")) Is Nothing Then
Range("L7").Value = Selection.Value
Target.Interior.Color = vbYellow
End If
End If
End sub
I'm wondering if I can apply a sum function to
Range("L7").Value = Selection.Value
The status bar can be configured to show the Average, Count, Sum, etc of the selected cells. If you want to show it in a worksheet cell, try this
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1:B10")) Is Nothing Then
Range("L7").Value = WorksheetFunction.Sum(Target)
End If
End Sub
Related
i need to write a cell value in other cell by clicking a range of cells as below:
my range is between b1:x30 and i want every time i click on these selection range then the value of b1 to b30 (depending which row selected) will write in cell z1
i tried to write a code as below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B1:X30")) Is Nothing Then
If Intersect(Target, Range("B1:X30")) Then
Worksheets("Order Sheet").Range("Z1").Value = .Range("B1:B30").Value
End If
End If
End If
End Sub
i know this code is not complete yet and i need to help to complete.
I have this function where anytime a cell inside the specific range changes, calls a function.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:L60")) Is Nothing Then
Call fit_text
End If
End Sub
The function fit_text changes the font size of the value of the active cell.
Sub fit_text()
MsgBox ActiveCell.Characters.Count
If ActiveCell.Characters.Count > 100 Then
ActiveCell.Font.Size = 8
Else
ActiveCell.Font.Size = 10
End If
End Sub
PROBLEM: whenever I change the value of a cell where the character count is bigger then 100, the font size remains 10 and the message box that tells the value of the count shows 0, but whenever I run it on vba the message box shows the correct value and changes the font size if the count is bigger then 100. I need it to be automatic. CanĀ“t change the height or the width of the cells
Note that Excel can automatically shrink the font size to fit into the cell. Therefore select your cell, press Ctrl+1 go to the Alignment tab and select Shrink To Fit.
To fix your code:
Don't use ActiveCell. Use Target or the Intersect range instead. The ActiveCell might not be the cell that was changed. And also Target can be multiple cells so you need to loop through all the changed cells and test each cell individually.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Intersect(Target, Target.Parent.Range("A1:L60"))
If Not AffectedRange Is Nothing Then
Dim Cell As Range
For Each Cell In AffectedRange 'loop through all changed cells
MsgBox Len(Cell.Value)
If Len(Cell.Value) > 100 Then
Cell.Font.Size = 8
Else
Cell.Font.Size = 10
End If
Next Cell
End If
End Sub
ActiveCell is the one active after the Change event. You can pass Target from the event to your method fit_text, so that it will always refer to the changed cells:
Private Sub Worksheet_Change(ByVal target As Range)
If Not Intersect(target, Range("A1:L60")) Is Nothing Then
Call fit_text(target)
End If
End Sub
Sub fit_text(target As Range)
MsgBox ActiveCell.Address(False, False)
MsgBox target.Characters.Count
' If ActiveCell.Characters.Count > 100 Then
' ActiveCell.Font.Size = 8
' Else
' ActiveCell.Font.Size = 10
' End If
If target.Characters.Count > 100 Then
target.Font.Size = 8
Else
target.Font.Size = 10
End If
End Sub
You will also want to include a check for when Target is more than a single cell; in which case you will probably want your procedure to check each cell's content.
The problem is the 'ActiveCell'.
For example when you edit the Cell A1 and press enter, the ActiveCell you are using in fit_text is not A1, but A2.
This however can easily fixed, by just passing the Cell from the Worksheet_Change to fit_text.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:L60")) Is Nothing Then
'Pass the Target to 'fit_text'
Call fit_text(Target)
End If
End Sub
Sub fit_text(Cell)
'Instead of using ActiveCell, use Cell (which is the passed Target)
MsgBox Cell.Characters.Count
If Cell.Characters.Count > 100 Then
Cell.Font.Size = 8
Else
Cell.Font.Size = 10
End If
End Sub
I have a Dynamic Table that changes based on the value of B1.
I want the value of B1 to change by clicking on another cell from another workbook. I'm super new to VBA and don't really know what I'm doing.
What I want is to click on any cell from column E and it will change the value of B1 to equal value of column O.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E2:E")) Is Nothing Then
Worksheets("Dynamic Table").Range("B1").Value = ActiveCell.Offest(0, 10)
End If
End If
End Sub
My Amateur Code
Do you mean sth like that
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 5 Then
If Target.Cells.CountLarge = 1 Then
Worksheets("Dynamic Table").Range("B1").Value = Target.Offset(, 10).Value
End If
End If
End Sub
I assumed you mean with another workbook just another worksheet.
I am using following macro to show value of clicked cell (range A5:A200) in A1 cell:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A5:A200")) Is Nothing Then
Range("A1").Value = Selection.Value
End If
End If
End Sub
...and it works great. Additionally in A2 I'd like to show related value from C column.
Example:
I click on A10 -> and see the value in A1, then in A2 i'd like to see the value from C10 cell
You can utilize the .row property of Selection. But, you already have Target being used from the arugument within the Worksheet_Selection() event as your "Selection" range, and using that range is preferred over using selection.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 Then
If Not Intersect(Target, Range("A5:A200")) Is Nothing Then
Range("A1").Value = Target.Value
Range("A2").Value = Cells(Target.row, "C")
End If
End If
End Sub
So replace your selection ranges with Target, and use the row property within Cell() to get your value from the C column.
I am using the following piece of vba inside my Worksheet_Change Sub to autosize a particular column with text entries:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextTarget As Range
Set nextTarget = Range(Selection.Address) 'store the next range the user selects
If Target.Column = 1 Then
Target.Columns.Select 'autofit requires columns to be selected
Target.Columns.AutoFit
nextTarget.Select
End If
End Sub
The above has the problem that every time you enter in a cell of that column text which is shorter than the other cells, it will shrink the column to fit the target cell, leaving the other cells with text outside. Is there any addition that I could make to solve this?
Use the .EntireColumn method. With this there is no need to Select any cells.
Private Sub Worksheet_Change(ByVal Target As Range)
'added extra error trapping in case something happens where more than 1 column is changed.
If Target.Columns.Count = 1 And Target.Column = 1 Then
Target.EntireColumn.AutoFit
End If
End Sub