VBA needed to separate project from 1 worksheet to 3

marialewis16

New Member
Joined
Sep 22, 2023
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I got a great little code from someone named BSB a couple of weeks ago on the other excel forum. However, I changed my project a little and need an adjustment to it that I can't seem to figure out.

His code worked perfectly for what I had asked for at the time. Basically I would like to enter a number into a cell and then that number of rows from the bottom of a Table would populate to the top of a Range in different columns. The code here does this well when everything is on one worksheet.

However, I need help separating the parts to different worksheets. I would like the Data Source Table to be on Sheet1, the input cell (where I would type in the number of rows I want) to be on Sheet2 and the Range where the rows are sent to would be on sheet3.

Also, I would like the new Range to actually be a Table and have the empty rows that you get when reducing the number in the input cell to be deleted. Hopefully that can be coded as well.

I appreciate any help you guys can give me, thank you so much!
-Maria

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x As Long
    
    With ThisWorkbook.Sheets("Sheet1")
        Application.EnableEvents = False
        If Target.Cells.Count = 1 Then
            .Range("K2:N" & .Cells(Rows.Count, "K").End(xlUp).Row).ClearContents
            
            x = .Range("I3")
            .Range("K2").Resize(x, 4).Value = .ListObjects("Table1").Range(.ListObjects("Table1").ListRows.Count + 1, 1).Offset(-x + 1).Resize(x, 4).Value
        End If
        Application.EnableEvents = True
    End With
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Retrieve a specified number of rows from a Table
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
I am sorry, I thought these forums were completely separate. I was trying to increase my odds of getting a faster answer. Won't happen again, thanks for letting me know.
 
Upvote 0
They are completely different, that's why it's cross-posted. If you follow the link given in the rules & read what is said you would understand.
 
Upvote 0
This has now been marked as solved on EF
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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