have a workbook_open macro run every 5 times and not every time

still learning

Well-known Member
Joined
Jan 15, 2010
Messages
821
Office Version
  1. 365
Platform
  1. Windows
Hi,

I use this when a workbook opens
VBA Code:
Private Sub Workbook_open()
Sheets("sheet1").Select
movebox
opening
End Sub
The macro “movebox” goes to the range “loans” and moves it down a line
VBA Code:
Sub movebox()
Range("loans").Select
Application.CutCopyMode = False
Selection.Cut
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Range("A1").Select
End Sub
Is there a way to only run this after the wookbook opens 5 times and not every time

mike
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Test this with a new workbook.
Include a worksheet called "Count". You can hide that worksheet if you want.
Then include this workbook open code.

If it does what you want then you can replace the message box line with whatever code you want to run each 5th time.

VBA Code:
Private Sub Workbook_Open()
  With Sheets("Count").Range("A1")
    If .Value = 4 Then
      .Value = 0
      MsgBox "5th time"
    Else
      .Value = .Value + 1
    End If
  End With
  ThisWorkbook.Save
End Sub
 
Upvote 0
Solution
Hi Pete
I get a "type mismatch" error at this line (run time error 13)
VBA Code:
.Value = .Value + 1

mike
 
Upvote 0
What is in A1 of sheet 'Count'?
That cell should have been empty when you created the sheet and this code only puts numbers in it so should be no type mismatch.
Testing worked fine at my end.
 
Upvote 0
Hi Pete
I had made a test macro to see if I was putting it in the right place in your macro. It went to A1 and typed "mike". Every time i saved it, it stayed.
I fixed that problem and it worked perfect.
Guess I should learn to read better. I didn't pay attention to the ....Range("A1")
Thank you


mike
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,052
Members
452,542
Latest member
Bricklin

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