Need Help with VBA Code, Not sure if its possible.

Giovanni03

New Member
Joined
May 23, 2023
Messages
33
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello Everyone!

I've been thinking of a way to improve a certain function that I currently use on on one my of workbooks. Its a copy a paste code that allows me to copy any amount of rows that i select over into another sheet. What I do now is highlight specific words in Column I then with those highlighted cells I'm able to identify what I need to move over onto another sheet. If you're thinking why don't you just had a set up where those words in column I are identified and copied is because they're only a part of a larger order. That's were I'm stuck...

Is it at all possible if cells are highlighted (or Bold) in Column I then it'll copy paste all of the rows based on the order # (Column D) into a new sheet? As well as delete all of the copied information from the first sheet that it copied it from (avoid duplicate data)

So basically, If cells in column I are highlighted then in column D the order number will be the same.

See below of an example of what I'm talking about,

Column D Column I
Order # 324512
Customer NameSERVICE
9410101​
Product Description
Order # 324512
0:40:00​
Customer NameSERVICE
5246600​
Product Description
Order # 324512
0:40:00​
Customer NameDELIVERY
3762114​
Product Description (Highlighted Cell)
Order # 324512
0:40:00​
Customer NameDELIVERY
5770014​
Product Description
Order # 324512
0:40:00​
Customer NameDELIVERY
5446387​
Product Description
Order # 324512
0:40:00​
Customer NameDELIVERY
8930357​
Product Description (Highlighted Cell)
Order # 324512
0:40:00​
Customer NameDELIVERY
9282812​
Product Description
Order # 324512
0:40:00​
Customer NameDELIVERY
6451044​
Product Description (Highlighted Cell)
 
Re: "Basically trying to have it so if any part of an order has a 1 in column J then paste all rows that include that order # into new sheet"
Why do you have a 1 twice with Order #55084 in Post #7?
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this:
Data start at row 2
Make sure "Sheet2" exist.
VBA Code:
Sub Giovanni03()
Dim i As Long, h As Long, n As Long
Dim va, vb
Dim d As Object

n = Range("D" & Rows.Count).End(xlUp).Row
'Data start at row 2
va = Range("D2:D" & n)
vb = Range("J2:J" & n)

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
   
    For i = 1 To UBound(vb, 1)
        If vb(i, 1) = 1 Then d(va(i, 1)) = Empty
    Next

    For i = 1 To UBound(va, 1)
        If d.exists(va(i, 1)) Then vb(i, 1) = 1
    Next

Range("K2").Resize(UBound(vb, 1), 1) = vb

Range("A1:K" & n).Sort Key1:=Columns(11), Order1:=xlAscending, Header:=xlYes

h = WorksheetFunction.Sum(Range("K1:K" & n))
'copy data to Sheet2
Range("A2").Resize(h, 9).Copy Sheets("Sheet2").Range("A1")


End Sub
Oh Man this code is Super close!!!
Definitely excited to see it in action the only thing (and its my fault for not previously mentioning) is that my headers on the sheet that I use this on start on the second row and the third is where the data begins. When I run the code my headers disappear to and get sorted down to the end of the last row that was copied. Also where its being pasted into (sheet 2) also has headers which is getting pasted over.
 
Upvote 0
Re: "Basically trying to have it so if any part of an order has a 1 in column J then paste all rows that include that order # into new sheet"
Why do you have a 1 twice with Order #55084 in Post #7?
That order has two items that are identified when i highlight the specific items im looking for.
 
Upvote 0
my headers on the sheet that I use this on start on the second row and the third is where the data begins.
If data start at row 3 then:
VBA Code:
Sub Giovanni03_1()
Dim i As Long, h As Long, n As Long
Dim va, vb
Dim d As Object


n = Range("D" & Rows.Count).End(xlUp).Row
'Data start at row 3
va = Range("D3:D" & n)
vb = Range("J3:J" & n)

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
    
    For i = 1 To UBound(vb, 1)
        If vb(i, 1) = 1 Then d(va(i, 1)) = Empty
    Next

    For i = 1 To UBound(va, 1)
        If d.exists(va(i, 1)) Then vb(i, 1) = 1
    Next

Range("K3").Resize(UBound(vb, 1), 1) = vb

Range("A2:K" & n).Sort Key1:=Columns(11), Order1:=xlAscending, Header:=xlYes

h = WorksheetFunction.Sum(Range("K1:K" & n))

Range("A3").Resize(h, 9).Copy Sheets("Sheet2").Range("A1")


End Sub
 
Upvote 1
Solution
If data start at row 3 then:
VBA Code:
Sub Giovanni03_1()
Dim i As Long, h As Long, n As Long
Dim va, vb
Dim d As Object


n = Range("D" & Rows.Count).End(xlUp).Row
'Data start at row 3
va = Range("D3:D" & n)
vb = Range("J3:J" & n)

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
   
    For i = 1 To UBound(vb, 1)
        If vb(i, 1) = 1 Then d(va(i, 1)) = Empty
    Next

    For i = 1 To UBound(va, 1)
        If d.exists(va(i, 1)) Then vb(i, 1) = 1
    Next

Range("K3").Resize(UBound(vb, 1), 1) = vb

Range("A2:K" & n).Sort Key1:=Columns(11), Order1:=xlAscending, Header:=xlYes

h = WorksheetFunction.Sum(Range("K1:K" & n))

Range("A3").Resize(h, 9).Copy Sheets("Sheet2").Range("A1")


End Sub
Thank you so much for you help!!! it works perfectly!!!!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,063
Members
449,090
Latest member
fragment

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