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

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
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
 
Use the latest code.
After updating the data in sheet1 run macro once again. Sheet2 will have all the required from Sheet1.
During the process of macro run what is happening do not worry. See only final result.
If problem is not solved upload a sample file. showing the result.
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this code. Names of sheets changed to RETSEL and result.
VBA Code:
Sub DataColoured3()
Dim col As Long, Lr As Long, T As Long, Ro As Long
Dim M, Adr
Dim Sh1, Sh2 As Worksheet
Set Sh1 = Sheets("RETSEL"): Set Sh2 = Sheets("result")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
Sh2.Delete
On Error GoTo 0
Sh1.Copy After:=Sh1
ActiveSheet.Name = "result"
Lr = Range("B" & Rows.Count).End(xlUp).Row
col = 16777215
M = Filter(Evaluate("transpose(IF(RETSEL!B2:B" & Lr & "=""SUMMING"",Row(RETSEL!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 Range(Mid(Adr, 2)).Delete Shift:=xlUp
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Try this code also.
VBA Code:
Sub DataColoured3()
Dim col As Long, Lr As Long, T As Long, Ro As Long
Dim M, Adr
Dim Sh1, Sh2 As Worksheet
Set Sh1 = Sheets("RETSEL"): Set Sh2 = Sheets("result")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
Sh2.Delete
On Error GoTo 0
Sh1.Copy After:=Sh1
ActiveSheet.Name = "result"
Lr = Range("B" & Rows.Count).End(xlUp).Row
col = 16777215
M = Filter(Evaluate("transpose(IF(RETSEL!B2:B" & Lr & "=""SUMMING"",Row(RETSEL!B2:B" & Lr & "),False))"), False, False)

For T = UBound(M) To 0 Step -1
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
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Hi kvsrinivasamurthy,

Sheet RETSEL on my sample workbook holds data up to row 10145.

#42 still will raise the very same error 1004 caused by Adr, breaking the code so no time is available.

#43 will run, time is 18,828125 sec (the times listed in #14 are based on the same data set but all codes keep sheet result not delete and copy data Sheet2/result and these are all close to 4 sec).

Again: all codes are run for the same data sheet (no matter how it is called): I wonder what I am doing wrong as your code was labelled to be the fastest of all by KalilMe.

Maybe you should update your userprofile. ;)

Holger
 
Upvote 0
I tested with about 4,500 rows and this was about 5 times faster than the post #43 code.
From what I can tell the results are the same.

VBA Code:
Sub Copy_Blocks()
  Dim a As Variant
  Dim nc As Long, j As Long, rws As Long
  Dim rA As Range
  
  Dim tm As Double
  tm = Timer
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  On Error Resume Next
  Sheets("result").Delete
  On Error Resume Next
  Application.DisplayAlerts = True
  Sheets.Add(After:=Sheets("RETSEL")).Name = "result"
  With Sheets("RETSEL")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    ReDim a(1 To .Range("E" & Rows.Count).End(xlUp).Row + 3, 1 To 1)
    For Each rA In .Range("E1", .Range("E" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
      rws = rA.Rows.Count + 3
      If Not rA.Cells(rws - 2, 4).Interior.Color = 16777215 Then
        For j = rA.Row To rA.Row + rws - 1
          a(j, 1) = 1
        Next j
      End If
    Next rA
    With .Cells(1, nc).Resize(UBound(a))
      .Value = a
      .SpecialCells(xlConstants).EntireRow.Copy Destination:=Sheets("result").Range("A1")
      .ClearContents
    End With
    Sheets("result").Cells(1, nc).Resize(UBound(a)).ClearContents
    Sheets("result").UsedRange.EntireColumn.AutoFit
  End With
  Application.ScreenUpdating = True
  MsgBox Format(Timer - tm, "0.00")
End Sub
 
Upvote 0
Solution
I tested with about 4,500 rows and this was about 5 times faster than the post #43 code.
are you sure?
I'm really surprised from that !!
in reality the code in post#43 is faster than code in post #45
I run the macro about more than 10 times for each code
code in post#43 gives from 0.110 to 0.120
code in post #45 gives from 0.200 to 0.220
In fact, you have a preference with the code compared to the code in post#43, because it copies without specifying where a SUMMING word is located, unlike the code in post #43 . If it was an error in its location, a problem with the code would occur. For this reason, I chose the mark for your solution. As for the speed, for me, it is not a big difference.
thanks Peter for your solution
 
Upvote 0
Hi KalilMe,

I was cuious and developped a workbook where all codes from this thread are covered and started from one procedure so that all work on the same dataset. I can assure you that Peter_SSs on this dataset is a lot faster than the praised code from #43.

VBA Code:
Sub StartAllMacrosFormHere()

  Const cblnRun As Boolean = False

  dream_02
  MrE_1226327_05
  CopyData_07
  MrE_1226327_08
  MrE_1226327_17
  DataColoured_18
  If cblnRun Then DataColoured_23   'msgbox to avoid RTE for adding Sheet
  If cblnRun Then DataColoured_24   'error for this setup
  MrE_1226327_26
  If cblnRun Then DataColoured_27   'error for this setup
  If cblnRun Then DataColoured_33   'error for this setup
  If cblnRun Then DataColoured_35   'error for this setup
  If cblnRun Then DataColoured_39   'error for this setup
  If cblnRun Then DataColoured_42   'error for this setup
  DataColoured_43
  Copy_Blocks_45
 
End Sub

Result from the immediate window_

Rich (BB code):
2,5859375 sec for procedure 'dream_02' by shinigamilight
1,9609375 sec for procedure 'MrE_1226327_05' by HaHoBe
2,46875 sec for procedure 'CopyData_07' by mumps
2,5390625 sec for procedure 'MrE_1226327_08' by HaHoBe
1 sec for procedure 'MrE_1226327_17' by HaHoBe - only copying values
2,3984375 sec for procedure 'DataColoured_18' by kvsrinivasamurthy - please check for areas copied to result
0,515625 sec for procedure 'MrE_1226327_26' by HaHoBes
12,359375 sec for procedure 'DataColoured_43' by kvsrinivasamurthy
1,43 sec for procedure 'Copy_Blocks_45' by Peter_SSs

Workbook maybe downloaded here, Size is 554 KB. Dataset goes up to Row 9934. Macros need to get started from IDE/MacroBox.

Added Code in Workbook_Open/DieseArbeitsmappe:

VBA Code:
Private Sub Workbook_Open()
  With tblRemarks
    .Move After:=Worksheets(Worksheets.Count)
    .Activate
  End With
End Sub

Waiting for your results when running StartAllMacrosFormHere

Ciao,
Holger
 
Upvote 0
Update:

the formatting of time for PeterSSs wasn't correct, I altered the code to

VBA Code:
  Application.ScreenUpdating = True
  Dim tmEnd As Double
  tmEnd = Timer
  Debug.Print tmEnd - tm & " sec for procedure 'Copy_Blocks_45' by Peter_SSs"

and got

Rich (BB code):
0,390625 sec for procedure 'Copy_Blocks_45' by Peter_SSs
0,421875 sec for procedure 'Copy_Blocks_45' by Peter_SSs
0,390625 sec for procedure 'Copy_Blocks_45' by Peter_SSs

Holger
 
Upvote 0
@HaHoBe

you're right about the speed for your file .
in reality I tested just post #26,43,45 (theses solve my requiremnts)
despite of you don't use borders and some colors like my file contains colors not just the headers also the lastrow contains color , even if that my testing is faster than you . this is strange!!
just guess , is the reason from using formulas in some ranges make slow the codes as you use that ?
actually I have not formulas at all on my file or OP . and I tested for 8000 rows for all of the codes .
 
Upvote 0

Forum statistics

Threads
1,215,833
Messages
6,127,153
Members
449,366
Latest member
reidel

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