Copy rows without triggering a macro VBA
up vote
1
down vote
favorite
There's a few questions relating to this subject but I'm struggling a bit.
I have a MASTER sheet
that stores various information. I use Column B
to hide/unhide other columns
. So for each different value entered in Column B, it will display the appropriate Columns
. e.g. If X
is entered in Column B
, Col C:F
will be displayed and Col G:I
will be hidden.
This works fine but I want to automatically copy
identical values from this sheet to separate sheets
. e.g. grab all X's
in MASTER and copy to a separate sheet
that contains only X's
.
I can do this but only through the use of a macro
that needs to be triggered. This isn't very efficient if I need to copy
numerous rows
. Especially if you've only updated 1 row
but need to copy everything.
I want to automatically auto-copy as soon as a value is entered. Without needing to trigger a macro
This is the script
the hides/unhides specific columns
in the MASTER sheet
:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
As I can't add a separate Worksheet_Change to this script to auto-copy across I'm a bit confused in how to do this
I'm currently using the script below. This copies the appropriate rows
to the respective sheet
. But it only works when triggered. I'm hoping to automatically copy
the row
once filled in.
@Gexas, how is this?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
excel vba automation copy
|
show 6 more comments
up vote
1
down vote
favorite
There's a few questions relating to this subject but I'm struggling a bit.
I have a MASTER sheet
that stores various information. I use Column B
to hide/unhide other columns
. So for each different value entered in Column B, it will display the appropriate Columns
. e.g. If X
is entered in Column B
, Col C:F
will be displayed and Col G:I
will be hidden.
This works fine but I want to automatically copy
identical values from this sheet to separate sheets
. e.g. grab all X's
in MASTER and copy to a separate sheet
that contains only X's
.
I can do this but only through the use of a macro
that needs to be triggered. This isn't very efficient if I need to copy
numerous rows
. Especially if you've only updated 1 row
but need to copy everything.
I want to automatically auto-copy as soon as a value is entered. Without needing to trigger a macro
This is the script
the hides/unhides specific columns
in the MASTER sheet
:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
As I can't add a separate Worksheet_Change to this script to auto-copy across I'm a bit confused in how to do this
I'm currently using the script below. This copies the appropriate rows
to the respective sheet
. But it only works when triggered. I'm hoping to automatically copy
the row
once filled in.
@Gexas, how is this?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
excel vba automation copy
So call it from theWorksheet_Change
event? I feel like I'm missing something here...
– Comintern
Nov 9 at 5:37
Huh? You've completely lost me. Why wouldFilterAndCopy()
need to be in the MASTER sheet - it's implicitly public.
– Comintern
Nov 9 at 5:49
I'm obviously a bit lost here. I don't understand what needs to go into what Worksheet.
– JPA0888
Nov 9 at 5:51
What am I calling from the Worksheet_Change event?
– JPA0888
Nov 9 at 5:52
FilterAndCopy()
as I understand the question.
– Comintern
Nov 9 at 5:54
|
show 6 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
There's a few questions relating to this subject but I'm struggling a bit.
I have a MASTER sheet
that stores various information. I use Column B
to hide/unhide other columns
. So for each different value entered in Column B, it will display the appropriate Columns
. e.g. If X
is entered in Column B
, Col C:F
will be displayed and Col G:I
will be hidden.
This works fine but I want to automatically copy
identical values from this sheet to separate sheets
. e.g. grab all X's
in MASTER and copy to a separate sheet
that contains only X's
.
I can do this but only through the use of a macro
that needs to be triggered. This isn't very efficient if I need to copy
numerous rows
. Especially if you've only updated 1 row
but need to copy everything.
I want to automatically auto-copy as soon as a value is entered. Without needing to trigger a macro
This is the script
the hides/unhides specific columns
in the MASTER sheet
:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
As I can't add a separate Worksheet_Change to this script to auto-copy across I'm a bit confused in how to do this
I'm currently using the script below. This copies the appropriate rows
to the respective sheet
. But it only works when triggered. I'm hoping to automatically copy
the row
once filled in.
@Gexas, how is this?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
excel vba automation copy
There's a few questions relating to this subject but I'm struggling a bit.
I have a MASTER sheet
that stores various information. I use Column B
to hide/unhide other columns
. So for each different value entered in Column B, it will display the appropriate Columns
. e.g. If X
is entered in Column B
, Col C:F
will be displayed and Col G:I
will be hidden.
This works fine but I want to automatically copy
identical values from this sheet to separate sheets
. e.g. grab all X's
in MASTER and copy to a separate sheet
that contains only X's
.
I can do this but only through the use of a macro
that needs to be triggered. This isn't very efficient if I need to copy
numerous rows
. Especially if you've only updated 1 row
but need to copy everything.
I want to automatically auto-copy as soon as a value is entered. Without needing to trigger a macro
This is the script
the hides/unhides specific columns
in the MASTER sheet
:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
As I can't add a separate Worksheet_Change to this script to auto-copy across I'm a bit confused in how to do this
I'm currently using the script below. This copies the appropriate rows
to the respective sheet
. But it only works when triggered. I'm hoping to automatically copy
the row
once filled in.
@Gexas, how is this?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
excel vba automation copy
excel vba automation copy
edited Nov 9 at 6:33
asked Nov 9 at 5:15
JPA0888
659
659
So call it from theWorksheet_Change
event? I feel like I'm missing something here...
– Comintern
Nov 9 at 5:37
Huh? You've completely lost me. Why wouldFilterAndCopy()
need to be in the MASTER sheet - it's implicitly public.
– Comintern
Nov 9 at 5:49
I'm obviously a bit lost here. I don't understand what needs to go into what Worksheet.
– JPA0888
Nov 9 at 5:51
What am I calling from the Worksheet_Change event?
– JPA0888
Nov 9 at 5:52
FilterAndCopy()
as I understand the question.
– Comintern
Nov 9 at 5:54
|
show 6 more comments
So call it from theWorksheet_Change
event? I feel like I'm missing something here...
– Comintern
Nov 9 at 5:37
Huh? You've completely lost me. Why wouldFilterAndCopy()
need to be in the MASTER sheet - it's implicitly public.
– Comintern
Nov 9 at 5:49
I'm obviously a bit lost here. I don't understand what needs to go into what Worksheet.
– JPA0888
Nov 9 at 5:51
What am I calling from the Worksheet_Change event?
– JPA0888
Nov 9 at 5:52
FilterAndCopy()
as I understand the question.
– Comintern
Nov 9 at 5:54
So call it from the
Worksheet_Change
event? I feel like I'm missing something here...– Comintern
Nov 9 at 5:37
So call it from the
Worksheet_Change
event? I feel like I'm missing something here...– Comintern
Nov 9 at 5:37
Huh? You've completely lost me. Why would
FilterAndCopy()
need to be in the MASTER sheet - it's implicitly public.– Comintern
Nov 9 at 5:49
Huh? You've completely lost me. Why would
FilterAndCopy()
need to be in the MASTER sheet - it's implicitly public.– Comintern
Nov 9 at 5:49
I'm obviously a bit lost here. I don't understand what needs to go into what Worksheet.
– JPA0888
Nov 9 at 5:51
I'm obviously a bit lost here. I don't understand what needs to go into what Worksheet.
– JPA0888
Nov 9 at 5:51
What am I calling from the Worksheet_Change event?
– JPA0888
Nov 9 at 5:52
What am I calling from the Worksheet_Change event?
– JPA0888
Nov 9 at 5:52
FilterAndCopy()
as I understand the question.– Comintern
Nov 9 at 5:54
FilterAndCopy()
as I understand the question.– Comintern
Nov 9 at 5:54
|
show 6 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Filter and Copy
If you move the 'Sub' into the sheet code (Master), you can lose the 'sht1' in it like you did in the 'Private Sub', if not just add the line FilterAndCopy appropriately into the 'Private Sub'.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo safe_exit
' Something has changed in Column "B"
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Application.EnableEvents = False
' *******************
FilterAndCopy ' *
' *******************
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
'****************
Sub FilterAndCopy()
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
'****************
ButFilterAndCopy()
still won't be triggered automatically, or I am wrong?
– Gexas
Nov 9 at 6:40
add a comment |
up vote
1
down vote
Worksheet_Change
event can have both of your functions inside. Since code is read from first to last line, with with order VBA will copy content from your second code and later hides columns. Code below should work, assuming everything else is good with. Note that I can not test it and did not check for other errors. Just changed order.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
Nope, every time there is a change anywhere on the worksheet, sheet CON is being changed. No need to do this unless a value in a cell in the B column has changed to "Change of Numbers".
– VBasic2008
Nov 9 at 7:05
Oh I see what you talking about now, I'll change my answer to correct order then.
– Gexas
Nov 9 at 7:39
That's nice. Now I want to manually trigger FilterAndCopy (run macro).
– VBasic2008
Nov 9 at 7:52
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Filter and Copy
If you move the 'Sub' into the sheet code (Master), you can lose the 'sht1' in it like you did in the 'Private Sub', if not just add the line FilterAndCopy appropriately into the 'Private Sub'.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo safe_exit
' Something has changed in Column "B"
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Application.EnableEvents = False
' *******************
FilterAndCopy ' *
' *******************
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
'****************
Sub FilterAndCopy()
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
'****************
ButFilterAndCopy()
still won't be triggered automatically, or I am wrong?
– Gexas
Nov 9 at 6:40
add a comment |
up vote
2
down vote
accepted
Filter and Copy
If you move the 'Sub' into the sheet code (Master), you can lose the 'sht1' in it like you did in the 'Private Sub', if not just add the line FilterAndCopy appropriately into the 'Private Sub'.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo safe_exit
' Something has changed in Column "B"
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Application.EnableEvents = False
' *******************
FilterAndCopy ' *
' *******************
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
'****************
Sub FilterAndCopy()
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
'****************
ButFilterAndCopy()
still won't be triggered automatically, or I am wrong?
– Gexas
Nov 9 at 6:40
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Filter and Copy
If you move the 'Sub' into the sheet code (Master), you can lose the 'sht1' in it like you did in the 'Private Sub', if not just add the line FilterAndCopy appropriately into the 'Private Sub'.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo safe_exit
' Something has changed in Column "B"
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Application.EnableEvents = False
' *******************
FilterAndCopy ' *
' *******************
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
'****************
Sub FilterAndCopy()
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
'****************
Filter and Copy
If you move the 'Sub' into the sheet code (Master), you can lose the 'sht1' in it like you did in the 'Private Sub', if not just add the line FilterAndCopy appropriately into the 'Private Sub'.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo safe_exit
' Something has changed in Column "B"
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Application.EnableEvents = False
' *******************
FilterAndCopy ' *
' *******************
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
'****************
Sub FilterAndCopy()
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
End Sub
'****************
edited Nov 25 at 10:00
answered Nov 9 at 6:32
VBasic2008
679213
679213
ButFilterAndCopy()
still won't be triggered automatically, or I am wrong?
– Gexas
Nov 9 at 6:40
add a comment |
ButFilterAndCopy()
still won't be triggered automatically, or I am wrong?
– Gexas
Nov 9 at 6:40
But
FilterAndCopy()
still won't be triggered automatically, or I am wrong?– Gexas
Nov 9 at 6:40
But
FilterAndCopy()
still won't be triggered automatically, or I am wrong?– Gexas
Nov 9 at 6:40
add a comment |
up vote
1
down vote
Worksheet_Change
event can have both of your functions inside. Since code is read from first to last line, with with order VBA will copy content from your second code and later hides columns. Code below should work, assuming everything else is good with. Note that I can not test it and did not check for other errors. Just changed order.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
Nope, every time there is a change anywhere on the worksheet, sheet CON is being changed. No need to do this unless a value in a cell in the B column has changed to "Change of Numbers".
– VBasic2008
Nov 9 at 7:05
Oh I see what you talking about now, I'll change my answer to correct order then.
– Gexas
Nov 9 at 7:39
That's nice. Now I want to manually trigger FilterAndCopy (run macro).
– VBasic2008
Nov 9 at 7:52
add a comment |
up vote
1
down vote
Worksheet_Change
event can have both of your functions inside. Since code is read from first to last line, with with order VBA will copy content from your second code and later hides columns. Code below should work, assuming everything else is good with. Note that I can not test it and did not check for other errors. Just changed order.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
Nope, every time there is a change anywhere on the worksheet, sheet CON is being changed. No need to do this unless a value in a cell in the B column has changed to "Change of Numbers".
– VBasic2008
Nov 9 at 7:05
Oh I see what you talking about now, I'll change my answer to correct order then.
– Gexas
Nov 9 at 7:39
That's nice. Now I want to manually trigger FilterAndCopy (run macro).
– VBasic2008
Nov 9 at 7:52
add a comment |
up vote
1
down vote
up vote
1
down vote
Worksheet_Change
event can have both of your functions inside. Since code is read from first to last line, with with order VBA will copy content from your second code and later hides columns. Code below should work, assuming everything else is good with. Note that I can not test it and did not check for other errors. Just changed order.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
Worksheet_Change
event can have both of your functions inside. Since code is read from first to last line, with with order VBA will copy content from your second code and later hides columns. Code below should work, assuming everything else is good with. Note that I can not test it and did not check for other errors. Just changed order.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("MASTER")
Set sht2 = Worksheets("CON")
sht2.UsedRange.ClearContents
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False ' unhide columns
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter field:=1, Criteria1:="Change of Numbers"
.Range("A:F, BL:BO").Copy Destination:=sht2.Cells(2, "B")
.Parent.AutoFilterMode = False
.Range("H:BK").EntireColumn.Hidden = True ' hide columns
End With
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "Change of Numbers"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
'do nothing
End Select
Next t
End If
safe_exit:
Application.EnableEvents = True
End Sub
edited Nov 9 at 7:41
answered Nov 9 at 6:37
Gexas
1199
1199
Nope, every time there is a change anywhere on the worksheet, sheet CON is being changed. No need to do this unless a value in a cell in the B column has changed to "Change of Numbers".
– VBasic2008
Nov 9 at 7:05
Oh I see what you talking about now, I'll change my answer to correct order then.
– Gexas
Nov 9 at 7:39
That's nice. Now I want to manually trigger FilterAndCopy (run macro).
– VBasic2008
Nov 9 at 7:52
add a comment |
Nope, every time there is a change anywhere on the worksheet, sheet CON is being changed. No need to do this unless a value in a cell in the B column has changed to "Change of Numbers".
– VBasic2008
Nov 9 at 7:05
Oh I see what you talking about now, I'll change my answer to correct order then.
– Gexas
Nov 9 at 7:39
That's nice. Now I want to manually trigger FilterAndCopy (run macro).
– VBasic2008
Nov 9 at 7:52
Nope, every time there is a change anywhere on the worksheet, sheet CON is being changed. No need to do this unless a value in a cell in the B column has changed to "Change of Numbers".
– VBasic2008
Nov 9 at 7:05
Nope, every time there is a change anywhere on the worksheet, sheet CON is being changed. No need to do this unless a value in a cell in the B column has changed to "Change of Numbers".
– VBasic2008
Nov 9 at 7:05
Oh I see what you talking about now, I'll change my answer to correct order then.
– Gexas
Nov 9 at 7:39
Oh I see what you talking about now, I'll change my answer to correct order then.
– Gexas
Nov 9 at 7:39
That's nice. Now I want to manually trigger FilterAndCopy (run macro).
– VBasic2008
Nov 9 at 7:52
That's nice. Now I want to manually trigger FilterAndCopy (run macro).
– VBasic2008
Nov 9 at 7:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220237%2fcopy-rows-without-triggering-a-macro-vba%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So call it from the
Worksheet_Change
event? I feel like I'm missing something here...– Comintern
Nov 9 at 5:37
Huh? You've completely lost me. Why would
FilterAndCopy()
need to be in the MASTER sheet - it's implicitly public.– Comintern
Nov 9 at 5:49
I'm obviously a bit lost here. I don't understand what needs to go into what Worksheet.
– JPA0888
Nov 9 at 5:51
What am I calling from the Worksheet_Change event?
– JPA0888
Nov 9 at 5:52
FilterAndCopy()
as I understand the question.– Comintern
Nov 9 at 5:54