Can´t execute macros in another workbook

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
I have 2 Workbooks open (one is called "Machine_Data.xls" and the other is "Corrections_Data.xlsx") the goal is to pass variable values from one book to the other and it is working, but in addition to the cells I would like to run a macro present in the first book in the second book, can someone help me?


Code for pass values:

VBA Code:
 Private Sub Write_Miter()
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 14).Value = Y1_Axis_Orig_D
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 15).Value = Y2_Axis_Orig_D
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 16).Value = Z_100_Orig_D
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 17).Value = Z_250_Orig_D
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 18).Value = Z_400_Orig_D
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 19).Value = Z_550_Orig_D
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 20).Select
    Miter_2.ColorCells_Miter_2
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 21).Value = X1_Axis_Orig
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 22).Value = X2_Axis_Orig
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 23).Value = Z_100_Final
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 24).Value = Z_250_Final
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 25).Value = Z_400_Final
    Workbooks("Corrections_Data.xlsx").Sheets("Miter_Registers").Cells(k_miter, 26).Value = Z_550_Final
    End Sub




Macro:
Code:
Public Sub ColorCells_Miter_2()

If ActiveCell.Column = 20 Then
      
    If Z_550_Orig_D <= 0 And Y2_Axis_Orig_D <= 0 Then

        If Abs(Z_550_Orig_D * 1) + Abs(Y2_Axis_Orig_D * 1) < 30 Then
            ActiveCell.Interior.ColorIndex = 4
            ActiveCell.Font.Color = vbBlack
            ActiveCell.Value = "Não Precisa de Correção"
        
        ElseIf Abs(Z_550_Orig_D * 1) + Abs(Y2_Axis_Orig_D * 1) >= 30 Then
            ActiveCell.Interior.ColorIndex = 3
            ActiveCell.Value = "Correção Necessária"
        End If
    
    
    
    ElseIf Z_550_Orig_D > 0 And Y2_Axis_Orig_D <= 0 Then

        If Abs(Z_550_Orig_D + (Y2_Axis_Orig_D * 1)) < 30 Then
            ActiveCell.Interior.ColorIndex = 4
            ActiveCell.Font.Color = vbBlack
            ActiveCell.Value = "Não Precisa de Correção"
        
        ElseIf Abs(Z_550_Orig_D + (Y2_Axis_Orig_D * 1)) >= 30 Then
            ActiveCell.Interior.ColorIndex = 3
            ActiveCell.Value = "Correção Necessária"
        End If
    
        
          
    ElseIf Z_550_Orig_D < 0 And Y2_Axis_Orig_D >= 0 Then

        If Abs((Z_550_Orig_D * 1) + Y2_Axis_Orig_D) < 30 Then
            ActiveCell.Interior.ColorIndex = 4
            ActiveCell.Font.Color = vbBlack
            ActiveCell.Value = "Não Precisa de Correção"
        
        ElseIf Abs((Z_550_Orig_D * 1) + Y2_Axis_Orig_D) >= 30 Then
            ActiveCell.Interior.ColorIndex = 3
            ActiveCell.Value = "Correção Necessária"
        End If
          
          
    
    ElseIf Z_550_Orig_D >= 0 And Y2_Axis_Orig_D >= 0 Then

        If Y2_Axis_Orig_D + Z_550_Orig_D < 30 Then
            ActiveCell.Interior.ColorIndex = 4
            ActiveCell.Font.Color = vbBlack
            ActiveCell.Value = "Não Precisa de Correção"
        
        ElseIf Y2_Axis_Orig_D + Z_550_Orig_D >= 30 Then
            ActiveCell.Interior.ColorIndex = 3
            ActiveCell.Value = "Correção Necessária"
        End If
    End If
    
End If
End Sub


The macro would have to be executed on the cells scrawled in blue

Capturar.JPG
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:

VBA Code:
Sub run_a_macro_in_other_book()
  Run "'Machine_Data.xls'!ColorCells_Miter_2"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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