+ Reply to Thread
Results 1 to 3 of 3

Combining two macros - why won't they work?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-22-2014
    Location
    London
    MS-Off Ver
    2010
    Posts
    30

    Combining two macros - why won't they work?

    Hi all,

    I'd appreciate you thought on the attached sheet. I have two macros. A worksheet macro that filters the table according to cell M1. And, in addition to that, I have a macro that sorts the table simply by click on the header cell. Both work separately, however, I need both to work in unison so that I have a filter and and sorting functionality.

    Please let me know your thoughts, any help would be appreciated!

    Thanks,

    Mike
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Combining two macros - why won't they work?

    Only One Macro on this workbook and it only runs if M1 is changed
    Last edited by mehmetcik; 08-31-2014 at 05:24 PM.

  3. #3
    Registered User
    Join Date
    07-22-2014
    Location
    London
    MS-Off Ver
    2010
    Posts
    30

    Re: Combining two macros - why won't they work?

    Hi mehmetcik,

    Thanks for looking! Sorry, you're right, the other is a module, which obviously hasn't carried through with the worksheet. Please see the additonal modules I am using below.

    Sub SetupOneTime()
    
    'adds rectangle at top of each column
    'code written by Dave Peterson 2005-10-22
      Dim myRng As Range
      Dim myCell As Range
      Dim curWks As Worksheet
      Dim myRect As Shape
      Dim iCol As Integer
      Dim iFilter As Integer
      iCol = 12  'number of columns
    ' 2010-Oct-31 added space for autofilter dropdowns
    ' set iFilter to 0 if not using autofilter
      iFilter = 12 'width of drop down arrow
      
      Set curWks = ActiveSheet
    
      With curWks
          
        Set myRng = .Range("a1").Resize(1, iCol)
        For Each myCell In myRng.Cells
            With myCell
              Set myRect = .Parent.Shapes.AddShape _
                  (Type:=msoShapeRectangle, _
                  Top:=.Top, Height:=.Height, _
                  Width:=.Width - iFilter, Left:=.Left)
            End With
            With myRect
              .OnAction = ThisWorkbook.Name & "!SortTable"
    ''        2010-Oct-31 revised to fill shapes in Excel 2007
    ''          .Fill.Visible = False
              .Fill.Solid
              .Fill.Transparency = 1#
              .Line.Visible = False
            End With
        Next myCell
      End With
    End Sub
    And the second part (a separate module), I have highlighted in red the line of code that VBA editor flags as problematic. As said, these two work fine on their own, however this issue occurs when trying to run them in the attached sheet (which has an additional worksheet macro):

    Sub SortTable()
      'code written by Dave Peterson 2005-10-22
      '2006-08-06 updated to accommodate hidden rows
      Dim myTable As Range
      Dim myColToSort As Long
      Dim curWks As Worksheet
      Dim mySortOrder As Long
      Dim FirstRow As Long
      Dim TopRow As Long
      Dim LastRow As Long
      Dim iCol As Integer
      Dim strCol As String
      Dim rng As Range
      Dim rngF As Range
    
      TopRow = 1
      iCol = 7  'number of columns in the table
      strCol = "A"  ' column to check for last row
    
      Set curWks = ActiveSheet
    
      With curWks
        LastRow = .Cells(.Rows.Count, strCol).End(xlUp).Row
        If Not .AutoFilterMode Then
            Set rng = .Range(.Cells(TopRow, strCol), _
                    .Cells(LastRow, strCol))
        Else
            Set rng = .AutoFilter.Range
        End If
        
        Set rngF = Nothing
        On Error Resume Next
        With rng
           'visible cells in first column of range
           Set rngF = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
               .SpecialCells(xlCellTypeVisible)
        End With
        On Error GoTo 0
        
        If rngF Is Nothing Then
             MsgBox "No visible rows. Please try again."
             Exit Sub
        Else
             FirstRow = rngF(1).Row
        End If
            
        myColToSort = .Shapes(Application.Caller).TopLeftCell.Column
        
        Set myTable = .Range(strCol & TopRow & ":" _
            & strCol & LastRow).Resize(, iCol)
        If .Cells(FirstRow, myColToSort).Value _
          < .Cells(LastRow, myColToSort).Value Then
            mySortOrder = xlDescending
        Else
            mySortOrder = xlAscending
        End If
        
        myTable.Sort key1:=.Cells(FirstRow, myColToSort), _
                  order1:=mySortOrder, _
                  header:=xlYes
      End With
    
    End Sub
    Any advice would be greatly appreciated!

    Thanks,

    Mike

+ 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. [SOLVED] Macro Doesn't Work Through Button, Does Work Through Developer ->Macros Option
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-27-2013, 11:55 AM
  2. Macros stop to work when work sheet is protected. Run time error 1004
    By sellim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2012, 01:14 AM
  3. [SOLVED] Combining Macros
    By Clue_Less in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-13-2012, 01:46 PM
  4. Combining Macros
    By rocketboy13 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-01-2011, 02:45 PM
  5. [SOLVED] Combining macros
    By GarToms in forum Excel General
    Replies: 2
    Last Post: 02-09-2006, 08:35 AM

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