I wrote the following code in my worksheet and all ranges are in this same sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("DistMatrix")) Is Nothing Then
Dim out1() As Double
out1 = OutStat(bucket(Target), Range("RegScale"))
FwdOut = outright(bucket(Target), Range("RegScale"))
Call NewScatter(FwdOut, out1)
End If
End Sub
I want to run a called sub if I choose a cell in the range DistMatrix.
This is partly working. I have to click on a cell in the range as if I want to write in it and then afterwards pick another one for the called sub to run.
I want however the sub to run as soon as I pick the cell. I don't want to go have to double click it as if it were to edit it and then pick another one for it to run.
You can use Worksheet_SelectionChange instead.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("DistMatrix")) Is Nothing Then
Dim out1() As Double
out1 = OutStat(bucket(Target), Range("RegScale"))
FwdOut = outright(bucket(Target), Range("RegScale"))
Call NewScatter(FwdOut, out1)
End If
End Sub
Related
I used to have the following code that worked to bring up a user form (calendar)when A11:A29 or G8 was clicked:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Target, Range("A11:A29")) Is Nothing Then DatePickerForm.Show
If Not Application.Intersect(Target, Range("G8")) Is Nothing Then DatePickerForm.Show
End Sub
I wound up having to scrap that user form due to issues with MS MonthView Control 6.0. I moved to a different calendar found here. The site suggests using double click anywhere on the sheet to show the userform. This works as designed but does so for any cell whereas I would like to limit it to only cells A11:A29 and G8, either by double clicking or clicking once. I tried modifying the code on the page to:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ARange As Range
Dim GRange As Range
Set ARange = Range("A11:A29")
Set GRange = Range("G8")
If Not Application.Intersect(Target, ARange) Is Nothing Then DatePickerForm.Show
If Not Application.Intersect(Target, GRange) Is Nothing Then DatePickerForm.Show
End Sub
I get run-time error 91, object variable or with block not set. Debugging highlights the "If isdate" line below:
Private Sub UserForm_Activate()
If IsDate(Target.Value) Then
Calendar1.Value = Target.Value
End If
Call MoveToTarget
End Sub
It is because Target is Range object familiar only inside the specific Worksheet code module it is placed (like a local variable) , oo it's not recognized inside the DatePickerForm form module.
Use:
Private Sub UserForm_Activate()
If IsDate(ActiveCell.Value) Then Calendar1.Value = ActiveCell.Value
End If
Also, there is a more efficient way to see if Target falls inside multiple Range, use Union to merge multiple Ranges into one Range, see code below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim UnionRange As Range
Set UnionRange = Application.Union(Range("A11:A29"), Range("G8"))
If Not Application.Intersect(Target, UnionRange) Is Nothing Then DatePickerForm.Show
End Sub
Currently using this code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address = "$B$1" Then Range("B9:AE53").Interior.Color = xlNone
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Range("B9:AE53")) Is Nothing Then
For Each c In Intersect(Target, Range("B9:AE53"))
Target.Interior.Color = vbYellow
Next c
End If
End Sub
Autohighlight only works when I manually edit (or F2 then enter) the cells in B9:AE53. I was hoping for something that would change the cell color if I edit the data in the orders sheet (reference for B9:AE53).
Was also hoping to transfer the event from B1 to a command button.
You can easily change the second part of your question, the one where you wish to reset the cell colors to nothing, to be executed by a button.
Change the code to this:
Sub CleanUp()
Range("B9:AE53").Interior.Color = xlNone
End Sub
Then add a button via the Forms Control Button in the developer toolbar and asign the above macro to it. This will clean up the area. Once you specifiy what the desired outcome is for the first part of you question I might be able to help with that.
Sheet 1 of my workbook contains (besides other data) a list of the other worksheets in column A. I wish to be able to click on any cell in column A5:A50 and go to the appropriate worksheet listed in that cell. My Sheet1 code is:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A5:A50")) Is Nothing Then SelectWorksheet
End Sub
and Module2 is:
Sub SelectWorksheet()
Dim strName As String
strName = Sheet1.Range("Target").Text (Error occurrs here: "Method 'Range' of object 'Worksheet' failed")
Sheets(strName).Select
End Sub
How do I get this to work as I expect? I know I could just click on the appropriate worksheet tab but I'm trying to learn how to code in VBA. Thanks. By the way, how do I get my post to show the code as typed in the question box?
Like this. You probably want to use the _SelectionChange event instead of the _Change event. Or you may find it necessary to use both events to trigger it. In any case here is how you pass the variable to another subroutine/module:
Sub Worksheet_SelectionChange(byVal Target as Range)
'Some code...
'
Call OtherMacro(Target)
'
End Sub
And then in your other macro, declare a range variable as required argument, like so:
Sub SelectWorksheet(rng as Range)
'
Dim strName as String
' at this point you can work with the "rng" variable, because it's been received from the other subroutine
strName = rng.Value
Sheets(strName).Activate
'
End Sub
You would need to add additional test to make sure user has not selected multiple cells, etc., but this should get you started.
Why not pass the sheet name from the cell to the sub?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A5:A50")) Is Nothing _
And Target.Cells.Count = 1 Then SelectWorksheet (Target.Value)
End Sub
Sub SelectWorksheet(strName As String)
Sheets(strName).Select
End Sub
I've also done a check to make sure that only one cell is in the selection.
I am trying to write a macro where changing any column
should automatically save worksheet.
My Excel sheet expands till G25.
I tried this but its not working:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("G25")) Is Nothing Then
ActiveWorkbook.Save
End Sub
I have save it under ThisWorkBook.
Any help is appreciated.
Under ThisWorkbook that handler is called Workbook_SheetChange and it accepts two arguments, Sh (of type Object) and Target (a Range). So, your code won't work there.
If you put your code in the sheet you want (Sheet1, for instance) instead of in the ThisWorkbook, put the End If and change the range to "A1:G25" (representing a square from row 1 column A to row 25 column 25), it should work.
This did:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
MsgBox "changed"
End If
End Sub
For completeness, under ThisWorkbook, this will work:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
MsgBox "changed"
End If
End Sub
The other common reason why an Event doesn't work is that EnableEvents has been set to False
I would code your problem like this as
typically you need to work with the range of interest that is being tested. So creating a variable rng1 below serves both as an early exit point, and a range object to work with. On a sheet event Target.Worksheet.Range("A1:G25") will work but it is lengthy for it's actual use
If you do have a range to manipulate then making any further changes to the sheet will trigger a recalling of the Change event and so on, which can cause Excel to crash. So it is best to disable further Events, run your code, then re-enable Events on exit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Set rng1 = Intersect(Target, Range("A1:G25"))
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
'work with rng1
Application.EnableEvents = True
End Sub
I usually try to avoid VBA in Excel, but it would be convenient to be able to type text into a cell, and have its column get wider or narrower to accommodate the text remaining as it's entered or deleted.
This would be subject, of course, to the lengths of the text in the other cells in the column.
'Auto-fit as you type', I guess you might call it.
Is there an easy way to do this in a suitable handler?
I'm not sure if there is a way to do it while your typing. I think excel generally stretches the cell view to display all the text before it fires the worksheet_change event.
This code will resize the column after you have changed and moved the target to a new range. Place it in the worksheet module.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextTarget As Range
Set nextTarget = Range(Selection.Address) 'store the next range the user selects
Target.Columns.Select 'autofit requires columns to be selected
Target.Columns.AutoFit
nextTarget.Select
End Sub
If your just looking to do it for a particular column you would need to check the target column like this:
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
I cannot think of a way to do what you ask for but something very close to your need.
In modern versions of Excel (2010+, I don't know about the 2007 version) you could use the following macro to resize your column to fit data as soon you finished entering data in a cell.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
ActiveSheet.Columns.AutoFit
End Sub
Put the macro in ThisWorkbook module
This will automatically fit columns width
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Columns.AutoFit
End Sub
I just tried the previous two answers on a sheet and they didn't do anything, idk if the "ByVal Sh" is the problem?? was it a typo?
Anyhow, here is my answer, checked it and it works:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target Is Nothing Then
Exit Sub
Else
With Target
.Columns.Select
.Columns.AutoFit
End With
End If
End Sub
-.Reverus.