+ Reply to Thread
Results 1 to 3 of 3

Fixing Macro to work in active sheets

  1. #1
    Registered User
    Join Date
    12-29-2014
    Location
    Charlotte, NC
    MS-Off Ver
    Windows 7 Professional
    Posts
    4

    Fixing Macro to work in active sheets

    I am trying to fix one of my macros so that it works in active sheet. It used to have to be changed for each sheet I wanted to run it on but now I am getting runtime errors saying Compile Error: Argument not optional. I have highlighted the 3 places that I switched it to be looking for the active workbook. I have attached the macro below and bold/underline the sections causing the error - Any suggestions?



    ___________________________________________
    Sub CopyData()


    Dim vSearch As Variant

    Dim i As Long
    Dim k As Long
    Dim lRowToCopy As Long

    'Tells user to enter a search phrase
    vSearch = InputBox("Search")
    'makes sure to search from 1st row
    i = 1
    'macro will loop until it finds a row with no records
    'if the macro finds a row with no records it quits the loop
    Do Until WorksheetFunction.CountA.ActiveWorkbook.ActiveSheet.Rows(i) = 0

    'here I let the macro to continue its run despite a possible errors (explanation below)
    On Error Resume Next
    lRowToCopy = 0
    'if Find method finds no value VBA returns an error, this is why I allowed macro to run despite that. In case of error variable lRowToCopy keeps 0 value
    'if Find method finds a searched value it assigns the row number to var lRowToCopy
    lRowToCopy = ActiveWorkbook.ActiveSheet.Rows(i).Find(What:=vSearch, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows).Row
    'here we allow macro to disiplay error messages
    On Error GoTo 0

    'if var lRowToCopy does not equal to 0 that means a row with a searched value has been found
    If lRowToCopy > 0 Then

    'this loop looks for the first blank row in 2nd sheet, I also used COUNTA to find absolutely empty row
    For k = 1 To Sheets("ToCopy").Rows.Count

    'when the row is found, the macro performs copy-paste operation
    If WorksheetFunction.CountA(Sheets("ToCopy").Rows(k)) = 0 Then

    ActiveWorkbook.ActiveSheet.Rows(i).Copy
    Sheets("ToCopy").Select
    Rows(k).Select
    ActiveSheet.Paste
    'do not forget to exit for loop as it will fill all empty rows in 2nd sheet
    Exit For

    End If
    Next k

    End If

    i = i + 1
    Loop

    End Sub
    Last edited by PWilson0727; 02-04-2015 at 05:10 PM.

  2. #2
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Fixing Macro to work in active sheets

    try adding brackets as per below:

    Please Login or Register  to view this content.
    Please consider adding a * if I helped.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Fixing Macro to work in active sheets

    You are changing the active sheet here.
    Please Login or Register  to view this content.
    Try setting a reference to the active sheet at the start of the code.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

+ 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] Why wont my search function work when i change it from all sheets to active sheet?
    By joshnathan in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-03-2012, 12:58 PM
  2. Macro to collate data from different work books and different work sheets
    By bvdileep in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-04-2012, 05:34 AM
  3. Make Macro work accross all active (grouped) sheets
    By CRIMEDOG in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-27-2009, 01:44 PM
  4. [SOLVED] How to split the active sheets into two different work windows?
    By NiNi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-28-2006, 02:50 PM
  5. Macro doesn't work if on wrong active sheet
    By shternm in forum Excel General
    Replies: 1
    Last Post: 02-02-2005, 09:05 PM

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