Run Macro on all sheets ??

Gungor

New Member
Joined
Jun 8, 2015
Messages
5
Hi all,
The following macro works only in active sheets.
Can you please advise how can we do, that this works in all worksheets?
Or all just the worksheets which i needs that works.



Code:
Option Explicit

'A custom type that holds the scale factors of the block.
Private Type ScaleFactor
    X As Double
    Y As Double
    Z As Double
End Type


Sub InsertBlocks()


    '--------------------------------------------------------------------------------------------------------------------------
  
    'Declaring the necessary variables.
    Dim acadApp                 As Object
    Dim acadDoc                 As Object
    Dim acadBlock               As Object
    Dim LastRow                 As Long
    Dim I                       As Long
    Dim InsertionPoint(0 To 2)  As Double
    Dim BlockName               As String
    Dim BlockScale              As ScaleFactor
    Dim RotationAngle           As Double
    
    
[COLOR=#ff0000]    With Sheets(ActiveSheet.Name)[/COLOR]
    .Activate
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
        
    'Check if there are coordinates for at least one circle.
    If LastRow < 2 Then
        MsgBox "There are no coordinates for the insertion point!", vbCritical, "Insertion Point Error"
        Exit Sub
    End If
    
    'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    
    'Check (again) if there is an AutoCAD object.
    If acadApp Is Nothing Then
        MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
    On Error GoTo 0
    
    'If there is no active drawing create a new one.
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add
    End If
    On Error GoTo 0


    'Check if the active space is paper space and change it to model space.
    If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
        acadDoc.ActiveSpace = 1     '1 = acModelSpace in early binding
    End If
    
    On Error Resume Next
    'Loop through all the rows and add the corresponding blocks in AutoCAD.
[COLOR=#ff0000]    With Sheets(ActiveSheet.Name)[/COLOR]
        For I = 2 To LastRow
                 'Set the block name.
            BlockName = .Range("D" & I).value
                 'If the block name is not empty, insert the block.
            If BlockName <> vbNullString Then
     
                'Set the insertion point.
                InsertionPoint(0) = .Range("A" & I).value
                InsertionPoint(1) = .Range("B" & I).value
                InsertionPoint(2) = .Range("C" & I).value
              
                'Initialize the optional parameters.
                BlockScale.X = 1
                BlockScale.Y = 1
                BlockScale.Z = 1
                RotationAngle = 0
               
                'Add the block using the sheet data (insertion point, block name, scale factors and rotation angle).
                'The 0.0174532925 is to convert degrees into radians.
                Set acadBlock = acadDoc.ModelSpace.InsertBlock(InsertionPoint, BlockName, _
                                BlockScale.X, BlockScale.Y, BlockScale.Z, RotationAngle * 0.0174532925)
                                
    
                ' Get the attributes for the block reference
            Dim varAttributes As Variant
                varAttributes = acadBlock.GetAttributes
                
                ' Move the attribute tags and values into a string to be displayed in a Msgbox
            Dim strAttributes As String
                Dim K As Integer
                For K = LBound(varAttributes) To UBound(varAttributes)
                       strAttributes = strAttributes & vbLf & "  Tag: " & varAttributes(K).TagString & _
                       vbLf & "  Value: " & varAttributes(K).TextString & vbLf & "    "
                Next
                           
                                
            End If
        Next I
    End With
    
    'Zoom in to the drawing area.
    acadApp.ZoomExtents


End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Maybe this, UNTESTED, so try it on a copy

Code:
Sub InsertBlocks()


    '--------------------------------------------------------------------------------------------------------------------------
  
    'Declaring the necessary variables.
    Dim acadApp                 As Object
    Dim acadDoc                 As Object
    Dim acadBlock               As Object
    Dim LastRow                 As Long
    Dim I                       As Long
    Dim InsertionPoint(0 To 2)  As Double
    Dim BlockName               As String
    Dim BlockScale              As ScaleFactor
    Dim RotationAngle           As Double
    Dim ws As Worksheet
    
    For Each ws In Worksheets
    ws.Activate
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    'Check if there are coordinates for at least one circle.
    If LastRow < 2 Then
        MsgBox "There are no coordinates for the insertion point!", vbCritical, "Insertion Point Error"
        Exit Sub
    End If
    
    'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    
    'Check (again) if there is an AutoCAD object.
    If acadApp Is Nothing Then
        MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
    On Error GoTo 0
    
    'If there is no active drawing create a new one.
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add
    End If
    On Error GoTo 0


    'Check if the active space is paper space and change it to model space.
    If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
        acadDoc.ActiveSpace = 1     '1 = acModelSpace in early binding
    End If
    
    On Error Resume Next
    'Loop through all the rows and add the corresponding blocks in AutoCAD.
        For I = 2 To LastRow
                 'Set the block name.
            BlockName = Range("D" & I).Value
                 'If the block name is not empty, insert the block.
            If BlockName <> vbNullString Then
     
                'Set the insertion point.
                InsertionPoint(0) = Range("A" & I).Value
                InsertionPoint(1) = Range("B" & I).Value
                InsertionPoint(2) = Range("C" & I).Value
              
                'Initialize the optional parameters.
                BlockScale.X = 1
                BlockScale.Y = 1
                BlockScale.Z = 1
                RotationAngle = 0
               
                'Add the block using the sheet data (insertion point, block name, scale factors and rotation angle).
                'The 0.0174532925 is to convert degrees into radians.
                Set acadBlock = acadDoc.ModelSpace.InsertBlock(InsertionPoint, BlockName, _
                                BlockScale.X, BlockScale.Y, BlockScale.Z, RotationAngle * 0.0174532925)
                                
    
                ' Get the attributes for the block reference
            Dim varAttributes As Variant
                varAttributes = acadBlock.GetAttributes
                
                ' Move the attribute tags and values into a string to be displayed in a Msgbox
            Dim strAttributes As String
                Dim K As Integer
                For K = LBound(varAttributes) To UBound(varAttributes)
                       strAttributes = strAttributes & vbLf & "  Tag: " & varAttributes(K).TagString & _
                       vbLf & "  Value: " & varAttributes(K).TextString & vbLf & "    "
                Next
                           
                                
            End If
        Next I
    
    'Zoom in to the drawing area.
    acadApp.ZoomExtents
Next ws

End Sub
 
Upvote 0
Just to be clear, you just want to run this macro on all sheets, always, just like it is?

If so, I think you can loop your macro trough all sheets, like this:

Code:
Sub InsertBlocks()

'' your stuff here
'Loop here:

[COLOR=#ff0000]For SheetIndex = 1 To Application.Sheets.Count

With Sheets(SheetIndex)[/COLOR]

' rest of your macro here
'
'
'
'
'
[COLOR=#ff0000]Next SheetIndex[/COLOR]
End Sub

EDIT: Oh, you can declare SheetIndex As Long but imo it's not necessary, it works fine without.

Matias
 
Last edited:
Upvote 0
Hi Michael,
Thanks for your reply, but unfortunately not working
Maybe this, UNTESTED, so try it on a copy

Code:
Sub InsertBlocks()


    '--------------------------------------------------------------------------------------------------------------------------
  
    'Declaring the necessary variables.
    Dim acadApp                 As Object
    Dim acadDoc                 As Object
    Dim acadBlock               As Object
    Dim LastRow                 As Long
    Dim I                       As Long
    Dim InsertionPoint(0 To 2)  As Double
    Dim BlockName               As String
    Dim BlockScale              As ScaleFactor
    Dim RotationAngle           As Double
    Dim ws As Worksheet
    
    For Each ws In Worksheets
    ws.Activate
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    'Check if there are coordinates for at least one circle.
    If LastRow < 2 Then
        MsgBox "There are no coordinates for the insertion point!", vbCritical, "Insertion Point Error"
        Exit Sub
    End If
    
    'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    
    'Check (again) if there is an AutoCAD object.
    If acadApp Is Nothing Then
        MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
    On Error GoTo 0
    
    'If there is no active drawing create a new one.
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add
    End If
    On Error GoTo 0


    'Check if the active space is paper space and change it to model space.
    If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
        acadDoc.ActiveSpace = 1     '1 = acModelSpace in early binding
    End If
    
    On Error Resume Next
    'Loop through all the rows and add the corresponding blocks in AutoCAD.
        For I = 2 To LastRow
                 'Set the block name.
            BlockName = Range("D" & I).Value
                 'If the block name is not empty, insert the block.
            If BlockName <> vbNullString Then
     
                'Set the insertion point.
                InsertionPoint(0) = Range("A" & I).Value
                InsertionPoint(1) = Range("B" & I).Value
                InsertionPoint(2) = Range("C" & I).Value
              
                'Initialize the optional parameters.
                BlockScale.X = 1
                BlockScale.Y = 1
                BlockScale.Z = 1
                RotationAngle = 0
               
                'Add the block using the sheet data (insertion point, block name, scale factors and rotation angle).
                'The 0.0174532925 is to convert degrees into radians.
                Set acadBlock = acadDoc.ModelSpace.InsertBlock(InsertionPoint, BlockName, _
                                BlockScale.X, BlockScale.Y, BlockScale.Z, RotationAngle * 0.0174532925)
                                
    
                ' Get the attributes for the block reference
            Dim varAttributes As Variant
                varAttributes = acadBlock.GetAttributes
                
                ' Move the attribute tags and values into a string to be displayed in a Msgbox
            Dim strAttributes As String
                Dim K As Integer
                For K = LBound(varAttributes) To UBound(varAttributes)
                       strAttributes = strAttributes & vbLf & "  Tag: " & varAttributes(K).TagString & _
                       vbLf & "  Value: " & varAttributes(K).TextString & vbLf & "    "
                Next
                           
                                
            End If
        Next I
    
    'Zoom in to the drawing area.
    acadApp.ZoomExtents
Next ws

End Sub
 
Upvote 0
Your response gives me idea why it isn't working !!
Is there an error message ?
Are the sheets being activated ?
What is happening / not happening ??
Have you tried stepping through manually to see where the code fails / stops ??
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

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