Solved

Run macro if Workbook_Open() criteria is met

Posted on 2013-06-05
4
234 Views
Last Modified: 2013-06-09
I want to run my procedure of moving row data to another worksheet. I have that but I want it to run automatically when the spreadsheet opens. If Column B date is 7 days ago from today's i want it to run my macro (TransferPrints) . For example if today is 06/05/2013 and column B has a date of  05/20/2013 (Subtract only weekdays), it would run, if it is 6/01/2013 it would not run my macro for that particular row.

I tried adding the following procedure to the workbook page but I'm having all my data in Column B removed instead of filtering and selecting only days that are greater than 7 days from today


Private Sub Workbook_Open()
    On Error Resume Next
   
    If (Application.WorksheetFunction.Weekday(Format(Now(), "m/d/yyyy") > 7)) Then
            If (Format(Now(), "m,d,yyyy") = Sheets("Print").Range("B:B").Value) Then
   
     
     'If (WorksheetFunction.WorkDay(Sheets("Print").Range("B:B"), -7)) Then
       
            TransferPrints
        End If
    End If
End Sub
0
Comment
Question by:Southern_Gentleman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39225394
Not sure how your TransferPrints function works but the formula  .Range("B:B").  selects the entire column as a whole, and doesnt check each individual cell.

something like this may work....

dim cellX as range
for each cellX in Sheets("Print").Range("B:B")
    if now() - cellX >7 then
          TransferPrints cellX  ''does Transfer prints use the ActiveCell ??
    endif
next cellX

Open in new window


can you post parts of a sample workbook for us to test against ?
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39225951
I'm amazed that anything works at all.

This code:
 If (Application.WorksheetFunction.Weekday(Format(Now(), "m/d/yyyy") > 7)) Then

Open in new window

Should always be false since the Weekday function returns a number from 1-7, which will never be > 7.

In addition, there is no reason to format the value unless you want to see it differently in the debugger.

And the "Application.WorksheetFunction" part is optional.

So what I think you're looking for is something closer to what robberbaron posted.  Ultimately, if you want to check more than one cell, you either need to use a function that supports ranges (of which neither Format nor Now do in your second If) or you need to use some kind of loop (like robberbaron's For Each)
And as indicated, the problem may be in other parts of the code.
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
ID: 39230849
to determine the number of WeekDays is not simple but has been done before.
Function fctnNetWorkDays(datFrom As Date, datTo As Date, Optional booIncEnd As Boolean = False) As Long
    'Code sample from Accessory http://www22.brinkster.com/accessory/modules/003.shtml
    Dim lngDiff As Long, booFWD As Boolean, booTWD As Boolean, bytF As Byte, bytT As Byte
    If booIncEnd Then datTo = datTo + 1
    lngDiff = DateDiff("d", datFrom, datTo, 0)
    bytF = WeekDay(datFrom, 0)
    bytT = WeekDay(datTo, 0)
    booFWD = (bytF < 6)
    booTWD = (bytT < 6)
    If booFWD And booTWD Then
        fctnNetWorkDays = (Int(lngDiff \ 7) * 5) + lngDiff Mod 7
        If bytF > bytT Then fctnNetWorkDays = fctnNetWorkDays - 2
    ElseIf Not booFWD And booTWD Then
        fctnNetWorkDays = (Int(lngDiff \ 7) * 5) + (lngDiff Mod 7) + bytF - 8
    ElseIf booFWD And Not booTWD Then
        fctnNetWorkDays = (Int(lngDiff \ 7) * 5) + (lngDiff Mod 7) - bytT + 6
    Else
        fctnNetWorkDays = (Int(lngDiff \ 7) * 5) + (lngDiff Mod 7) + (bytF <> bytT)
    End If

    
End Function

Open in new window

0
 

Author Closing Comment

by:Southern_Gentleman
ID: 39233696
Thanks Roberbarron, good stuff.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question