+ Reply to Thread
Results 1 to 4 of 4

How can I get macro to recognize every time a number changes?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-28-2003
    Location
    Happyville, USA
    MS-Off Ver
    365
    Posts
    300

    How can I get macro to recognize every time a number changes?

    Hi,

    The code below copies data and places it on it's own sheet. It works great. My problem is I want it take the data and place it on it's own sheet every time the number changes. Example: Right now the below code will take the data for PO # 101012752 and place it on it's own sheet. I want to change the code so it will recognize every time the PO # changes and place it on it's own sheet without me having to go in and manually change the PO # in the code each time. Any thoughts?

    Thank you so much in advance for your helpe.

    Eddie.


    'Copies data onto its own sheet.

        Dim WS As Worksheet
        Dim WSNew As Worksheet
        Dim rng As Range
        Dim rng2 As Range
        Dim Str As String
     
        Set WS = Sheets("Corp Paying")  '<<< Change
        'A1 is the top left cell of your filter range and the header of the first column
        Set rng = WS.Range("A1").CurrentRegion  '<<< Change
        Str = "101012752"  '<<< Change
     
        'Close AutoFilter first
        WS.AutoFilterMode = False
     
        'This example filter on the first column in the range (change the field if needed)
        rng.AutoFilter Field:=1, Criteria1:=Str
     
        Set WSNew = Worksheets.Add
    
        WS.AutoFilter.Range.Copy
        With WSNew.Range("A1")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
            .Select
        End With
     
        '    'If you want to delete the rows in WS that you copy use this also
        '    With WS.AutoFilter.Range
        '        On Error Resume Next
        '        Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
                 '                   .SpecialCells(xlCellTypeVisible)
        '        On Error GoTo 0
        '        If Not rng2 Is Nothing Then rng2.EntireRow.Delete
        '    End With
     
        WS.AutoFilterMode = False
     
        On Error Resume Next
        WSNew.name = Str
        If Err.number > 0 Then
            MsgBox "Change the name of : " & WSNew.name & " manually"
            Err.Clear
        End If
        On Error GoTo 0
        
        End Sub

  2. #2
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Eddie,

    In the VBA Editor you can go to the Sheet where the change will occur. Using the Worksheet_Change you would have it look at the address of the cell where the change occur. In your case you would reference the cell containing the PO #. If that cell changes, call the code for moving the information

    Would look something like the code below where "$A$1" would be the cell where the PO # is located
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Address = "$A$1" Then
            Call Whatever
        End If
    
    End Sub
    You would then set this line of your code to be a cell reference instead of a manual entry.
    Str = "101012752"  '<<< Change
    Does this help any?
    Sincerely,
    Jeff

  3. #3
    Forum Contributor
    Join Date
    10-28-2003
    Location
    Happyville, USA
    MS-Off Ver
    365
    Posts
    300
    Yes it did help. Thank you very much. I'm sorry I forgot to reply quicker than this.

    Eddie.

  4. #4
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Eddie,

    Glad it helped!

+ 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