Autoexecute an Excel macro - vba

I have a macro which is executed on the striking of ctrl-u - I would like that macro to AUTOMATICALLY execute everytime a number gt > 0 is entered into A2 is there an easy way to do that?

You could call the macro when the cell content changes. Open Excel's the Visual Basic editor, and add something like this to the sheet where you want the macro to run automatically:
Private Sub Worksheet_Change(ByVal Target As Range)
' Column 1 is the A column
If Target.Column = 1 And Target.Row = 2 Then
If Target.Value > 0 Then
' call macro here
MyMacroName
End If
End If
End Sub

An even easier solution is. Place this in the "ThisWorkBook" module
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A2").Address and Target.Value >0 Then
' do something here
MsgBox "This works!"
End If
End Sub

Related

Macro doesnt appear on the List after copy-pasting code

I am completely new to VBA and hence have no idea what I'm doing...
Below is the description of my problem and also the code in question.
What I originally wanted to do was this:
Copy a clicked cell, Select new sheet, Select any cell, Paste as values
Now i found a code that apparently does the trick which is this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("A")) Is Nothing Then
Cancel = True
If Target.Row > 1 And Len(Target.Value) Then Worksheets("S11").Range("C2").Value = Target.Value
End If
End Sub
I say "apparently" as I am not able to test it. Here is the problem:
I saved the workbook as excel macro-enabled workbook
Clicked ALT Q - to go back to my workbook
I then ALT F8 to run it - but there is nothing there... blank....
what am I missing?
In a module, have the macro you wish to use.
public sub mymacro(r as excel.range)
If Not Intersect(r, Columns("A")) Is Nothing Then
If r.Row > 1 And Len(r.Value) Then Worksheets("S11").Range("C2").Value= r.Value
End If
end sub
public sub wbtest()
mymacro activecell
end sub
and use like so
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("A")) Is Nothing then
mymacro target
end if
end sub

VBA - Open a msgbox when cell value = 1

I need help with a very basic vba macro. When the value in A6 is equal 1 a msgbox needs to appear in the workstation. I developed the below code but the problem is that when I add any other information in the spreadsheet (for example, if I write "i love pizza" in B6) the msgbox will prompt again and I need it to prompt just one time, just when I set the value of A6 = 1. Could you please help me?
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A6").Value = 1 Then
MsgBox "THIS IS A MSGBOX."
End If
End Sub
#edit
I forgot one very important thing... the value "1" is getted with a VLOOKUP so its not insert manually by the user, i'm sorry about this. I tried the codes you people answered and it worked just when I put the vlue manually and as I said in the edit start, the value is set with a VLOOKUP. Any other tips, please?
You need to check if the change is due to the cell A6 being changed, rather than a different cell.
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row = 6 Then
If Target.Value = 1 Then
MsgBox "THIS IS A MSGBOX."
End If
End If
End Sub
You can use this code instead of the previous one
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Me.Range("A6").Address And Me.Range("A6").Value = 1 Then
MsgBox "THIS IS A MSGBOX."
End If
End Sub
Target is the changed cell in Excel. so for every change event, I check if the target is Range("A6").
Pertaining to the statment : and I need it to prompt just one time, you need to save the oldvalue. So the prompt is displayed only once, when you set the value to 1. If A6 is already 1 and then you type 1 again, no prompt.
Option Explicit
Dim oldVal
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Address = "$A$6") Then
If Target.Value = 1 And Target.Value <> oldVal Then
oldVal = Target.Value
MsgBox "Test"
End If
End If
End Sub
You need to check inside the Worksheet_Change event, that only if Cell "A6" is pressed, then continue. And afterwards, check if the value of the cell equals 1.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A6")) Is Nothing Then
If Target.Value = 1 Then MsgBox "THIS IS A MSGBOX."
End If
End Sub
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$6" Then
If Target.Value = 1 Then
MsgBox "THIS IS A MSGBOX."
End If
End If
End Sub
I forgot one very important thing... the value "1" is getted with a VLOOKUP so its not insert manually by the user, i'm sorry about this. I tried the codes you people answered and it worked just when I put the vlue manually and as I said in the edit start, the value is set with a VLOOKUP. Any other tips, please?

Sheet specific macro will not run when condition is met

I have a very simple workbook macro for testing and it is not executing when the condition is met. Do you know why? Macros are enabled and modules are working, however, sheet codes will not work. Any idea why?
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$J$3" Then
Call macro1
End If
End Sub
And then I tried
if A1 = "correct!"
msgbox "hey"
else <do nothing>
If I adjust anything in cell J3 or putting "correct!" in A1 in that sheet, neither code will execute. macro1's code is simply msgbox "Hey". Any idea what I can do?
Thanks!
I used Workbook_SheetChange and it worked for me:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Sheet1" And Target.Address = "$J$3" Then
Call macro1
End If
End Sub
Sub macro1()
MsgBox "hello world!"
End Sub
I hope I'm understanding your post correctly, you want that once you modify a value in your worksheet at Cell J3, and the value in Cell A1 (at the same worksheet) is "correct!", that a MsgBox with "hey" will pop-up.
The code in your Worksheet_Change event is good, just modify the code in macro1 (which can be placed at another code module).
Sub macro1()
If Range("A1").Value = "correct!" Then
MsgBox "hey"
End If
End Sub
Option 2: using the Like operator
If Range("A1").Value Like "correct!" Then
MsgBox "hey"
End If

VBA - Open a UserForm by clicking anywhere in a specific column

I would like to build a makro in VBA which opens a UserForm when I click in a cell in a specific column, for more details look here.
With this code (from Mr.Burns):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A1")) Is Nothing Then
'name of userform .Show
End If
End If
End Sub
I was able to open the UserForm by clicking in the cell A1, but not by clicking in any cell inside the column A.
I tried to solve this problem with this code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
Dim check As Boolean
check = True
If check Then
Dim i As Long
For i = 1 To 100000
If Not Intersect(Target, Range("A" & i)) Is Nothing Then
UserForm1.Show
check = False
End If
Next
End If
End If
End Sub
It actually works fine, but it is very slow, is there any better possibility to solve this?
To display the form when a cell is selected in column A:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' if target is one cell and in column A
If Target.Columns.count = 1 And Target.Rows.count = 1 And Target.Column = 1 Then
UserForm1.Show
End If
End Sub
You can use .count and .column property together with AND and it will become so much simple and fast. Following code triggers pop-up if u click in column A on active-sheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo errorhandler
If Target.Count = 1 And Target.Column = 1 Then '.count to check if only one cell is selected and .column to check if it is a first column
'UserForm1.Show
'Do whatever you want to do here like opening User form
MsgBox "You clicked in column A"
End If
errorhandler:
End Sub

VBA to format cells based on another cell not working

SOLVED: FOUND MY OWN WORKSHEET ERROR
The problem I was having was trying to use two worksheet_change events in the same workbook. Because I thought that was possible, I was just renaming the worksheet event in question when I received an error, thinking nothing of it. Both my original code and the answer provided work, when combined with my other worksheet_change event.
Thanks everyone.
Original Request:
I am trying to run a macro that does this:
every time cell r6 changes, run a macro that looks to see if the value in cell s9 is > or < 1, then format cells s9:t100 based on that.
I have the macro on its own to do the second part:
sub macro1()
If Range("S9").Value < 1 Then
Range("S9:S100,T9:T100").Select
Selection.NumberFormat = "0.0%"
Else
Range("S9:S100,T9:T100").Select
Selection.NumberFormat = "#,##0"
End If
end sub
This macro run on its own, works exactly as I want and formats the cells.
Then I have the worksheet event to call up that macro:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$R$6" Then
Call Macro1
End If
End Sub
When it is run to call up the same macro, it does not format the cells. They just stay as a % regardless of when cell r6 changes.
Any ideas why the worksheet event causes the macro to not work?
Try passing the worksheet object to your macro. This fully qualifies the Ranges to make sure you're working on the right area.
Also, you don't need to Select at all. Just use the range and directly change the settings.
Public Sub Macro1(ws as Worksheet)
If ws.Range("S9").Value < 1 Then
ws.Range("S9:S100,T9:T100").NumberFormat = "0.0%"
Else
ws.Range("S9:S100,T9:T100").NumberFormat = "#,##0"
End If
end sub
Sub test()
Macro1 ActiveSheet
End Sub
And in your Worksheet_Change...
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$R$6" Then
Macro1 Target.Worksheet
End If
End Sub