A code to run a macro

Unexpc

Active Member
Joined
Nov 12, 2020
Messages
496
Office Version
  1. 2019
Platform
  1. Windows
Hi guys
I have a code thats working with run a macro
Exist a code when a range of specific cell is fill, after that runing macro?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If cells are changed by keyborad and not by formula or macro you can exploit the event Workbooks_Change. The following example must be placed in the affected sheet's module:
VBA Code:
Option Explicit
Private Sub Workbooks_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("J2:K20")) Is Nothing Then 'change range as suited
        '
        'here do what you need
        'or Call a macro
        '
    End If
End Sub
 
Upvote 0
If cells are changed by keyborad and not by formula or macro you can exploit the event Workbooks_Change. The following example must be placed in the affected sheet's module:
VBA Code:
Option Explicit
Private Sub Workbooks_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("J2:K20")) Is Nothing Then 'change range as suited
        '
        'here do what you need
        'or Call a macro
        '
    End If
End Sub
hmmm, i can't compile and understand how add code in this option
this my vba code for run a macro
VBA Code:
Sub MyCopyRange()

    Dim nr As Long
    Dim i As Long
        Application.ScreenUpdating = False

'   Set initial value of next row
    nr = 34
    
'   Copy range
    For i = 2 To 30840 Step 1
'   Copy Range based Cell B66  & B98
      If Sheets("sheet1").Range("B" & (nr - 2) * i + 2).Value <> "" Then
      
        Sheets("sheet2").Range("A" & nr * (i - 1) + 7 & ":H" & nr * i + 6).Copy Sheets("sheet2").Range("A" & nr * i + 7)
'       Add 34 to next row

      Else
         Exit Sub
      End If
     Next i

    Application.ScreenUpdating = True
    
End Sub
i want when filling specific cells (B66,B98 and next 32 cells) or this rows in sheet1, that's run macro after that
 
Upvote 0
Sorry, got the wrong event, use this instead:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Union(Range("B66"), Range("B98:B130"))) Is Nothing Then
        Call MyCopyRange
    End If
End Sub
This macro is not to be add to your macro, it must be copied as it is in Sheet1 vba module.
When you change data in range B66 or B98:B130 your macro MyCopyRange will be then executed.
 
Upvote 0
Sorry, got the wrong event, use this instead:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Union(Range("B66"), Range("B98:B130"))) Is Nothing Then
        Call MyCopyRange
    End If
End Sub
This macro is not to be add to your macro, it must be copied as it is in Sheet1 vba module.
When you change data in range B66 or B98:B130 your macro MyCopyRange will be then executed.
Sorry, i mean, the specific cells thats a sequence (66,98,130,162,... start from 66 and difference between consecutive numbers is 32) this my range i want(thats exist in the code but im not write code and i don't know which line for make this sequence)
 
Upvote 0
Your english is worse than mine :p but I now understand that you need some sort of step 32 in your macro.
I can't realize your goal so I give up, sorry.
 
Upvote 0
Perhaps this is what you are after, it goes in the worksheet module ...
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 And Target.Column = 2 And Target.Row >= 66 Then
        If (Target.Row - 34) Mod 32 = 0 Then
            '
            '  do your stuff
            '
        End If
    End If
End Sub
 
Upvote 0
Solution
Perhaps this is what you are after, it goes in the worksheet module ...
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 And Target.Column = 2 And Target.Row >= 66 Then
        If (Target.Row - 34) Mod 32 = 0 Then
            '
            '  do your stuff
            '
        End If
    End If
End Sub
Thank You, This is what I wanted
 
Upvote 0
You are welcome and thanks for letting me know.
Meanwhile a took a closer look at your code and I think you'll ultimately were looking for something like the code below.
The code runs on every change of your worksheet. It checks if the change took place in column B. If that's the case it checks wether the cell is on row 66, 98, 130, 162 (or ...+32 and so on). If that's the case it checks wether the change results in any value or any text. If so than the code performs a copy action equivalent to that of your original code.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lRow As Long
   
    If Target.Count = 1 And Target.Column = 2 And Target.Row >= 66 Then
        If (Target.Row - 34) Mod 32 = 0 Then
            If Target.Value <> "" Then
                lRow = (((Target.Row - 34) * 34) / 32) + 7
                With ThisWorkbook.Sheets("Sheet2")
                    .Range("A" & lRow & ":H" & lRow + 33).Copy .Range("A" & lRow + 34)
                End With
            End If
        End If
    End If
End Sub
 
Upvote 0
You are welcome and thanks for letting me know.
Meanwhile a took a closer look at your code and I think you'll ultimately were looking for something like the code below.
The code runs on every change of your worksheet. It checks if the change took place in column B. If that's the case it checks wether the cell is on row 66, 98, 130, 162 (or ...+32 and so on). If that's the case it checks wether the change results in any value or any text. If so than the code performs a copy action equivalent to that of your original code.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lRow As Long
  
    If Target.Count = 1 And Target.Column = 2 And Target.Row >= 66 Then
        If (Target.Row - 34) Mod 32 = 0 Then
            If Target.Value <> "" Then
                lRow = (((Target.Row - 34) * 34) / 32) + 7
                With ThisWorkbook.Sheets("Sheet2")
                    .Range("A" & lRow & ":H" & lRow + 33).Copy .Range("A" & lRow + 34)
                End With
            End If
        End If
    End If
End Sub
How to change number 66 in first lf line to 67? and next target cell, 99,131,... (that you say every 32 cell after that)
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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