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
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
VBA Code:
Sub dream()
        Dim lr As Long
        Dim Coloring As Long
        Dim wk1, wk2 As Worksheet
        Set wk1 = Sheets("Project1") 'this is the input sheet
        Set wk2 = Sheets("Project2") ' this is the output sheet aka result sheet
        Dim k As Integer
       
        ' make sure input sheet is selected at the time of running the code very important
       
        lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlWhole, xlByRows, xlPrevious, False).Row
        Coloring = 52377 'this is the color of your highlighted cell you can change it to your liking
       
        For k = 1 To lr
                If wk1.Range("H" & k).Interior.Color = Coloring Then
                    wk1.Range("H" & k).CurrentRegion.Copy _
                        wk2.Range("A" & Rows.Count).End(xlUp).Offset(4, 0)
                End If
        Next k
       
        wk2.Range("A1:A4").EntireRow.Delete shift:=xlUp
       
       

       
End Sub
 

Attachments

  • 1673176620251.png
    1673176620251.png
    50 KB · Views: 3
Upvote 0
VBA Code:
Sub dream2()
        ' this how your find the color of a cell
        
        MsgBox Range("H9").Interior.Color
        
End Sub
 
Upvote 0
thanks but seem slow when I have data 8000 rows . it takes more time .
do you have a way to make faster ,please?
 
Upvote 0
Hi KalilMe,

what about

VBA Code:
Public Sub MrE_1226327_1700811()
' 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

'/// Please note: ALWAYS run the macros on a copy of your Workbook !!!!

Dim wsTarg As Worksheet
Dim wsData As Worksheet
Dim lngStart 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

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.ColorIndex = 6 Then
        Set rngHit = wsTarg.Columns(5).Find(.Cells(2, "E").Value, LookIn:=xlValues)
        If Not rngHit Is Nothing Then
          Set rngTarg = rngHit.CurrentRegion
          rngTarg.Cells(rngTarg.Rows.Count, "H").Value = .Range("H" & .Rows.Count).Value
          Set rngTarg = Nothing
          Set rngHit = Nothing
        End If
      End If
      lngStart = .Cells(.Rows.Count, .Columns.Count).End(xlDown).Row
    End With
    Set rngWork = Nothing
  Loop
End With

Application.ScreenUpdating = False
Set wsTarg = Nothing
Set wsData = Nothing
End Sub

Ciao,
Holger
 
Upvote 0
@HaHoBe
thanks but I no know what's the problem !
there is no error and doesn't show anything in result sheet
 
Upvote 0
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, rng As Range, srcRng As Range
    With Sheets("RETSEL")
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("A1:H" & LastRow).AutoFilter Field:=8, Criteria1:=RGB(153, 204, 0), Operator:=xlFilterCellColor
        Set srcRng = .Range("H2:H" & LastRow).SpecialCells(xlCellTypeVisible)
        .Range("A1").AutoFilter
        For Each rng In srcRng
            .Range("H" & rng.Row).CurrentRegion.Copy Sheets("result").Cells(Sheets("result").Rows.Count, "A").End(xlUp).Offset(4)
        Next rng
    End With
    Sheets("result").Range("A1").Resize(4).EntireRow.Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi KalilMe,

VBA Code:
Public Sub MrE_1226327_1700811_mod()
' 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

'/// Please note: ALWAYS run the macros on a copy of your Workbook !!!!

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")

wsTarg.Cells.Clear

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.ColorIndex = 6 Then
        wsTarg.Cells(lngCopy, 1).Resize(.Rows.Count, .Columns.Count).Value = .Value
        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

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

Holger
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

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