+ Reply to Thread
Results 1 to 2 of 2

need to change a macro that works only for a specific worksheet to work for others, also

Hybrid View

  1. #1
    Registered User
    Join Date
    04-07-2013
    Location
    San Diego, USA
    MS-Off Ver
    Excel 2011
    Posts
    5

    need to change a macro that works only for a specific worksheet to work for others, also

    I need to change a macro that works only for a specific worksheet to work for each among other worksheets individually [not sequentially], e.g.. to run in a worksheet with a different name like a modified copy of the original worksheet.
    I'm working in Excel 2011. The following code runs in a worksheet called NOW. What's the easiest way to get it to run in all worksheets? I've looked around & tried various things but nothing has worked yet. I'm a super-newbie. Note: the comments are not specifically accurate because I edited the copied code to make it work for me but not the comments in case I needed to backtrack. [I got the original code here & it was a lifesaver]. Thanks!!! Shawn
    Sub HistoricalData()
    Dim TargetSht As Worksheet, SourceSht As Worksheet, SourceCol As Integer, SourceCells As Range
    
    
    'If an error occurs skip code to the Err-Hanlder line and the display the error message.
    On Error GoTo Err_Handler
    
    
    'This is the sheet where your copy information from. Change "Sheet1" to the name of your soure sheet
    Set SourceSht = ThisWorkbook.Sheets("NOW")
    
    
    'Name of the sheet where data is to be copied to. Rename Sheet2 to the name of your target sheet
    Set TargetSht = ThisWorkbook.Sheets("NOW")
    
    
    'This is the cells you will copy data from. This is targeting cells B1 to the last used cell in column B
    Set SourceCells = SourceSht.Range("e20:e30")
    
    
    'This is finding the next column available in the target sheet. It assumes dates will be in row 1 and data in row 2 down
    If TargetSht.Range("A1").Value = "" Then
    'Cell A1 is blank so the column to put data in will be column #1 (ie A)
    SourceCol = 1
    ElseIf TargetSht.Range("IV1").Value <> "" Then
    'Cell IV1 has something in it so we have reached the maximum number of columns we can use in this sheet.
    'Dont paste the data but advise the user.
    MsgBox "There are no more columns available in the sheet " & TargetSht.Name, vbCritical, "No More Data Can Be Copied"
    'stop the macro at this point
    Exit Sub
    Else
    'cell A1 does have data and we havent reached the last column yet so find the next available column
    SourceCol = TargetSht.Range("IV1").End(xlToLeft).Column + 1
    End If
    
    
    'Put in the date in the appropriate column in row 1 of the target sheet
    TargetSht.Cells(1, SourceCol).Value = Format(Date, "mm/dd/YYYY")
    
    
    'We can now start copying data. This will copy the cells in column B from the source sheet to row 2+ in the target sheet
    SourceCells.Copy TargetSht.Cells(4, SourceCol)
    
    
    'Advise the user that the process was successful
    MsgBox "Data copied successfully!", vbInformation, "Process Complete"
    
    
    Exit Sub 'This is to stop the procedure so we dont display the error message every time.
    Err_Handler:
    MsgBox "The following error occured:" & vbLf & "Error #: " & Err.Number & vbLf & "Description: " & Err.Description, _
    vbCritical, "An Error Has Occured", Err.HelpFile, Err.HelpContext
    
    
    
    
    End Sub

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: need to change a macro that works only for a specific worksheet to work for others, al

    Quote Originally Posted by sdsaavedra View Post
    What's the easiest way to get it to run in all worksheets?
    The code is not dependent on File or sheetnames:

    Sub HistoricalData()
        Dim TargetSht As Worksheet, SourceSht As Worksheet, SourceCol As Integer, SourceCells As Range
        Dim xlWs As Worksheet
        
        'If an error occurs skip code to the Err-Hanlder line and the display the error message.
        On Error GoTo Err_Handler
        
        For Each xlWs In ThisWorkbook.Worksheets        'loop al sheets
        
            'This is the sheet where your copy information from. Change "Sheet1" to the name of your soure sheet
            Set SourceSht = xlWs
            
            'Name of the sheet where data is to be copied to. Rename Sheet2 to the name of your target sheet
            Set TargetSht = xlWs
            
            
            'This is the cells you will copy data from. This is targeting cells B1 to the last used cell in column B
            Set SourceCells = SourceSht.Range("e20:e30")
            
            
            'This is finding the next column available in the target sheet. It assumes dates will be in row 1 and data in row 2 down
            If TargetSht.Range("A1").Value = "" Then
                'Cell A1 is blank so the column to put data in will be column #1 (ie A)
                SourceCol = 1
            ElseIf TargetSht.Range("IV1").Value <> "" Then
                'Cell IV1 has something in it so we have reached the maximum number of columns we can use in this sheet.
                'Dont paste the data but advise the user.
                MsgBox "There are no more columns available in the sheet " & TargetSht.Name, vbCritical, "No More Data Can Be Copied"
                'stop the macro at this point
                Exit Sub
            Else
                'cell A1 does have data and we havent reached the last column yet so find the next available column
                SourceCol = TargetSht.Range("IV1").End(xlToLeft).Column + 1
            End If
            
            
            'Put in the date in the appropriate column in row 1 of the target sheet
            TargetSht.Cells(1, SourceCol).Value = Format(Date, "mm/dd/YYYY")
            
            
            'We can now start copying data. This will copy the cells in column B from the source sheet to row 2+ in the target sheet
            SourceCells.Copy TargetSht.Cells(4, SourceCol)
            
        Next xlWs
        
        'Advise the user that the process was successful
        MsgBox "Data copied successfully!", vbInformation, "Process Complete"
        
        
        Exit Sub 'This is to stop the procedure so we dont display the error message every time.
    Err_Handler:
        MsgBox "The following error occured:" & vbLf & "Error #: " & Err.Number & vbLf & "Description: " & Err.Description, _
        vbCritical, "An Error Has Occured", Err.HelpFile, Err.HelpContext
    
    End Sub
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

+ 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