Consulting

Results 1 to 2 of 2

Thread: Getting a total copied set of rows in VBA and storing it in a variable

  1. #1
    VBAX Newbie
    Joined
    Sep 2011
    Posts
    1
    Location

    Getting a total copied set of rows in VBA and storing it in a variable

    Hello,

    I have a fairly simple syntax question:

    I'm trying to copy and paste n rows from one excel file to another. In addition, I'd like to store the total filtered copied rows into a variable. Can someone help me accomplish this?

    For example:

    '1) Activate CSV file, Apply Filter to Column B (Page Title) & uncheck "blanks" ("<>") filter
    Windows("Test_Origin.xlsm").Activate
    ActiveSheet.Range("$A$1:$J$206").AutoFilter Field:=2, Criteria1:="<>"

    '2) Copy Filtered Lines with data (Excluding Row 1)
    Range("B2:F189").Select
    Selection.Copy
    copiedRowTotal = Selection.Rows.Count <-- This doesn't give me the FILTERED rows

    Thanks

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "A beggar of love, second hand hero...King of Dreams"
    Posts
    2,598
    [VBA]
    Sub CountVisibleRows()

    Dim filterRange As Range
    Dim copiedRowTotal As Long

    Set filterRange = ActiveSheet.AutoFilter.Range

    copiedRowTotal = filterRange.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
    '-1 to exclude header row

    MsgBox copiedRowTotal
    'to display the copiedRowTotal value in message box

    End Sub
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •