Copy individual range based on highlighted last cell from sheet to another

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
399
Office Version
  1. 2016
Platform
  1. Windows
Hello
I want copying for each range just contains higlighted cell in column H for the last row (SUMMING) from sheet to another
source data
Copy of TR.xlsm
ABCDEFGH
1CODE
2FR-00
3ITEMDATEGOODSTYPEPRQTYUNITTOTAL
4101/01/2021BANANAFOPL200.0012.002400.00
5207/01/2021BANANAFOPL450.0012.005400.00
6308/01/2021BANANAFOPL500.0019.579785.71
7414/01/2021BANANAFOPL800.0025.3620285.71
8515/01/2021BANANAFOPL850.0026.3222373.21
9SUMMING2800.0060244.64
10
11
12CODE
13FR-01
14ITEMDATEGOODSTYPEPRQTYUNITTOTAL
15102/01/2021APPLEFRPPL100.0014.001400.00
16209/01/2021APPLEFRPPL550.0020.5411294.64
17316/01/2021APPLEFRPPL900.0027.2924557.14
18SUMMING1550.0037251.79
19
20
21CODE
22FR-02
23ITEMDATEGOODSTYPEPRQTYUNITTOTAL
24103/01/2021PEACHFRLLP300.0015.004500.00
25210/01/2021PEACHFRLLP600.0021.5012900.00
26317/01/2021PEACHFRLLP950.0028.2526837.50
27SUMMING1850.0044237.50
28
29
30CODE
31FR-03
32ITEMDATEGOODSTYPEPRQTYUNITTOTAL
33104/01/2021PEARFRTTL300.0012.003600.00
34211/01/2021PEARFRTTL650.0022.4614601.79
35318/01/2021BANANAFOPL1000.0029.2129214.29
36SUMMING1950.0047416.07
37
38
39CODE
40FR-04
41ITEMDATEGOODSTYPEPRQTYUNITTOTAL
42105/01/2021STRWBERRAYFRMML350.0020.007000.00
43212/01/2021STRWBERRAYFRMML700.0023.4316400.00
44319/01/2021APPLEFRPPL1050.0030.1831687.50
45SUMMING2100.0055087.50
RETSEL


result like this
Copy of TR.xlsm
ABCDEFGH
1CODE
2FR-00
3ITEMDATEGOODSTYPEPRQTYUNITTOTAL
4101/01/2021BANANAFOPL200.0012.002400.00
5207/01/2021BANANAFOPL450.0012.005400.00
6308/01/2021BANANAFOPL500.0019.579785.71
7414/01/2021BANANAFOPL800.0025.3620285.71
8515/01/2021BANANAFOPL850.0026.3222373.21
9SUMMING2800.0060244.64
10
11
12CODE
13FR-02
14ITEMDATEGOODSTYPEPRQTYUNITTOTAL
15103/01/2021PEACHFRLLP300.0015.004500.00
16210/01/2021PEACHFRLLP600.0021.5012900.00
17317/01/2021PEACHFRLLP950.0028.2526837.50
18SUMMING1850.0044237.50
19
20
21CODE
22FR-04
23ITEMDATEGOODSTYPEPRQTYUNITTOTAL
24105/01/2021STRWBERRAYFRMML350.0020.007000.00
25212/01/2021STRWBERRAYFRMML700.0023.4316400.00
26319/01/2021APPLEFRPPL1050.0030.1831687.50
27SUMMING2100.0055087.50
result


note: the data are increasable and changeable in sheet RETSEL
 
@HaHoBe
thanks again your code gives about 1.00 sec for me yes this is really fast than before ,but you ignore formatting
that's why your code is faster than Kvsrinivasamurthy . shouldn't ignore formatting .

 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try this code. Let us know How much time this code takes.
Code creates new sheet "Sheet2New"
VBA Code:
Sub DataColoured()
Dim col As Long, Lr As Long, T As Long, Ro As Long
Dim M
Application.ScreenUpdating = False
Sheets("Sheet1").Copy Before:=Sheets(1)
ActiveSheet.Name = "Sheet2New"
Lr = Range("B" & Rows.Count).End(xlUp).Row
col = Range("h9").Interior.Color
M = Filter(Evaluate("transpose(IF(Sheet1!B2:B" & Lr & "=""SUMMING"",Row(Sheet1!B2:B" & Lr & "),False))"), False, False)

For T = 0 To UBound(M)
If Range("H" & M(T)).Interior.Color <> col Then
With Range("H" & M(T)).CurrentRegion
.Resize(.Rows.Count + 2).Delete Shift:=xlUp
End With
End If
Next T
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this code also. Let us know How much time this code takes.
Code creates new sheet "Sheet3New"
VBA Code:
Sub DataColoured3()
Dim col As Long, Lr As Long, T As Long, Ro As Long
Dim M, Adr
Application.ScreenUpdating = False
Sheets("Sheet1").Copy Before:=Sheets(1)
ActiveSheet.Name = "Sheet3New"
Lr = Range("B" & Rows.Count).End(xlUp).Row
col = Range("h9").Interior.Color
M = Filter(Evaluate("transpose(IF(Sheet1!B2:B" & Lr & "=""SUMMING"",Row(Sheet1!B2:B" & Lr & "),False))"), False, False)

For T = 0 To UBound(M)
If Range("H" & M(T)).Interior.Color <> col Then
With Range("H" & M(T)).CurrentRegion
Adr = Adr & "," & .Resize(.Rows.Count + 2).Address
End With
End If
Next T
If Adr <> "" Then Adr = Mid(Adr, 2)
Range(Adr).Delete Shift:=xlUp
Application.ScreenUpdating = True
End Sub
 
Upvote 0
@kvsrinivasamurthy
Code in post#23 gives 1.10
Code in post#24 gives error method range of object _global failed n this line
VBA Code:
Range(Adr).Delete Shift:=xlUp
about after add new sheet , can you make just updtating data for the added new sheet instead of shows error when run the macro every time please?
 
Upvote 0
Hi KalilMe,

VBA Code:
Public Sub MrE_1226327_1700B0B()

' https://www.mrexcel.com/board/threads/copy-individual-range-based-on-highlighted-last-cell-from-sheet-to-another.1226327/

Dim wsTarg As Worksheet
Dim wsData As Worksheet
Dim lngStart As Long
Dim rngWork As Range
Dim rngTarg As Range
Dim dblStart As Double
Dim dblEnd As Double

dblStart = Timer

'change sheetnames to suit
Set wsData = Worksheets("SpeedTest")
Set wsTarg = Worksheets("result")
lngStart = 1

wsTarg.Range("A1:N1").EntireColumn.Delete

Application.ScreenUpdating = False
With wsData
  Do While lngStart < .Cells(.Rows.Count, "B").End(xlUp).Row
    Set rngTarg = .Cells(lngStart, "E").CurrentRegion
    With rngTarg
      If .Range("H" & .Rows.Count).Interior.Color = 52377 Then
        If rngWork Is Nothing Then
          Set rngWork = rngTarg.Resize(rngTarg.Rows.Count + 2)
        Else
          Set rngWork = Union(rngWork, rngTarg.Resize(rngTarg.Rows.Count + 2))
        End If
      End If
      lngStart = .Cells(.Rows.Count, .Columns.Count).End(xlDown).Row
    End With
  Loop
End With

With wsTarg
  rngWork.Copy wsTarg.Cells(1, 1)
  .Range("A1:H1").EntireColumn.AutoFit
  Application.Goto .Range("A1"), True
End With

dblEnd = Timer

Debug.Print "elapsed time for procedure 'MrE_1226327_1700B0B': " & dblEnd - dblStart & " sec"

Application.ScreenUpdating = False
Set rngWork = Nothing
Set rngTarg = Nothing
Set wsTarg = Nothing
Set wsData = Nothing

End Sub

Rich (BB code):
elapsed time for procedure 'MrE_1226327_1700B0B': 0,859375 sec

Holger
 
Upvote 0
I have removed the name for new sheet. If required pl change the name of the sheet manually.
I am not getting error while running macro. If problem is still there Pl upload file.
VBA Code:
Sub DataColoured3()
Dim col As Long, Lr As Long, T As Long, Ro As Long
Dim M, Adr
Application.ScreenUpdating = False
Sheets("Sheet1").Copy Before:=Sheets(1)
Lr = Range("B" & Rows.Count).End(xlUp).Row
col = Range("h9").Interior.Color
M = Filter(Evaluate("transpose(IF(Sheet1!B2:B" & Lr & "=""SUMMING"",Row(Sheet1!B2:B" & Lr & "),False))"), False, False)

For T = 0 To UBound(M)
If Range("H" & M(T)).Interior.Color <> col Then
With Range("H" & M(T)).CurrentRegion
Adr = Adr & "," & .Resize(.Rows.Count + 2).Address
End With
End If
Next T
If Adr <> "" Then Adr = Mid(Adr, 2): Range(Adr).Delete Shift:=xlUp
Application.ScreenUpdating = True
End Sub
 
Upvote 0
@kvsrinivasamurthy
magnificent !
gives 0.110 sec
I have removed the name for new sheet. If required pl change the name of the sheet manually
I don't need add new sheet every time when I run macro every time , just work at the sheet has already added . could be ,please?.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
Members
453,021
Latest member
Justyna P

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