+ Reply to Thread
Results 1 to 11 of 11

Add vb code to a new workbook using a macro

Hybrid View

  1. #1
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Add vb code to a new workbook using a macro

        ChDir "L:\tapeaudit"
        Workbooks.OpenText Filename:="L:\tapeaudit\tlmsbiwk.txt", Origin:=437, _
            StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True _
            , Space:=False, Other:=False, FieldInfo:=Array(Array(1, 9), Array(2, 2), _
            Array(3, 1)), TrailingMinusNumbers:=True
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "=IF(COUNTIF(R1C2:R2000C2,RC[-1]),"""",""NOPE"")"
        Range("D1").Select
        Selection.AutoFill Destination:=Range("D1:D2000"), Type:=xlFillDefault
        Range("D1:D2000").Select
        ActiveWorkbook.SaveAs Filename:="L:\tapeaudit\tlmsbiwk.xls", FileFormat:= _
            xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
            , CreateBackup:=False
    End Sub
    This is the code I am using now. I am creating a new workbook with the data from this text file and inputting the formulas that I need. The issue that I am having is that I want to be able to add the code below to the new sheet on it's creation and I have not been able to get it to work. Thanks for your help.

    Option Explicit
    
    Private Declare Function sndPlaySound32 Lib "winmm.dll" _
        Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
        ByVal uFlags As Long) As Long
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    
        If Not Intersect(Target, Range("B:B")) Is Nothing Then
            For Each Cell In Intersect(Target, Range("B:B"))
                If Range("D" & Cell.Row) = "NOPE" Then
                    Call sndPlaySound32("C:\windows\media\notify.wav", 1)
                    Exit Sub
                End If
            Next Cell
        End If
    
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    This macro will copy the contents of a text file to the active sheet code module of the workbook. You will need to copy your macro and save it as a text file. Change the file path in the macro to this location. Call the macro before you close the workbook and save it.
    Sub AddMacroToWorkbook()
    
      Dim MacroFile As String
      Dim TextFile As Object
      Dim VBmod As Object
      Dim VBproj As Object
      Dim Wkb As Workbook
      
        MacroFile = "C:\Sound Macro.txt"     'Change this path and file name to match your file
        Set Wkb = ActiveWorkbook
          
          Set VBproj = Wkb.VBProject
          Set VBmod = VBproj.VBComponents(ActiveSheet.Name).CodeModule
        
          VBmod.AddFromFile MacroFile
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    I run into an issue here. I am creating the new worksheet in my macro and filling it with data and formulas at that step. Looking at this, can I incorporate this into the code at the beginning to do this all in one step?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    I have added the macro call to your code. You will still need to copy and paste your sound macro into Notepad and save it. Once you have done that, you will need to change the path in the macro I wrote to your file's location.
        ChDir "L:\tapeaudit"
        Workbooks.OpenText Filename:="L:\tapeaudit\tlmsbiwk.txt", Origin:=437, _
            StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True _
            , Space:=False, Other:=False, FieldInfo:=Array(Array(1, 9), Array(2, 2), _
            Array(3, 1)), TrailingMinusNumbers:=True
        AddMacroToWorkbook
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "=IF(COUNTIF(R1C2:R2000C2,RC[-1]),"""",""NOPE"")"
        Range("D1").Select
        Selection.AutoFill Destination:=Range("D1:D2000"), Type:=xlFillDefault
        Range("D1:D2000").Select
        ActiveWorkbook.SaveAs Filename:="L:\tapeaudit\tlmsbiwk.xls", FileFormat:= _
            xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
            , CreateBackup:=False
    End Sub

  5. #5
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    I get an error when I try to run that.

    Programmatic access to Visual Basic Project is not trusted

    the debugger then highlights this line
    Set VBproj = Wkb.VBProject

    Thanks for the help.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    If you are running this code on you r own machine, you should not be getting this error. However, if you are attempting to change another workbook on someone else's machine you will.

  7. #7
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    sure the workbook and all of the files are on a shared drive.

  8. #8
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    So I checked the box for trusted access and that step goes okay however now the next step gives an error

    Run-time error '9':
    Subscript out of range
    Set VBmod = VBproj.VBComponents(ActiveSheet.Name).CodeModule

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    The error indicates the name of the module was not found. Which doesn't make sense because a workbook must have at least one worksheet in it and it is looking for the Active Sheet. Can you post the workbook for review?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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