1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Need help with VB code

Discussion in 'BBS Hangout' started by KMC1499, Jan 28, 2010.

  1. KMC1499

    KMC1499 Member

    Joined:
    Dec 6, 2002
    Messages:
    170
    Likes Received:
    2
    So I inherited this excel worksheet that my company uses for time sheets. So I got this and my vb skills are little to nonexistent, lol. I can follow most of what it’s doing but I can’t quite pin down why it won’t pull the correct information.

    The time sheets consist of two things, the manager workbook and the timesheet itself where the employees enter time. Once an employee enters the time they email it to the manager who checks it and approves it. Upon hitting the approval button the workbook is supposed to check the manager workbook to see if the person hitting the buttons name is listed as a manager and if they are it should grab their name and the path on the server where the file should be saved. This is the part where its messing up. Currently it will find the manager and put their name in the correct cell on the worksheet, then it says it saving but for the life of me I can’t tell where.


    Here is the code from the manager signature file

    'Sub find_user_sig_test()
    'Dim find_user_sig As String
    Function find_user_sig()


    Dim usrName, objFound As String
    Dim obj As Object
    Dim rowCnt
    Dim rowNum


    'validate input to see if MGR name exists
    'if it does then exit loop and set find_user_sig to "ok"
    'if not exist - set find_user_sig = "error" and goto end of function
    usrName = LCase(Environ("USERNAME"))
    objFound = "false" 'may not need this ??????? depends on extend of validation
    rowCnt = 1
    rowNum = 0
    For rowCnt = 1 To 65000
    If LCase(Range("A" & rowCnt).Formula) = usrName Then
    objFound = "true" 'may not need this ??????? depends on extend of validation
    rowNum = rowCnt
    GoTo contenueScript
    End If
    Next rowCnt

    'if sig is not found then set error state and end function
    find_user_sig = "sigNotFound"
    GoTo EndFunction

    contenueScript: 'sig was found and function can contenue

    'return the user
    find_user_sig = "oK|" & Range("B" & rowCnt).Formula '& "|" & Range("C" & rowCnt).Value

    EndFunction:
    End Function
    This is the code from the find manager workbook. The line “ find_user_sig = "oK|" & Range("B" & rowCnt).Formula '& "|" & Range("C" & rowCnt).Value

    Is interesting because it has the last part & "|" & Range("C" & rowCnt).Value commented out. The column c holds the file path where the timesheet folder is supposed to be going. I uncommented it and didn’t make a difference.

    Here is the code from the timesheet worksheet the employee sends to the manager:

    'approves/disproves the employ's time sheet by manager
    'calls function in another excel timesheet that checkes to see if user is authorized to sign time sheets
    'places managers name on the sig line of emp time sheet
    'can only be run from a manager that has access to the HR folder holding the sig sheet
    '
    'called by sig_btn1,2,or3 and given the location that the signature and date should be placed
    'see above
    Sub Mgr_sig_copy(sigLoc As String, dateLoc As String, sigBtn As String, compLoc As String)

    Dim wbName, pgName, errorState, sigDate, sigName, passwd, passwd_Final, Passwd_WB_File As String
    Dim openWB

    errorState = "error" 'initializes errorState

    'turn off alerts and screen updateding
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    'grabs password to protect/unprotect sheet
    Passwd5_WS passwd 'sub call byref
    Passwd5_WB_File Passwd_WB_File 'subcall byref

    'unprotect sheet
    ActiveSheet.Unprotect Password:=passwd

    'get Name of this employ WorkBook
    wbName = ActiveWorkbook.Name 'to get path use FullName
    pgName = ActiveSheet.Name 'name of the sheet that needs mgr sig

    'open sig workbook
    Set objExcel = CreateObject("Excel.Application")
    On Error Resume Next
    Set openWB = objExcel.Workbooks.Open(Filename:="\\star\car\HR Folders\HR RESOURCES\remote_scripts_sig.xls", Password:=Passwd_WB_File)


    If Err.Number <> 0 Then 'usr can not open sig workbook and errorState = "error"
    'close mgr sig workbook without saving
    openWB.Close SaveChanges:=False
    GoTo skipSig
    End If

    'call/run macro in other workbook holding approved mgrs and grabs their name in errorState
    errorState = openWB.Application.Run("remote_scripts_sig.xls!find_user_sig")

    'close mgr sig workbook without saving
    openWB.Close SaveChanges:=False

    'select the location for the sig and then place it in corect cell
    If InStr(1, errorState, "oK|") = 1 Then 'mgr is approved to sign/approve time sheet

    'pull out the name and pathname from errorState
    CharPos1 = InStr(1, errorState, "|")
    CharPos2 = InStr(CharPos1 + 1, errorState, "|")

    sigName = Mid(errorState, CharPos1 + 1, CharPos2 - CharPos1 - 1)
    ' sigName = Right(errorState, (Len(errorState) - 3))
    Workbooks(wbName).Sheets(pgName).Range(sigLoc).Formula = sigName
    'HRFolderPath = Mid(errorState, CharPos2 + 1)

    'set date week was approved/signed
    sigDate = Now
    Range(dateLoc).Formula = sigDate

    'Places computer name of submitter in a hidden field next to sig date
    computer_name = Environ("COMPUTERNAME") 'the enviromental variable to get the compouter name
    Range(compLoc).Formula = computer_name
    Range(compLoc).Font.ColorIndex = 2 'white text

    'delete the button 'for the final release, activate the delete "reset page" button
    ActiveSheet.Shapes(sigBtn).Delete
    ActiveSheet.Shapes("Reset Page").Delete

    Range("B10").Select

    'sub call byref used to protect worksheet once it has been signed by Manager
    Passwd5_WS_Final passwd_Final

    'protect sheet for final release
    ActiveSheet.Protect Password:=passwd_Final

    'turn on alerts and screen updating
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True

    'Save workbook in HR Folders
    Select Case sigLoc
    Case "L45"
    FolderDates = Application.WorksheetFunction.Text(Range("E7"), "mm-dd-yy") & _
    " to " & Application.WorksheetFunction.Text(Range("H7"), "mm-dd-yy")
    Case "L49"
    FolderDates = Application.WorksheetFunction.Text(Range("M7"), "mm-dd-yy") & _
    " to " & Application.WorksheetFunction.Text(Range("P7"), "mm-dd-yy")
    Case "L53"
    FolderDates = Application.WorksheetFunction.Text(Range("U7"), "mm-dd-yy") & _
    " to " & Application.WorksheetFunction.Text(Range("X7"), "mm-dd-yy")
    Case Else
    End Select

    EmpName = InStr(1, Sheets("Info").Range("C2"), ",")
    LastName = Left(Sheets("Info").Range("C2"), EmpName - 1)
    FirstName = Right(Sheets("Info").Range("C2"), Len(Sheets("Info").Range("C2")) - EmpName - 1)


    HRFolderPathName = HRFolderPath & FolderDates & "\"
    Set fs = CreateObject("Scripting.FileSystemObject")
    If fs.FolderExists(HRFolderPath) = False Then
    MsgBox "Timesheet folder:" & vbCrLf & HRFolderPathName & vbCrLf & _
    "does not exist. Please contact HR for assistance.", vbOKOnly, "Timesheet folder does not exist"
    GoTo endSub
    Else
    End If


    ActiveWorkbook.SaveAs Filename:=HRFolderPath & FolderDates & "\" & LastName & " " & FirstName & " 2010.xls"
    ActiveWorkbook.Close

    GoTo endSub
    End If

    What I tried to do was comment out the HRFolder path and set it to a unc path, when I do that the managers name who approved the worksheet name dosent get added.
    Any help with this is greatly appreciated
     
  2. vlaurelio

    vlaurelio Contributing Member

    Joined:
    Jan 26, 2005
    Messages:
    21,310
    Likes Received:
    11,755
    was it even working before?
     
  3. KMC1499

    KMC1499 Member

    Joined:
    Dec 6, 2002
    Messages:
    170
    Likes Received:
    2

    Yes it was working, its what they were using until they needed it for another 6 month period. A just copied the pages and changed some dates.
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now