+ Reply to Thread
Results 1 to 14 of 14

Stop Macro From Falling Over

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Stop Macro From Falling Over

    This macro works fine but falls over when it can't find a match can anyone advise how I can get around that please? I have highlighted in bold

    Thanks

    Sub Test()
    Dim Alpha
    Dim Alpha1
    Dim Bravo
    Dim CopyData
    Dim RowNo
    
        Windows("MASTER_SCHEDULE.XLS").Activate
        Alpha = ActiveWorkbook.Name
        Sheets("Master").Select
        Range("X5").Select
        Alpha1 = ActiveCell.Value
        CopyData = ActiveCell.Offset(0, -1).Value
        
        'Range(ActiveCell, ActiveCell.Offset(0, -1)).Copy
      
        Windows("FW_KP 2011 TITLES_190911 VS1.XLS").Activate
        Bravo = ActiveWorkbook.Name
        Sheets("Link To Cinc").Activate
        Range("A:A").Select
        
        Selection.Find(What:=Alpha1, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        
        ActiveCell.Offset(0, 2).Range("A1") = CopyData
        RowNo = 1
        
        Do Until Finish = True
        Windows(Alpha).Activate
        CopyData = ActiveCell.Offset(RowNo, -1).Value
        Alpha1 = ActiveCell.Offset(RowNo, 0).Value
        Windows(Bravo).Activate
        
        Selection.Find(What:=Alpha1, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        
        ActiveCell.Offset(0, 2).Range("A1") = CopyData
        RowNo = RowNo + 1
        If Alpha1 = "" Then Finish = True
        Loop
    End Sub
    Last edited by timbo1957; 10-13-2011 at 10:46 AM.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Stop Macro From Falling Over

    You need to define a variable as a range, set the value of that variable to equal the results of the .Find operation (rather than trying to select the results directly) and then test for that variable being Nothing, e.g.

    Dim rngResult As Range
    
    Set rngResult=Sheets("Link To Cinc").Columns(1).Find(Alpha1, LookIn:=xlFormulas, LookAt:=xlPart, MatchCase:=False)
    
    If rngResult Is Nothing Then
      'No match found
    Else
      'Do something
    End If

    You could really do with revisiting your code - in VBA it's rarely necessary to select or activate anything, you can just tell Excel which object you're referring to - i.e.

    Alpha1=Workbooks("Master_Schedule.xls").Sheets("Master").Range("X5").Value

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Stop Macro From Falling Over

    Thanks Andrew-R,

    A great help.

  4. #4
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Stop Macro From Falling Over

    Hi Andrew-R,

    You helped me out a couple of days ago and I was wondering if you could help again today?

    I am using this code to delete all rows that don't have the letter "A" in column AN. How can I expand this to enable me to delete all rows that don't have the letter "A" or "n/a" in column AN.

    Many thanks

        Firstrow = 5
        LastRow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1
        With ActiveSheet
            .DisplayPageBreaks = False
            For lRow = LastRow To Firstrow Step -1
                If .Cells(lRow, "AN").Value = "A" Then
                    'Do nothing, This avoid a error if there is a error in the cell
     
                ElseIf .Cells(lRow, "AN").Value <> "A" Then .Rows(lRow).Delete
                    'This will delete each row with the Value "ron" in Column A, case sensitive.
     
                End If
            Next
        End With
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Stop Macro From Falling Over

    Is the data in a Table format that you can Autofilter
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Stop Macro From Falling Over

    Hi Roy,

    Yes but I have never used Autoformat in a macro.

    Basically what I am trying to do is -

    Wkbk1 if the value in column AN is "A" or "n/a" then go to the reference number in column X.
    offset that cell by one column to the left (W) and copy that value

    Switch to Wkbk2 Select column A and find the reference number from Column X in Wkbk1
    when the macro finds the ref no paste the copied cell offset 2 cells to the right

    The macro does that okay but I have the anomaly of what to do if the ref number in wkbk1 doesn't exist in wkbk2 - I'd like it copied to the bottom of the data in row A.

    Any help appreciated.

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Stop Macro From Falling Over

    Try recording a macro, then it can be edited

  8. #8
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Stop Macro From Falling Over

    Hi Roy,

    This works down to the bit in bold where it needs adapting to allow rows containging "n/a" to remain and not be deleted.

        Dim Firstrow As Long
        Dim LastRow As Long
        Dim lRow As Long
        Dim CalcMode As Long
     
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
     
        Firstrow = 5
        LastRow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1
        With ActiveSheet
            .DisplayPageBreaks = False
            For lRow = LastRow To Firstrow Step -1
                If .Cells(lRow, "AN").Value = "A" Then
                    'Do nothing, This avoid a error if there is a error in the cell
     
                ElseIf .Cells(lRow, "AN").Value <> "A" Then .Rows(lRow).Delete
                    'This will delete each row with the Value "ron" in Column A, case sensitive.
     
                End If
            Next
        End With
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
    End Sub

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Stop Macro From Falling Over

    Sub snb()
     With activeSheet.UsedRange.Columns(1)
       .AutoFilter 1, "ron"
       .Offset(1).SpecialCells(2).EntireRow.Delete
       .AutoFilter
     End With
    End Sub



  10. #10
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Stop Macro From Falling Over

    Hi snb,

    Can you explain where I should put this in the current macro and how it works please?

    Thanks

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Stop Macro From Falling Over

    It's a replacement of your whole macro........
    If you try to understand the code, you'll see why & how it works.

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Stop Macro From Falling Over

    Attach a dummy workbook

  13. #13
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Stop Macro From Falling Over

    Hi Roy,

    I have cut out all of the info except column AN where I want to delete all rows except those with either "A" or "n/a".

    Tim.
    Attached Files Attached Files

+ 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