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

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
382
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
 
Hi KalilMe,

VBA Code:
Public Sub MrE_1226327_1700811_mod02()
' https://www.mrexcel.com/board/threads/copy-individual-range-based-on-highlighted-last-cell-from-sheet-to-another.1226327/

'Assumptions: Code-Nr to transfer to is located on Column E in the second row of the area to work on
'             "SUMMING" is in capital letter and located on Column B in the last row of the area to work on
'             ColorIndex for highlighting is vbYellow or 6
'             Value to transfer is located on Column H in the last row of the area to work on

'changed the color to check from 6 (vbYellow) to 52377

Dim wsTarg As Worksheet
Dim wsData As Worksheet
Dim lngStart As Long
Dim lngCopy As Long
Dim rngWork As Range
Dim rngTarg As Range
Dim rngHit As Range

'change sheetbames to suit
Set wsData = Worksheets("RETSEL")
Set wsTarg = Worksheets("result")
lngStart = 1
lngCopy = 1

Application.ScreenUpdating = False
With wsData
  Do While lngStart < .Cells(.Rows.Count, "B").End(xlUp).Row
    Set rngWork = .Cells(lngStart, "E").CurrentRegion
    With rngWork
      If .Range("B" & .Rows.Count).Value = "SUMMING" And _
          .Range("H" & .Rows.Count).Interior.Color = 52377 Then
        .Copy wsTarg.Cells(lngCopy, 1)
        lngCopy = wsTarg.Cells(wsTarg.Rows.Count, "H").End(xlUp).Row + 3
      End If
      lngStart = .Cells(.Rows.Count, .Columns.Count).End(xlDown).Row
    End With
  Loop
End With

With Sheets("result")
  .Range("A1:H1").EntireColumn.AutoFit
  Application.Goto .Range("A1")
End With

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

End Sub

Holger
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
@HaHoBe
finally works but not as what I want.
first is still slow like @shinigamilight
second your code repeats copying to the bottom when run the macro everytime .
It should just replace when update data. I mean clear in sheet result before bring data.
Thanks for your trying.
 
Upvote 0
Hi KalilMe,

please define slow. Have you ever considered your data setup to be a possible reason for the runtime of the provided codes?

On a test the Immediate Window brought up this when a timer had been used:

Rich (BB code):
elapsed time for procedure: 4,126953125 sec
last row to check: 10.145

How fast do you think the procedure should be?


Regarding the multiple use of the procedure just add one line of code like

VBA Code:
lngCopy = 1

'delete data on sheet result
wsTarg.Range("A1:N1").EntireColumn.Delete

Application.ScreenUpdating = False

And I really wonder as this has never been mentioned before as it has not been part of any code supplied.

Holger
 
Upvote 0
Knowing that it relies on the computer used, Standalone or Network, OS, Processor, RAM, Applications running, HDD I wanted to know about the times for the 3 different codes supplied on my computer (Laptop Standalone no Network, Windows11, 8 GB RAM, Shared Memory Graphics, OS, Internet Securoty, InternetConnection, FireFox, Excel2019):

Rich (BB code):
elapsed time for procedure 'dream': 3,95703125 sec
elapsed time for procedure 'MrE_1226327_1700811_mod03': 3,921875 sec
elapsed time for procedure 'CopyData': 3,921875 sec
----
elapsed time for procedure 'MrE_1226327_1700811_mod03': 4,09765625 sec
elapsed time for procedure 'CopyData': 3,95703125 sec
elapsed time for procedure 'dream': 3,89453125 sec
----
elapsed time for procedure 'CopyData': 3,94921875 sec
elapsed time for procedure 'dream': 3,90625 sec
elapsed time for procedure 'MrE_1226327_1700811_mod03': 3,90625 sec

I modified the codes supplied to delete any content on sheet "result" and implemented the timer. In "dream" I added the object to the worksheet "RETSEL" for lr and disabled ScreenUpdating for running of the code (changedk to be of Type Long as lr is of that Type). The difference of 0.02 seconds does not point out which code is fastest (I would choose any of the other codes as they are much shorter than mine).

Holger
 
Upvote 0
@HaHoBe
this is what I got
macro dream is 10.3 sec
macro CopyData is 6.8
macro MrE_1226327_1700811_mod02 is 7.2
so nobody using at least (array & dic) to make fast I believe it can achieve to reach 2.5 is acceptable
thanks for all
 
Upvote 0
Hi KalilMe,

as you know how to solve the problem I would be glad to learn how to integrate the background color into either array or dictionary. Please let me know - I'm old but still trying to learn new tricks.

Holger
 
Upvote 0
Hi KalilMe,

my remarks above where meant to transfer the setup without any change to an array. If I enter another column (showing the color of the cells in the last column of the original data) a code like the following should work:

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

' using Arrays to copy over the values
' prior to assigning the range to the array a new column will be inserted to the right:
'     reading out the color for the last column of original data by UDF
' additional column will be deleted at end of procedure

Dim dblStart As Double
Dim dblEnd As Double

Dim lngStart As Long
Dim lngEnd As Long
Dim lngLast As Long
Dim lngNewCol As Long
Dim lngLoop As Long
Dim lngVar As Long
Dim lngWrite As Long
Dim lngCnt As Long

Dim arr() As Variant
Dim var() As Variant

Dim wsTest As Worksheet
Dim wsTarg As Worksheet
Dim wsData As Worksheet

Application.ScreenUpdating = False
dblStart = Timer

'please adjust the sheetnames
Set wsData = Worksheets("SPEEDTEST")
Set wsTarg = Worksheets("result")

wsTarg.Cells.Delete

With wsData
  lngLast = .Cells(.Rows.Count, "H").End(xlUp).Row
  lngNewCol = .Cells(3, .Columns.Count).End(xlToLeft).Column + 1
  
  .Cells(1, lngNewCol).EntireColumn.Insert
  With .Range(.Cells(1, lngNewCol), .Cells(lngLast, lngNewCol))
    .FormulaR1C1 = "=fncCellInteriorColor(RC[-1])"
    .Value = .Value
  End With
  
  arr = .Range("A1", .Cells(lngLast, lngNewCol)).Value
  
  For lngCnt = 1 To UBound(arr, 1)
    If arr(lngCnt, lngNewCol) = 65535 Then
      lngStart = lngCnt - 2
    ElseIf arr(lngCnt, lngNewCol) = 52377 Then
      lngEnd = lngCnt
      ReDim var(lngEnd - lngStart + 2, 8)
      lngWrite = 0
      For lngLoop = lngStart To lngEnd
        lngWrite = lngWrite + 1
        For lngVar = 1 To 8
          var(lngWrite, lngVar - 1) = arr(lngLoop, lngVar)
        Next lngVar
      Next lngLoop
      wsTarg.Cells(wsTarg.Rows.Count, 1).End(xlUp).Offset(3).Resize(UBound(var), 8).Value = var
      Erase var
    End If
  Next lngCnt
  .Cells(1, lngNewCol).EntireColumn.Delete
End With
With wsTarg
  .Range("A1").Resize(4).EntireRow.Delete
  .Range("A1:H1").EntireColumn.AutoFit
  Application.Goto .Range("A1"), True
End With

dblEnd = Timer
Application.ScreenUpdating = True

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

End Sub

Function fncCellInteriorColor(rng As Range) As Long
  fncCellInteriorColor = rng.Interior.Color
End Function

I haven't found a way to access the rows of the orignal array at once so another array is used and filled via loop (I know: old, learning nothing new and rusty). On Worksheets("SPEEDTEST") going up to row 10145 timer showed

Rich (BB code):
elapsed time for procedure 'MrE_1226327_1700A0F': 0,75390625 sec

Please make sure that the UDF is copied over as well (in my test both were located in the same module) and that there is only one function of that name present in the workbook.

Holger
 
Upvote 0
Try this this faster.
VBA Code:
Sub DataColoured()
Dim col As Long, Lr As Long, T As Long, Ro As Long
Dim M
Application.ScreenUpdating = False
With Sheets("Sheet1")
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)
Sheets("Sheet2").Cells.Clear
For T = 0 To UBound(M)
If .Range("H" & M(T)).Interior.Color = col Then
If T = 0 Then Ro = 0 Else Ro = 5
.Range("H" & M(T)).CurrentRegion.Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(Ro, 0)
End If
Next T
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi KalilMe,

my remarks above where meant to transfer the setup without any change to an array. If I enter another column (showing the color of the cells in the last column of the original data) a code like the following should work:

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

' using Arrays to copy over the values
' prior to assigning the range to the array a new column will be inserted to the right:
'     reading out the color for the last column of original data by UDF
' additional column will be deleted at end of procedure

Dim dblStart As Double
Dim dblEnd As Double

Dim lngStart As Long
Dim lngEnd As Long
Dim lngLast As Long
Dim lngNewCol As Long
Dim lngLoop As Long
Dim lngVar As Long
Dim lngWrite As Long
Dim lngCnt As Long

Dim arr() As Variant
Dim var() As Variant

Dim wsTest As Worksheet
Dim wsTarg As Worksheet
Dim wsData As Worksheet

Application.ScreenUpdating = False
dblStart = Timer

'please adjust the sheetnames
Set wsData = Worksheets("SPEEDTEST")
Set wsTarg = Worksheets("result")

wsTarg.Cells.Delete

With wsData
  lngLast = .Cells(.Rows.Count, "H").End(xlUp).Row
  lngNewCol = .Cells(3, .Columns.Count).End(xlToLeft).Column + 1
 
  .Cells(1, lngNewCol).EntireColumn.Insert
  With .Range(.Cells(1, lngNewCol), .Cells(lngLast, lngNewCol))
    .FormulaR1C1 = "=fncCellInteriorColor(RC[-1])"
    .Value = .Value
  End With
 
  arr = .Range("A1", .Cells(lngLast, lngNewCol)).Value
 
  For lngCnt = 1 To UBound(arr, 1)
    If arr(lngCnt, lngNewCol) = 65535 Then
      lngStart = lngCnt - 2
    ElseIf arr(lngCnt, lngNewCol) = 52377 Then
      lngEnd = lngCnt
      ReDim var(lngEnd - lngStart + 2, 8)
      lngWrite = 0
      For lngLoop = lngStart To lngEnd
        lngWrite = lngWrite + 1
        For lngVar = 1 To 8
          var(lngWrite, lngVar - 1) = arr(lngLoop, lngVar)
        Next lngVar
      Next lngLoop
      wsTarg.Cells(wsTarg.Rows.Count, 1).End(xlUp).Offset(3).Resize(UBound(var), 8).Value = var
      Erase var
    End If
  Next lngCnt
  .Cells(1, lngNewCol).EntireColumn.Delete
End With
With wsTarg
  .Range("A1").Resize(4).EntireRow.Delete
  .Range("A1:H1").EntireColumn.AutoFit
  Application.Goto .Range("A1"), True
End With

dblEnd = Timer
Application.ScreenUpdating = True

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

End Sub

Function fncCellInteriorColor(rng As Range) As Long
  fncCellInteriorColor = rng.Interior.Color
End Function

I haven't found a way to access the rows of the orignal array at once so another array is used and filled via loop (I know: old, learning nothing new and rusty). On Worksheets("SPEEDTEST") going up to row 10145 timer showed

Rich (BB code):
elapsed time for procedure 'MrE_1226327_1700A0F': 0,75390625 sec

Please make sure that the UDF is copied over as well (in my test both were located in the same module) and that there is only one function of that name present in the workbook.

Holger
Amazing code, I could have never come up with this. :oops:
 
Upvote 0
Hi kvsrinivasamurthy,

running your code on my sheet SpeedTest brings up

VBA Code:
elapsed time for procedure 'DataColoured': 4,015625 sec
elapsed time for procedure 'DataColoured': 3,796875 sec
elapsed time for procedure 'DataColoured': 3,9921875 sec


@shinigamilight:

it took some time for me to figure this one out. I started from a totally different point and am still loking for a way to avoid the loops for passing the data. ;)

Holger
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

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