+ Reply to Thread
Results 1 to 11 of 11

Have existing formatting code. Need to add macro to new workbook.

Hybrid View

  1. #1
    Valued Forum Contributor PeteABC123's Avatar
    Join Date
    09-21-2012
    Location
    Chicago, IL
    MS-Off Ver
    MS Office 365 ver 2202
    Posts
    1,104

    Have existing formatting code. Need to add macro to new workbook.

    Have a macro that reformats a newly opened workbook. (Items such as deleting columns, and inserting formulas in a couple of fields.)
    Would like to have the aformentioned macro (which is located in another workbook), add this event handler private sub below in addition to reformatting the page as described. It adds a highlight row.

    The code I'd like to add is this:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Cells.FormatConditions.Delete
        With Target.EntireRow
            .FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
            With .FormatConditions(1)
                With .Borders(xlTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = 2
                End With
                With .Borders(xlBottom)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = 8
                End With
            End With
            .FormatConditions(1).Interior.ColorIndex = 42        
        End With
    End Sub
    Thanks in advance for some direction on this.

    Petee

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Have existing formatting code. Need to add macro to new workbook.

    You could copy and paste it at the end of the reformating macro.

  3. #3
    Valued Forum Contributor PeteABC123's Avatar
    Join Date
    09-21-2012
    Location
    Chicago, IL
    MS-Off Ver
    MS Office 365 ver 2202
    Posts
    1,104

    Re: Have existing formatting code. Need to add macro to new workbook.

    I had tried recording the opening of the VBA editor (right click on tab, view code), and all I got was the selecting of the page. Thoughts on opening and closing the vba editor within a macro? (The sheet tab name changes every time. . .)
    Peter

  4. #4
    Valued Forum Contributor PeteABC123's Avatar
    Join Date
    09-21-2012
    Location
    Chicago, IL
    MS-Off Ver
    MS Office 365 ver 2202
    Posts
    1,104

    Re: Have existing formatting code. Need to add macro to new workbook.

    John, how would I integrate the highlight active row code per OP after executing the following code?

    Sub Macro4()
    '
    ' Macro4 Macro
    '
    
    '
        Columns("E:E").Select
        Range("E4").Activate
        Selection.Font.Bold = True
        Columns("F:F").Select
        Range("F4").Activate
        Selection.Delete Shift:=xlToLeft
        Range("A4").Select
    End Sub

    Thanks.

    Peter

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Have existing formatting code. Need to add macro to new workbook.

    You could attach it too the end.

    Sub PeteABC123()
        Columns("E:E").Select
        Range("E4").Activate
        Selection.Font.Bold = True
        Columns("F:F").Select
        Range("F4").Activate
        Selection.Delete Shift:=xlToLeft
        Range("A4").Select
        Cells.FormatConditions.Delete
        With ActiveCell.EntireRow
            .FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
            With .FormatConditions(1)
                With .Borders(xlTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = 2
                End With
                With .Borders(xlBottom)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = 8
                End With
            End With
            .FormatConditions(1).Interior.ColorIndex = 42
        End With
    End Sub

  6. #6
    Valued Forum Contributor PeteABC123's Avatar
    Join Date
    09-21-2012
    Location
    Chicago, IL
    MS-Off Ver
    MS Office 365 ver 2202
    Posts
    1,104

    Re: Have existing formatting code. Need to add macro to new workbook.

    John, thanks for your work on it. However, when using your suggested code, the row that became highlighted does not change from the point of insertion with subsequent mouseclicks, rather than changing to the row that contains the active cell; as the original code behaves. Thanks in advance for any additional takes on this.
    Pete

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Have existing formatting code. Need to add macro to new workbook.

    Perhaps I didn't understand. Do you want the snippet in Post# 5 added too the Event Code in Post# 1?

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Have existing formatting code. Need to add macro to new workbook.

    Do a web search for something like VBA Extensibility

    This link is a good start. Programming The VBA Editor
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ 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] How to integrate this code to an existing macro?
    By Aretradeser in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-04-2014, 01:38 PM
  2. Add new sheet to existing Excel workbook directory with VB code
    By mzkhrissy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-11-2013, 12:04 AM
  3. [SOLVED] Modify Existing Mbr Code to Save to Ext.Workbook
    By Miskondukt in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 03-06-2013, 02:36 PM
  4. Altering existing macro code
    By jsmity in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-03-2010, 01:05 PM
  5. Insert a new workbook with VBA code in the sheet module into existing macro
    By bobaftt in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-19-2009, 12:12 PM

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