Share via

CTRL-F in VBA but want to skip something missing

Anonymous
2020-12-09T17:09:34+00:00

Not sure I worded the subject line properly. What I have is this:

Cells.Find(What:="Jan total", After:=ActiveCell, LookIn:=xlFormulas2, _

        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _

        MatchCase:=False, SearchFormat:=False).Activate

    ActiveCell.EntireRow.Select

    Selection.Copy

    Selection.End(xlDown).Select

    ActiveCell.Offset(2, 0).Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

        :=False, Transpose:=False

I want to search the spreadsheet for "Jan Total" all the way to "Dec Total". Then I copy the row and paste it down. The problem is, sometimes a month could be missing. I can create one example of above for each month but if APR is missing, then the code errors out. 

How can I run this so it will skip any month that is missing? All of the "xxx Total" are in column A, if that helps. and I would need to change the ActiveCell.Offset by one for each month, so it would always copy below. I have Jan Total set to offset at 2,0, then Feb at 3,0 and so on.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

7 answers

Sort by: Most helpful
  1. Anonymous
    2020-12-09T21:12:49+00:00

    Hi Darrend

    Try the following code

    '''*********************************************************

    Sub CopyTotals()

    Dim TotalsRange As Range

    Dim pasteRow As Long

    With Sheets("Sheet1")

       '''To make sure, monthly subtotals levels (2) is selected

        .Outline.ShowLevels RowLevels:=2

        Set TotalsRange = .Range("A1").CurrentRegion.Offset(1).SpecialCells(xlCellTypeVisible)

        pasteRow = Cells(Rows.Count, "A").End(xlUp).Row + 2

        TotalsRange.Copy

        Cells(pasteRow, "A").PasteSpecial Paste:=xlPasteValues

        Cells(pasteRow, "A").Select

        Application.CutCopyMode = False

    End With

    End Sub

    ''''************************************************

    Do let me know if you need more help

    Regards

    Jeovany

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2020-12-10T11:37:57+00:00

    The reason I am doing this is so I can then copy all the data from the total rows at one time and paste into another workbook. If I try to do it on this grouped rows, I wind up getting all of the data in the group and all I want is the Total's rows. Unless I did one row at a time of course.

    Right-click on the sheet tab

    Choose "View Code"

    Within the menu click Insert \ Module

    Paste in the code below

    Close the VBA editor

    Press Alt-F8

    Choose a macro

    Click Run

    Notes:

    a) I made 3 main routines as example, choose the one you like. They work the way they sound.

    b) It doesn't matter for my code if the rows are grouped or not.

    c) A comment to your macro:

    Please never use SELECT, SELECTION, ACTIVECELL, it is slow and error prone. Always refer to the objects directly.

    Andreas.

    Option Explicit

    Sub CopyBelowTheData()

      Dim All As Range, Dest As Range

      'Find all Total rows

      Set All = FindAll(Range("A:A"), "Total", LookAt:=xlPart)

      'Find an empty cell below the data

      Set Dest = Range("A" & Rows.Count).End(xlUp).Offset(2)

      'Copy the rows

      All.EntireRow.Copy

      'Paste as Value

      Dest.PasteSpecial xlPasteValues

      'Cancel the copy

      Application.CutCopyMode = False

    End Sub

    Sub CopyToNewSheet()

      Dim All As Range, Dest As Range

      Dim Ws As Worksheet

      'Find all Total rows

      Set All = FindAll(Range("A:A"), "Total", LookAt:=xlPart)

      'Create a new sheet

      Set Ws = Worksheets.Add(After:=Sheets(Sheets.Count))

      'The destination is A1

      Set Dest = Ws.Range("A1")

      'Copy the rows

      All.EntireRow.Copy

      'Paste as Value

      Dest.PasteSpecial xlPasteValues

      'Cancel the copy

      Application.CutCopyMode = False

    End Sub

    Sub CopyToNewFile()

      Dim All As Range, Dest As Range

      Dim Wb As Workbook

      'Find all Total rows

      Set All = FindAll(Range("A:A"), "Total", LookAt:=xlPart)

      'Create a new file with one sheet

      Set Wb = Workbooks.Add(xlWBATWorksheet)

      'The destination is A1

      Set Dest = Wb.Worksheets(1).Range("A1")

      'Copy the rows

      All.EntireRow.Copy

      'Paste as Value

      Dest.PasteSpecial xlPasteValues

      'Cancel the copy

      Application.CutCopyMode = False

    End Sub

    Private Function FindAll(ByVal Where As Range, ByVal What, _

        Optional ByVal After As Variant, _

        Optional ByVal LookIn As XlFindLookIn = xlValues, _

        Optional ByVal LookAt As XlLookAt = xlWhole, _

        Optional ByVal SearchOrder As XlSearchOrder = xlByRows, _

        Optional ByVal SearchDirection As XlSearchDirection = xlNext, _

        Optional ByVal MatchCase As Boolean = False, _

        Optional ByVal SearchFormat As Boolean = False) As Range

      'Find all occurrences of What in Where (Windows version)

      Dim FirstAddress As String

      Dim C As Range

      'From FastUnion:

      Dim Stack As New Collection

      Dim Temp() As Range, Item

      Dim i As Long, j As Long

      If Where Is Nothing Then Exit Function

      If SearchDirection = xlNext And IsMissing(After) Then

        'Set After to the last cell in Where to return the first cell in Where in front if _

          it match What

        Set C = Where.Areas(Where.Areas.Count)

        'BUG in XL2010: Cells.Count produces a RTE 6 if C is the whole sheet

        'Set After = C.Cells(C.Cells.Count)

        Set After = C.Cells(C.Rows.Count * CDec(C.Columns.Count))

      End If

      Set C = Where.Find(What, After, LookIn, LookAt, SearchOrder, _

        SearchDirection, MatchCase, SearchFormat:=SearchFormat)

      If C Is Nothing Then Exit Function

      FirstAddress = C.Address

      Do

        Stack.Add C

        If SearchFormat Then

          'If you call this function from an UDF and _

            you find only the first cell use this instead

          Set C = Where.Find(What, C, LookIn, LookAt, SearchOrder, _

            SearchDirection, MatchCase, SearchFormat:=SearchFormat)

        Else

          If SearchDirection = xlNext Then

            Set C = Where.FindNext(C)

          Else

            Set C = Where.FindPrevious(C)

          End If

        End If

        'Can happen if we have merged cells

        If C Is Nothing Then Exit Do

      Loop Until FirstAddress = C.Address

      'FastUnion algorithm © Andreas Killer, 2011:

      'Get all cells as fragments

      ReDim Temp(0 To Stack.Count - 1)

      i = 0

      For Each Item In Stack

        Set Temp(i) = Item

        i = i + 1

      Next

      'Combine each fragment with the next one

      j = 1

      Do

        For i = 0 To UBound(Temp) - j Step j * 2

          Set Temp(i) = Union(Temp(i), Temp(i + j))

        Next

        j = j * 2

      Loop Until j > UBound(Temp)

      'At this point we have all cells in the first fragment

      Set FindAll = Temp(0)

    End Function

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2020-12-09T22:48:37+00:00

    I don't know why but the file I have on my side has

    14429 Rows of raw Data

    14442 rows including the Subtotals and Grand Totals rows.

    14444 is the Output row as per your requirements (2 in the picture)

    This is a picture after running the macro in my previous reply

    Image

    If you want to copy and paste the values in another workbook as you mentioned above we can tweak the code to do so.

    Here a link to the file with the macro

    https://www.dropbox.com/s/opj2qdm1turosj6/SummaryMacro-Answer.xlsm?dl=0

    Below also a picture from a file with a Power Query alternative solution to your problem 

    Image

    Here the link to that file too

    https://www.dropbox.com/s/dhhfxr1gnn3f9mj/SummaryMacro-PQ%20Answer.xlsm?dl=0

    I hope this helps you

    Do let me know if you need more help

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. Anonymous
    2020-12-09T19:50:15+00:00

    I have copied my file to my OneDrive public folder.

    https://1drv.ms/x/s!ArsWF46XYoZcm0zRJhhZd\_4oXpT7?e=wxNime

    Was this answer helpful?

    0 comments No comments
  5. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2020-12-09T17:17:07+00:00

    It is far too complicated to recreate such a scenario. And if it does not match yours, then our solution will not work for you.

    For this kind of requests, please create a sample file with the layout of your original file, filled with sample data and colored cells with the expected result.

    At best make a copy of your original file and anonymize the necessary data. For this please download this file

    https://www.dropbox.com/s/rkfxuh85j5wyj9y/modAnonymize.bas?dl=1

    Open your Excel file

    Right-click on the sheet tab

    Choose "View Code"

    Press CTRL-M

    Select the downloaded file and import

    Close the VBA editor

    Select the cells with the confidential data

    Press Alt-F8

    Choose the macro Anonymize

    Click Run

    Upload it on OneDrive (or an other Online File Hoster of your choice) and post the download link here.

    https://support.office.com/en-us/article/Share-OneDrive-files-and-folders-9fcc2f7d-de0c-4cec-93b0-a82024800c07

    Then we can look at the file and try to find a solution. Thank you for your understanding.

    Andreas.

    Was this answer helpful?

    0 comments No comments