macro support

sksanjeev786

Well-known Member
Joined
Aug 5, 2020
Messages
1,019
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Team, The macro is working correctly for me but the colunm numbers are not coming there are 8 colum but i am getting 4 colunm

as i am inserting hte colunm after "Is great for weekend trips"

1740176959123.png
acr

VBA Code:
[CODE=vba]
Sub InsertExactRowAfterWeekendTrips()
    Dim slide As slide
    Dim shape As shape
    Dim tbl As Table
    Dim totalCols As Integer
    Dim totalRows As Integer
    Dim rowIndex As Integer
    Dim found As Boolean
    Dim colIndex As Integer
    
    ' Get the active slide
    Set slide = ActivePresentation.Slides(Application.ActiveWindow.View.slide.slideIndex)
    
    ' Check if a shape is selected
    If Application.ActiveWindow.Selection.Type <> ppSelectionShapes Then
        MsgBox "Please select a table.", vbExclamation, "No Table Selected"
        Exit Sub
    End If
    
    ' Get the selected shape
    Set shape = Application.ActiveWindow.Selection.ShapeRange(1)
    
    ' Check if the shape contains a table
    If Not shape.HasTable Then
        MsgBox "Selected shape is not a table.", vbExclamation, "Error"
        Exit Sub
    End If
    
    ' Get the table object
    Set tbl = shape.Table
    totalCols = tbl.Columns.Count
    totalRows = tbl.Rows.Count
    found = False
    
    ' Loop through rows to find the text "Is great for weekend trips"
    For rowIndex = 1 To totalRows
        If tbl.cell(rowIndex, 1).shape.TextFrame.TextRange.Text = "Is great for weekend trips" Then
            found = True
            Exit For
        End If
    Next rowIndex
    
    ' If text is found, insert a new row after it and copy formatting
    If found Then
        tbl.Rows.Add (rowIndex + 1) ' Insert new row after the found row
        
        ' Copy text, formatting, font color, and background color
        For colIndex = 1 To totalCols
            With tbl.cell(rowIndex + 1, colIndex).shape.TextFrame.TextRange
                .Text = tbl.cell(rowIndex, colIndex).shape.TextFrame.TextRange.Text
                .Font.Size = tbl.cell(rowIndex, colIndex).shape.TextFrame.TextRange.Font.Size
                .Font.Bold = tbl.cell(rowIndex, colIndex).shape.TextFrame.TextRange.Font.Bold
                .Font.Italic = tbl.cell(rowIndex, colIndex).shape.TextFrame.TextRange.Font.Italic
                .Font.Color = tbl.cell(rowIndex, colIndex).shape.TextFrame.TextRange.Font.Color
            End With
            
            ' Copy cell background color
            tbl.cell(rowIndex + 1, colIndex).shape.Fill.ForeColor.RGB = tbl.cell(rowIndex, colIndex).shape.Fill.ForeColor.RGB
        Next colIndex
        
        MsgBox "Row duplicated successfully with exact format!", vbInformation, "Success"
    Else
        MsgBox "Text 'Is great for weekend trips' not found in the table.", vbExclamation, "Error"
    End If
End Sub
[/CODE]
 

Forum statistics

Threads
1,226,837
Messages
6,193,249
Members
453,784
Latest member
Chandni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top