Autofill a specific text in a range in excel 2013 - excel

I am trying to autofill text in a range(N6:N125) in excel 2013 via vba. My code works fine when range refers a single cell but not for a range. could you help me to find my mistake? My code is
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("N6:N125") = "" Then
Range("N6:N125").value = "My Text"
End If
End Sub

If you want to fill only the empty cells in N6 through N125 and some of the cells may already have values, but none have formulas, then a loop is not actually needed:
Sub marine()
Dim r As Range, s As String
Set r = Range("N6:N125")
s = "=IF(N6:N125="""",""MyText"",N6:N125)"
r.Value = Evaluate(s)
End Sub

If Range("N6:N125") = "" Then
This doesn't do the job. You will need to do this test cell-by-cell. An easier alternative would be this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
Range("N6:N125").SpecialCells(xlCellTypeBlanks).value = "My Text"
End Sub
This will fill empty cells within the range. BUT, if the intent of your test is to check whether the whole range is empty, this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Application.CountA(Range("N6:N125")) = 0 Then Range("N6:N125").value = "My Text"
End Sub

You can use:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Selection
For Each cell In Selection
If cell = "" Then
cell.Value = "My Text"
End If
Next cell
End With
End Sub
This code will test each cell in your selection if empty and write "My Text" in it, but if it is not empty it will skip it.
In your code you are not testing each cell and you are not using a loop to move from one cell to another

Related

Autofill Value in a cell based on other cell

Is there any way to get a specific value "66" in "B1" when I Put Text "Ok" in "A1" without entering Formula in "B1"
You can try Worksheet_Change event. Try below codes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("B1")) Is Nothing Then
If Val(Target.Value) = 66 Then
Range("A1") = "Ok"
End If
End If
End Sub

Automatically run macro when a value changes as the result of Vlookup

I want MyMacro to run automatically when the cell value in A1 changes according to a vlookup formula. MyMacro copies a row then pastes it to a different section of the worksheet when a condition dependant on the value of A1 is met.
I have tried variations of Target.Address and the following, which have not worked:
Public Sub Worksheet_Calculate(ByVal Target As Range)
Static OldVal As Variant
If Range("A1").Value <> OldVal Then
OldVal = Range("A1").Value
If Not Intersect(Target(1), Range("C1401:I140")) Is Nothing Then
If Range("A1").Value < ActiveSheet.Range("A1").Value Then
Range("C140:I140").Value = Range("B55:H55").Value
ElseIf Range("B55").Value > ActiveSheet.Range("A1").Value Then
Range("C140:I140").Formula = ""
End If
End If
End If
End Sub
Public Sub MyMacro()
Worksheet_Calculate ([C140])
End Sub
With this, MyMacro only works when I manually hit Run Macro after each time A1 changes. How can I get this to be automatic?
Thanks.
If you want to do something when the value of your calculated Cell A1 changes then use the Worksheet_Change event on the relevant worksheet.
(Find the worksheet in the VBA editor and right-click to select "view code")
Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("$A$1"), Target) Is Nothing Then
... [call your change_routine]
end if
End Sub

Excel VBA Click Cell to Fire Macro - Not working with Merged Cells

I am using the code below to fire a macro on the click of a cell. The cell in question is a header "Mitch's Macro" but it on merged cells B5 through J5. I have tried naming this merged range to MITCH, but it still doesnt run on click... Any ideas? Thank you in advance!
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("MITCH")) Is Nothing Then
MsgBox ("Hello World")
End If
End If
End Sub
The problem is Selection.Count = 1.
The merged cells have more than one cells so once you select any cell in the merged area, the code doesn't get executed.
Please give this a try...
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("MITCH")) Is Nothing Then
MsgBox ("Hello World")
End If
End Sub
Edit:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Set rng = Range("MITCH")
If Target.CountLarge > rng.Cells.Count Then Exit Sub
If Not Intersect(Target, rng) Is Nothing And Target.Cells(1).Address = rng.Cells(1).Address Then
MsgBox ("Hello World")
End If
End Sub
After a little more thinking I realised that most answers have a few drawbacks, and I think this is what we're really after:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Set rng = Range("MITCH")
If Target.Address = rng.MergeArea.Address Then 'check if what's selected matches the named range entirely
MsgBox ("Hello World")
End If
End Sub
As this checks whether the cells you have selected perfectly map onto the named area - specifically the MergeArea of the named range.
Matching with Intersect just checks if the selection contains the named Range
Matching by TL cell means any selection with the same TL as the named Range will also return a positive. E.g, if [B2:D3] is your merged named range, then matching by [B2] will return positive if [B2:D3] is selected (as expected), but also when [B2:XX100] is selected
This code only returns positive when the areas are identical, i.e. only the merged cell is selected.
If you have named the range you could use the code below
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tName As String
On Error Resume Next
tName = Target.Name.Name
On Error GoTo 0
If Len(tName) > 0 Then
If tName = "MITCH" Then
MsgBox ("Hello World")
End If
End If
End Sub

How to set a cell value depeinding on a specefic cell value

I am trying to automate the excel sheet by checking the value of a specific cell, and setting multiple columns with a value.
For example if A1 cell is "2" then I would like all the cells from B2:D54 to be the value equal to 0. I have a code written but it looks like it is wrong.
Private Sub Worksheet_Change(ByVal Target As Range)
//this selects the cell C3 and checks if the value if is 0619
If Range("C3")="0619"
Dim example As Range
Set example =Range("E3:AE53")
example.Value = 0
End if
End Sub
Edit - Added Then, but still not working.
Private Sub Worksheet_Change(ByVal Target As Range)
//this selects the cell C3 and checks if the value if is 0619
If Range("C3")="0619" Then
Dim example As Range
Set example =Range("E3:AE53")
example.Value = 0
End if
End Sub
You are missing Then in the line with If
Comments in VBA are started with ', not // so this will not parse correctly.
If Range("C3")="0619" Bear in mind Excel will remove leading zeros from numbers. Only have leading zeros if you will be formatting the value as text.
Edit: If Range("C3").Value better than If Range("C3")
Private Sub Worksheet_Change(ByVal Target As Range)
'this selects the cell C3 and checks if the value if is 0619
If Range("C3").Value = "0619" Then
Dim example As Range
Set example = Range("E3:AE53")
example.Value = 0
End If
'
Range("A1").Select
End Sub
only perform the work if C3 is changed.
if C3 is a number formatted as 0000 (leading zero) then use If Range("C3").Text ="0619" Then
as mentioned in comments, disable events or your routine will try to run on top of itself when you change the values in the cells.
event driven sub procedures should have error control
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
if not intersect(target, range("C3")) is nothing then
on error goto safe_exit
application.enableevents = false
select case range("C3").text
case "0619"
Range("E3:AE53") = 0
case else
'do nothing
end select
End if
safe_exit:
application.enableevents = true
End Sub
I've changed your criteria check to a Select Case statement to make it easier to accommodate additional conditions.

VBA macro to change color of cells in range when a cell is selected

I'm trying to write code that will, when a user selects a cell within a range, change the color of all the cells in that range. When running the code posted below, i get an error "Object doesn't support this property or method". I noticed that if i edit the code to just paste a "1" in all the cells that it works just fine. What am I doing incorrectly?
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.range)
If Not Intersect(Target, range("G1:I5")) Is Nothing Then
For Each cell In range("G1:I5")
cell.interier.ColorIndex = 10
Next
End If
End Sub
You should be aware that you do not have to loop over the cells:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Range
Set r = Range("G1:I5")
If Intersect(r, Target) Is Nothing Then Exit Sub
r.Interior.ColorIndex = 10
End Sub
The error is simply a typo of Interior
...
cell.Interior.ColorIndex = 10
'# ^
...

Resources