+ Reply to Thread
Results 1 to 2 of 2

Combining two VBA codes

Hybrid View

  1. #1
    Registered User
    Join Date
    10-04-2011
    Location
    Durban
    MS-Off Ver
    Excel 2010
    Posts
    1

    Cool Combining two VBA codes

    Hey all,

    Im having trouble combinding these two codes. I am still very new at this! Sorry! Basically the first code is to force the user to enable macro's and the second counts everytime the document is opened which generates a unique code I need.

    Code 1

    Option Explicit

    Private Sub Workbook_Open()

    With Application
    'disable the ESC key
    .EnableCancelKey = xlDisabled
    .ScreenUpdating = False

    Call UnhideSheets

    .ScreenUpdating = True
    're-enable ESC key
    .EnableCancelKey = xlInterrupt
    End With

    End Sub
    '
    Private Sub UnhideSheets()
    '
    Dim Sheet As Object
    '
    For Each Sheet In Sheets
    If Not Sheet.Name = "Prompt" Then
    Sheet.Visible = xlSheetVisible
    End If
    Next
    '
    Sheets("Prompt").Visible = xlSheetVeryHidden
    '
    Application.Goto Worksheets(1).[A1], True '< Optional
    '
    Set Sheet = Nothing
    ActiveWorkbook.Saved = True

    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Application
    .EnableCancelKey = xlDisabled
    .ScreenUpdating = False

    Call HideSheets

    .ScreenUpdating = True
    .EnableCancelKey = xlInterrupt
    End With
    End Sub

    Private Sub HideSheets()
    '
    Dim Sheet As Object '< Includes worksheets and chartsheets
    '
    With Sheets("Prompt")
    '
    'the hiding of the sheets constitutes a change that generates
    'an automatic "Save?" prompt, so IF the book has already
    'been saved prior to this point, the next line and the lines
    'relating to .[A100] below bypass the "Save?" dialog...
    If ThisWorkbook.Saved = True Then .[A100] = "Saved"
    '
    .Visible = xlSheetVisible
    '
    For Each Sheet In Sheets
    If Not Sheet.Name = "Prompt" Then
    Sheet.Visible = xlSheetVeryHidden
    End If
    Next
    '
    If .[A100] = "Saved" Then
    .[A100].ClearContents
    ThisWorkbook.Save
    End If
    '
    Set Sheet = Nothing
    End With
    '
    End Sub

    Code 2

    Option Explicit

    Private Sub Workbook_Open()
    ActiveSheet.Protect UserInterfaceOnly:=True
    Dim x As String

    On Error GoTo ErrorHandler
    One:
    Open "c:\" & ThisWorkbook.Name & _
    " Counter.txt" For Input As #1
    Input #1, x
    Close #1
    x = x + 1

    Two:
    '******THIS LINE IS OPTIONAL******
    Sheets(1).Range("F2").Value = x
    '********************************
    Open "c:\" & ThisWorkbook.Name & _
    " Counter.txt" For Output As #1
    Write #1, x
    Close #1

    Exit Sub

    ErrorHandler:
    Select Case Err.Number

    Case 53 'If Counter file does not exist...
    NumberRequired:
    x = InputBox("Enter a Number greater than " & _
    "zero to Begin Counting With", _
    "Create 'C:\" & ThisWorkbook.Name & _
    " Counter.txt' File")
    If Not IsNumeric(x) Then GoTo NumberRequired
    If x <= 0 Then GoTo NumberRequired
    Resume Two
    Case Else
    Resume Next
    End Select
    End Sub

    Thanks in advance! This has really got me confused!

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Combining two VBA codes

    Use the code tags as per forum rules.

    Option Explicit
    
    Private Sub Workbook_Open()
    
    With Application
    'disable the ESC key
    .EnableCancelKey = xlDisabled
    .ScreenUpdating = False
    
    Call UnhideSheets
    
    .ScreenUpdating = True
    're-enable ESC key
    .EnableCancelKey = xlInterrupt
    End With
    
    End Sub
    '
    Private Sub UnhideSheets()
    '
    Dim Sheet As Object
    '
    For Each Sheet In Sheets
    If Not Sheet.Name = "Prompt" Then
    Sheet.Visible = xlSheetVisible
    End If
    Next
    '
    Sheets("Prompt").Visible = xlSheetVeryHidden
    '
    Application.Goto Worksheets(1).[A1], True '< Optional
    '
    Set Sheet = Nothing
    ActiveWorkbook.Saved = True
    
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Application
    .EnableCancelKey = xlDisabled
    .ScreenUpdating = False
    
    Call HideSheets
    
    .ScreenUpdating = True
    .EnableCancelKey = xlInterrupt
    End With
    End Sub
    
    Private Sub HideSheets()
    '
    Dim Sheet As Object '< Includes worksheets and chartsheets
    '
    With Sheets("Prompt")
    '
    'the hiding of the sheets constitutes a change that generates
    'an automatic "Save?" prompt, so IF the book has already
    'been saved prior to this point, the next line and the lines
    'relating to .[A100] below bypass the "Save?" dialog...
    If ThisWorkbook.Saved = True Then .[A100] = "Saved"
    '
    .Visible = xlSheetVisible
    '
    For Each Sheet In Sheets
    If Not Sheet.Name = "Prompt" Then
    Sheet.Visible = xlSheetVeryHidden
    End If
    Next
    '
    If .[A100] = "Saved" Then
    .[A100].ClearContents
    ThisWorkbook.Save
    End If
    '
    Set Sheet = Nothing
    End With
    '
    End Sub
    
    Code 2 
    
    Option Explicit
    
    Private Sub Workbook_Open()
    ActiveSheet.Protect UserInterfaceOnly:=True
    Dim x As String
    
    On Error GoTo ErrorHandler
    One:
    Open "c:\" & ThisWorkbook.Name & _
    " Counter.txt" For Input As #1
    Input #1, x
    Close #1
    x = x + 1
    
    Two:
    '******THIS LINE IS OPTIONAL******
    Sheets(1).Range("F2").Value = x
    '********************************
    Open "c:\" & ThisWorkbook.Name & _
    " Counter.txt" For Output As #1
    Write #1, x
    Close #1
    
    Exit Sub
    
    ErrorHandler:
    Select Case Err.Number
    
    Case 53 'If Counter file does not exist...
    NumberRequired:
    x = InputBox("Enter a Number greater than " & _
    "zero to Begin Counting With", _
    "Create 'C:\" & ThisWorkbook.Name & _
    " Counter.txt' File")
    If Not IsNumeric(x) Then GoTo NumberRequired
    If x <= 0 Then GoTo NumberRequired
    Resume Two
    Case Else
    Resume Next
    End Select
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1