Copy certain lines from one Worksheet to another Worksheet

Clamarc

New Member
Joined
Apr 19, 2023
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
HI,
I need to copy a row from one spreadsheet to another using the value of a specific column in the original spreadsheet as a parameter.
See attached image.

Thank you.
 

Attachments

  • Copying_rows_between_spreadsheets.jpg
    Copying_rows_between_spreadsheets.jpg
    159.9 KB · Views: 12

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this:

VBA Code:
Sub CopyRow()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim n As Long, lr As Long, lr2 As Long
  
  Application.ScreenUpdating = False
  
  Set sh1 = Sheets("Incidents")
  Set sh2 = Sheets("Incidents FR")
  n = sh1.ListObjects(1).ListColumns("Situation").Index
  sh1.ListObjects(1).Range.AutoFilter Field:=n, Criteria1:="Out of the Rules2"
  
  lr = sh2.Range("B:B").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row + 1
  sh1.AutoFilter.Range.Offset(1).Copy sh2.Range("B" & lr)
  lr2 = sh2.Range("B:B").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  sh2.Range("A" & lr & ":A" & lr2).Value = 1
  
  sh1.ShowAllData
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi,
the code worked, thanks!... but I have three questions:

1 - my IncidentsFR spreadsheet has a table at the end, and I needed the lines to be copied before this table.
2 - how do I make the code run when I open the file?
3 - when I saved the file with the code, it appears with an exclamation mark.

See image
 

Attachments

  • Lines_copied.jpg
    Lines_copied.jpg
    126.5 KB · Views: 7
Upvote 0
You can clarify the following, in your first image on the "Incidents FR" sheet the "Opened" column is in column "C" and in your second image it appears in column "B"
1727706338603.png


1 - my IncidentsFR spreadsheet has a table at the end, and I needed the lines to be copied before this table.
For future threads, you must enter all the information necessary to provide you with a complete response.
To make the adjustment to the macro, you need to clarify the point above.


2 - how do I make the code run when I open the file?
In the events of ThisWorkbook put the following code:
VBA Code:
Private Sub Workbook_Open()
  Call CopyRow
End Sub
NOTE: ThisWorkbook EVENT
- Open the VB Editor (press Alt + F11).
- Over in the Project Explorer, double click on ThisWorkbook.
- In the white panel that then appears, paste the above code.


3 - when I saved the file with the code, it appears with an exclamation mark.
That's because the file is now macro-enabled. ;)

----- --
I hope to hear from you soon.
Respectfully
Dante Amor
----- --
 
Upvote 0
1727728072929.png

The columns are different because before I used a SUM command and needed to have the value 1 in column A... later I discovered that I could use the COUNT.VALUES command, so I simply changed the code and it worked.


1727728161430.png

OK, sorry....
...I didn't mention the table at the end of my file, because I thought your code would copy the row from the "Incidents" sheet, in the first empty row it found in the "Incidents FR" sheet, but the code didn't consider row 180 as empty and ended up copying it to the row after the table!


1727728251935.png

Thaks!!!
 

Attachments

  • 1727728233406.png
    1727728233406.png
    3.6 KB · Views: 3
Upvote 0
Try this:

VBA Code:
Sub CopyRow()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim rng As Range, f As Range, r As Range
  Dim i As Long, j As Long, n As Long
  Dim newRow As ListRow
  Dim cell As String
  
  Application.ScreenUpdating = False
  
  Set sh1 = Sheets("Incidents")
  Set sh2 = Sheets("Incidents FR")
  
  n = sh1.ListObjects(1).ListColumns("Situation").Index
  
  Set r = sh1.Columns(n)
  Set f = r.Find("Out of the Rules2", , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      Set newRow = sh2.ListObjects(1).ListRows.Add
      For j = 2 To sh2.Range("B" & Rows.Count).End(3).Row
        If sh2.Range("B" & j).Value = "" Then
          sh1.Rows(f.Row).Copy sh2.Range("A" & j)
          Exit For
        End If
      Next
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If

  Application.ScreenUpdating = True
End Sub

🤗
 
Upvote 0
Try this:....

🤗
Hi,

The code copied the line below the TOTAL 2024 line... would it be possible for the line to be copied above the TOTAL 2024 line? See image.
 

Attachments

  • Copied_line.jpg
    Copied_line.jpg
    181.4 KB · Views: 3
Upvote 0
Is that total line inside the table?
In your previous image it was in column B, now it appears in column A.
If you don't put things in from the initial post and continually change them, no macro is going to work.
 
Upvote 0
Is that total line inside the table?
In your previous image it was in column B, now it appears in column A.
If you don't put things in from the initial post and continually change them, no macro is going to work.
yes the total line is inside the Table

As I had informed here...
...The columns are different because before I used a SUM command and needed to have the value 1 in column A, later I discovered that I could use the COUNT.VALUES command.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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