Running a Macro continuously

Arcadian

Board Regular
Joined
Jul 27, 2004
Messages
111
Hello everyone,

I have a question.

I want to create a simple macro which continuously checks the data in cell A1.
Right now it doesn't work. I have to select "Run Macro" in the menu everytime I want my macro to check the data.
I know it's possible to automate this process and I suspect/hope it's very simple.

The code I'm using is as follows:

Sub test()
If [A1] = 1 Then
[B1] = "1"
ElseIf [A1] = 2 Then
[B1] = "2"
ElseIf [A1] = 3 Then
[B1] = "3"
ElseIf [A1] = 4 Then
[B1] = "4"
ElseIf [A1] > 4 Then
[B1] = "Wrong Entry"
ElseIf [A1] < 0 Then
[B1] = "Wrong Entry"
End If
End Sub

My experience with Visual Basic is no greater then the above lines of code so I'd apreciate it if you could keep it simple :)

Tim

Edit: I forgot to tell you, cell A1 is a pulldown list, created with the validation>list option.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello and welcome to the board,

If you go to the VB COde Window, select the relevant sheet, you can have the following

Code:
Private Sub Worksheet_Activate()
'your code here
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
'your code here
End Sub


The wroksheet activate will run the code when you select this sheet.

The Worksheet_change will run the code when you make a change anywhere in this sheet.

Is this explained enough?
 
Upvote 0
Actually, although it does the trick, it slows down the process bigtime since it runs the macro everytime I update the worksheet. I only want it to run that macro when I update cell A1.
Is there a way to do this?
 
Upvote 0
Hello,

Yes, no problem use this code

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
'your code here
End If
End Sub

Any better?
 
Upvote 0
Hi like the original poster my VBA knowledge is very limited. I am trying to get a macro to hide columns based on a cell value. The cell value is inputted manually and based on this the rows should hide. Basically if the cell d3 is equal to zero then all rows show, but if the cell value is greater than 0 then the rows 25:75 are hidden.

I have managed to put together some code based on googling around, and using this forum, and just playing around with code, and luckily for me I do not get any errors.

I've put the VBA into the VBA window by right clicking the sheet tab, and clicking view source using General and Declarations. The code I have used is shown below

Code:
Private Sub Worksheet_Activate()
If Cells(3, 4).Value > 0 Then
  ActiveSheet.Rows("25:75").Hidden = True
ElseIf Cells(3, 4).Value = 0 Then
  ActiveSheet.Rows("25:75").Hidden = False
End If
End Sub
 
 
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells = "$D$10" > 0 Then
    ActiveSheet.Rows("25:75").Hidden = True
ElseIf Target.Cells = "$D$10" = 0 Then
  ActiveSheet.Rows("25:75").Hidden = False
End If
End Sub

And I've saved a macro called 'run' in the macro VBA editor window

with the following code:

Code:
Sub run()
If Cells(3, 4) > 0 Then
  ActiveSheet.Rows("25:75").Hidden = True
ElseIf Cells(3, 4) = 0 Then
  ActiveSheet.Rows("25:75").Hidden = False
End If
End Sub

I want the macro to run in the background continuously. Now what happens is if I change the value in d3 from 0 to say '3' nothing happens, where it should hide row 25:75. However I can run the macro manually and it works. but when I then change the value from '3' back to 0 it automatically shows the row 25:75, which it should.

Where it works automatically one way, it doesn't work the other way.


Please can you help?

Thanks,

Zak
 
Upvote 0
You would do better to start a new thread as this isn't anything to do with the original poster's problem and as he appears to have solved it, there may not be many people watching this thread now.

Hardly anyone will notice this post, so start a new thread and you will get replies, I'm sure.
 
Upvote 0
Hello, all! I have a very similar situation except I can't get the second part to work. I have an order form with certain items that need to be ordered in quantities of six. The "Worksheet_Activate()" bit works; if I click into 'Sheet 2' and back into 'Sheet 1', the macro auto-runs and rounds the quantities in the select cells to multiples of 6.

Here's my code:

Private Sub Worksheet_Activate() 'This part is working fine
For Each cell In [N46:N51, N56:N59]
If cell = " " Then Exit Sub
cell.Value = Application.MRound(cell.Value, 6)
Next cell
End Sub


Private Sub Worksheet_Change(ByVal Target As Range) 'This part isn't working
For Each cell In [N46:N51, N56:N59]
If cell = " " Then Exit Sub
cell.Value = Application.MRound(cell.Value, 6)
Next cell
End Sub

The error I receive is "Run-time error '28': Out of stack space"; when I click to debug, "For Each cell In [N46:N51, N56:N59]" (under 'Worksheet_Change...") is highlighted.

Any ideas?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top