+ Reply to Thread
Results 1 to 7 of 7

Help moving a VBA code from one workbook to another- with different sheet name

Hybrid View

  1. #1
    Registered User
    Join Date
    07-11-2014
    Location
    Surprise,AZ
    MS-Off Ver
    2010
    Posts
    53

    Help moving a VBA code from one workbook to another- with different sheet name

    i need help using this code in another work book with different name. When I change the name of the sheet 2 to the new sheet of Table- it doent work the cells go blank and code doesn;t work. Can someone tell me what I'm doing wrong?


    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range, LR As Long, NR1 As Long, NR2 As Long
    
    If Not Intersect(Target, Range("E4")) Is Nothing Then
        Application.EnableEvents = False
        Range("E21:N30").ClearContents
        NR1 = 21
        NR2 = 21
        With Sheets("Sheet2")
            .AutoFilterMode = False
            .Rows(1).AutoFilter 3, "*" & Range("E4") & "*"
            LR = .Range("C" & .Rows.Count).End(xlUp).Row
            If LR > 1 Then
                For Each cell In .Range("M2:M" & LR).SpecialCells(xlVisible)
                    Select Case UCase(cell.Value)
                        Case "ME"
                            Range("E" & NR1).Value = .Cells(cell.Row, "K").Value
                            Range("H" & NR1).Value = .Cells(cell.Row, "B").Value
                            Range("I" & NR1).Value = .Cells(cell.Row, "I").Value
                            NR1 = NR1 + 1
                        Case "THEM"
                            Range("J" & NR2).Value = .Cells(cell.Row, "K").Value
                            Range("M" & NR2).Value = .Cells(cell.Row, "B").Value
                            Range("N" & NR2).Value = .Cells(cell.Row, "I").Value
                            NR2 = NR2 + 1
                    End Select
                Next cell
            Else
                Range("E21") = "none"
            End If
            .AutoFilterMode = False
        End With
        Application.EnableEvents = True
    End If
    End Sub
    Last edited by air044; 12-02-2014 at 12:06 PM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Help moving a VBA code from one workbook to another- with different sheet name

    Please add code tags as per the forum rules.

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Help moving a VBA code from one workbook to another- with different sheet name

    Hi there,

    I'm trying to figure out what might (or might not!) be happening at your end.

    What do you mean when you say the cells "go blank"? Are the values cleared from all of the cells? Does the AutoFilter hide all of the data rows? Does something else happen?

    The Worksheet_Change event is related to the worksheet which contains the Worksheet_Change routine in its VBA CodeModule - have you entered a copy of your code in the CodeModule for the "Table" worksheet?

    You don't need to refer to a worksheet by its Name when the code involved is contained in the CodeModule of that worksheet, so instead of using:
    With Sheets("Sheet2")
    you can use:
    With Me
    Hope the above helps - please let me know how you get on.

    Regards,

    Greg M

  4. #4
    Registered User
    Join Date
    07-11-2014
    Location
    Surprise,AZ
    MS-Off Ver
    2010
    Posts
    53

    Re: Help moving a VBA code from one workbook to another- with different sheet name

    works normal until i change the sheet title to TABLE, then i get Debug error:



    Then after hitting debug I get code error:




    and the cells in HQ table no longer pulls data from sheet 2(Table)?? help??

    debug.pngcode.jpg

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range, LR As Long, NR1 As Long, NR2 As Long
    
    If Not Intersect(Target, Range("E4")) Is Nothing Then
        Application.EnableEvents = False
        Range("E21:N30").ClearContents
        NR1 = 21
        NR2 = 21
        With Sheets("LOG")
            .AutoFilterMode = False
            .Rows(1).AutoFilter 3, "*" & Range("E4") & "*"
            LR = .Range("C" & .Rows.Count).End(xlUp).Row
            If LR > 1 Then
                For Each cell In .Range("M2:M" & LR).SpecialCells(xlVisible)
                    Select Case UCase(cell.Value)
                        Case "ME"
                            Range("E" & NR1).Value = .Cells(cell.Row, "K").Value
                            Range("H" & NR1).Value = .Cells(cell.Row, "B").Value
                            Range("I" & NR1).Value = .Cells(cell.Row, "I").Value
                            NR1 = NR1 + 1
                        Case "THEM"
                            Range("J" & NR2).Value = .Cells(cell.Row, "K").Value
                            Range("M" & NR2).Value = .Cells(cell.Row, "B").Value
                            Range("N" & NR2).Value = .Cells(cell.Row, "I").Value
                            NR2 = NR2 + 1
                    End Select
                Next cell
            Else
                Range("E21") = "none"
            End If
            .AutoFilterMode = False
        End With
        Application.EnableEvents = True
    End If
    End Sub

    This above is how ther error shows in VB " With Sheets ("Log") highlighted yellow. I tried your fix but it didn't work.
    Last edited by air044; 12-02-2014 at 02:54 PM.

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Help moving a VBA code from one workbook to another- with different sheet name

    Hi again,

    I wasn't able to view either the .png or the .jpg file you posted.

    Can you post some/all of your workbook here so we can get a better understanding of how it's supposed to work?

    Regards,

    Greg M

  6. #6
    Registered User
    Join Date
    07-11-2014
    Location
    Surprise,AZ
    MS-Off Ver
    2010
    Posts
    53

    Re: Help moving a VBA code from one workbook to another- with different sheet name

    [QUOTE=Greg M;3917451]Hi again,

    I wasn't able to view either the .png or the .jpg file you posted.

    Can you post some/all of your workbook here so we can get a better understanding of how it's supposed to work?

    Regards,

    Greg M[/QUOTE
    Last edited by air044; 12-02-2014 at 07:03 PM.

  7. #7
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Help moving a VBA code from one workbook to another- with different sheet name

    Hi again,

    The following code appears to work in the attached copy of the workbook you posted earlier:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range, LR As Long, NR1 As Long, NR2 As Long
    
    If Not Intersect(Target, Range("E4")) Is Nothing Then
        Application.EnableEvents = False
        Range("E21:N30").ClearContents
        NR1 = 21
        NR2 = 21
        With Sheets("TABLE")
            .AutoFilterMode = False
            .Rows(1).AutoFilter 3, "*" & Range("E4") & "*"
            LR = .Range("C" & .Rows.Count).End(xlUp).Row
            If LR > 1 Then
                For Each cell In .Range("M2:M" & LR).SpecialCells(xlVisible)
                    Select Case UCase(cell.Value)
                        Case "ME"
                            Range("E" & NR1).Value = .Cells(cell.Row, "K").Value
                            Range("H" & NR1).Value = .Cells(cell.Row, "B").Value
                            Range("I" & NR1).Value = .Cells(cell.Row, "I").Value
                            NR1 = NR1 + 1
                        Case "THEM"
                            Range("J" & NR2).Value = .Cells(cell.Row, "K").Value
                            Range("M" & NR2).Value = .Cells(cell.Row, "B").Value
                            Range("N" & NR2).Value = .Cells(cell.Row, "I").Value
                            NR2 = NR2 + 1
                    End Select
                Next cell
            Else
                Range("E21") = "none"
            End If
            .AutoFilterMode = False
        End With
        Application.EnableEvents = True
    End If
    End Sub
    Hope this helps - as always, please let me know how you get on.

    Regards,

    Greg M


    P.S. Many thanks for the Reputation increase
    Attached Files Attached Files
    Last edited by Greg M; 12-02-2014 at 09:17 PM. Reason: P.S. added

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 2
    Last Post: 10-09-2012, 08:40 PM
  2. Moving a sheet module from one workbook to another
    By bobaftt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2011, 10:01 AM
  3. moving a sheet to new workbook
    By share knowledge in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-26-2011, 08:16 AM
  4. Moving a protected sheet to a different workbook
    By TPD in forum Excel General
    Replies: 0
    Last Post: 02-13-2007, 10:59 AM
  5. Can I prevent a sheet from moving within a workbook?
    By JDGUILTY in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2005, 02:06 PM

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