+ Reply to Thread
Results 1 to 3 of 3

Replacing ActiveSheet... with myActiveSheet

Hybrid View

  1. #1
    Registered User
    Join Date
    02-07-2019
    Location
    Switzerland
    MS-Off Ver
    Office 365 Mac
    Posts
    23

    Replacing ActiveSheet... with myActiveSheet

    I have several macros and functions that are using ActiveSheet.... and want to replace ActiveSheet... with myActiveSheet. But I always get either compile- or runtime errors.

    
    Sub Main()
    
    Dim myActiveSheet As Worksheet
    Dim UsedRangeRows As Long
    
    myActiveSheet = createTestSheet("Basis Depot") 
    UsedRangeRows = myActiveSheet.UsedRange.Rows.Count 
    
    ...
    
    End Sub
    
    Public Function createTestSheet(Optional myTestSheet As String) As Worksheet
    
    If sheetExists("myTestSheet") Then
              Application.DisplayAlerts = False
              Sheets("myTestSheet").Delete
              Application.DisplayAlerts = True
              End If
    
    If Not myTestSheet = Empty Then
              Sheets(myTestSheet).Activate
              End If
              
    ActiveSheet.Copy After:=ActiveSheet
    ActiveSheet.Name = "myTestSheet"
    Set createTestSheet = ActiveSheet
    
    End Function

  2. #2
    Valued Forum Contributor dotchiejack's Avatar
    Join Date
    05-21-2015
    Location
    Antwerp,Belgium
    MS-Off Ver
    2016
    Posts
    507

    Re: Replacing ActiveSheet... with myActiveSheet

    Add this code to your macro's under the declarations.
    Set myActivesheet = Sheets("Basis Depot")
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    02-07-2019
    Location
    Switzerland
    MS-Off Ver
    Office 365 Mac
    Posts
    23

    Re: Replacing ActiveSheet... with myActiveSheet

    "Basis Depot" is just a sample value for the createTestSheet function which creates a working copy of this and activates the working copy. The createTestSheet function is placed in my .xlam workbook. The createTestSheet function returns the ActiveSheet "Public Function createTestSheet(Optional myTestSheet As String) As Worksheet" which should be stored in the variable myActiveSheet of the calling macro and should be used in the rest of the local macros.

+ 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. ActiveWorkBook.ActiveSheet vs ActiveSheet
    By bvwalker1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2019, 12:10 PM
  2. [SOLVED] ActiveSheet.Name = ActiveSheet.Range("G1"), Worksheet_Calculate Event not working, 1004
    By xlyfe in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-28-2018, 11:24 AM
  3. [SOLVED] Undo "ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value" after copy
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2016, 06:35 AM
  4. Replacing strings/ replacing numbers and letters
    By Insert Name in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-16-2015, 01:15 PM
  5. ActiveSheet.PivotTables => How to do it for custom sheet? (replace ActiveSheet)
    By chrisignm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-03-2014, 03:22 PM
  6. Replies: 3
    Last Post: 03-08-2013, 09:28 AM
  7. Copying new activesheet after other activesheet is hidden?
    By Simon Lloyd in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-20-2006, 05:10 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