Consulting

Results 1 to 9 of 9

Thread: Copy Code from Module2 to Module1

  1. #1

    Copy Code from Module2 to Module1


    If i use Sheet Names as Sheet1 Sheet2 ... etc code works good.
    But in my workbooks Sheet named As Sht1,Sht2... & AA1 AA2.
    gived:

    Run-time errer 9
    Subscript out of range

    In Project Windows I see:
    Sheet1(Sht1)
    Sheet2(Sht3)
    Sheet3(Sht2)

    Why dont gived SheetName Sht1 insted Sheet1?

    When I sort this sheets (with other code in the modules) code use names Sht1,Sht2...and works good.

    Hope you understand what i mean.


    [VBA]Sub CopyModule_Sht2_toSht1() 'Selle tööraamatu makro kopib töölehe nr2 makro töölehele nr 1
    Dim wbDest As Workbook, wbSource As Workbook, v_clsDest As VBComponent, v_clsSource As VBComponent, _
    clsM_Dest As CodeModule, clsM_Source As CodeModule, lCnt As Long, lStart As Long, lBeg As Long
    Dim Sht As Worksheet
    Set wbDest = ActiveWorkbook
    Set wbSource = ActiveWorkbook
    Set v_clsDest = wbDest.VBProject.VBComponents("Sht1") 'iF name Sheet1-works good.
    Set v_clsSource = wbSource.VBProject.VBComponents("Sht2") 'iF name Sheet2-works good.
    Set clsM_Dest = v_clsDest.CodeModule
    Set clsM_Source = v_clsSource.CodeModule
    For lCnt = 1 To clsM_Source.CountOfDeclarationLines
    If InStr(1, clsM_Source.Lines(lCnt, 1), _
    "Option Explicit", vbTextCompare) = 0 Then
    lBeg = lBeg + 1
    End If
    Next
    lBeg = clsM_Source.CountOfDeclarationLines - lBeg
    lStart = clsM_Source.CountOfLines
    For lCnt = 1 To clsM_Source.CountOfLines
    clsM_Dest.InsertLines lCnt + lStart, clsM_Source.Lines(lCnt + lBeg, 1)
    Next
    Set wbDest = Nothing
    Set wbSource = Nothing
    Set v_clsDest = Nothing
    Set v_clsSource = Nothing
    Set clsM_Dest = Nothing
    Set clsM_Source = Nothing
    End Sub
    [/VBA]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA] Set v_clsDest = wbDest.VBProject.VBComponents(Sheets("Sht1").CodeName) 'iF name Sheet1-works good.
    Set v_clsSource = wbSource.VBProject.VBComponents(Sheets("Sht2").CodeName) 'iF name Sheet2-works good.[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Thanks mdmackillop. (Very quick & good work).

    In this Sample file (litle part of my Project) the code works good.
    But in my real "BIG Project" this code dont work???

    With button in the “HEAD” File I want refresh File “Sample.
    Here part of this file:

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub CopyModule_Sht2_toSht1() 'Selle tööraamatu makro kopib töölehe nr2 makro töölehele nr 1
    Dim wbDest As Workbook, wbSource As Workbook, v_clsDest As VBComponent, v_clsSource As VBComponent, _
    clsM_Dest As CodeModule, clsM_Source As CodeModule, lCnt As Long, lStart As Long, lBeg As Long
    On Error Resume Next
    Set wbDest = ActiveWorkbook
    If Err.Number > 0 Then
    Set wbDest = Nothing
    Err.Clear
    On Error GoTo 0
    MsgBox "Sheets existing??"
    Exit Sub
    End If
    On Error GoTo 0
    Set wbSource = ThisWorkbook
    Set v_clsDest = wbDest.VBProject.VBComponents(Sheets("Sht1").CodeName)
    Set v_clsSource = wbSource.VBProject.VBComponents(Sheets("Sht2").CodeName)
    Set clsM_Dest = v_clsDest.CodeModule
    Set clsM_Source = v_clsSource.CodeModule
    For lCnt = 1 To clsM_Source.CountOfLines
    If InStr(1, clsM_Source.Lines(lCnt, 1), _
    "Option Explicit", vbTextCompare) = 0 Then
    lBeg = lBeg + 1
    End If
    Next
    lBeg = clsM_Source.CountOfLines - lBeg
    lStart = clsM_Source.CountOfLines
    For lCnt = 1 To clsM_Source.CountOfLines
    clsM_Dest.InsertLines lCnt + lStart, clsM_Source.Lines(lCnt + lBeg, 1)
    Next
    Set wbDest = Nothing
    Set wbSource = Nothing
    Set v_clsDest = Nothing
    Set v_clsSource = Nothing
    Set clsM_Dest = Nothing
    Set clsM_Source = Nothing
    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    In this place gived same error 9

    [VBA]
    Set v_clsDest = wbDest.VBProject.VBComponents(Sheets("Sht1").CodeName)

    Set v_clsDest=Nothing ???
    [/VBA]

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you summarise what your code should be doing?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    If in the Sheet Sht1 or in the Sheet AA1 Range IU1 not empty
    (Sheet completed=full) then insert new Sheet, rename this Sht1 or AA1 & copy worksheetcode Sht2 or AA2 to Sht1 or AA1.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Are you copying code within Head or Sample of from one to the other?

    If your code is simply that shown in Sample Sheet2, you can put code in ThisWorkbook which will handle a DoubleClick event from ant shhet, so no need for code in each sheet.
    [VBA]Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    With Sh
    .Range("A1").Copy .Range("E1")
    .Range("H1").Copy .Range("A2")
    .Range("A1").Activate
    End With
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Thanks mdmackillop.
    That simple code was just excample.
    Real code more complicated.

Posting Permissions

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