简体   繁体   中英

VBA - copy specific rows from one sheet into another sheet when user closes file

I'm new to VBA and I'm struggling a lot with a file I want to build.

I have a main Sheet that in a simple way looks like this (starting at column B - A is an empty column):

main

This is a simplified version just for the example. The first table of the sheet varies from B13 to O92, the second varies from B104 to O114 but some of those rows might be empty.

My goal is to join rows with content from the first area with rows with content from the second area in a different sheet (Sheet1), add to the left a column with 1s and "Cell 0" (content of cell B1). Using the example, the result would be something like this:

Sheet1

Sheet1 will stay hidden as I'm using it as a source of information to a different file. In fact, I may not need the 1s column if I find a way to copy information in a different way - I'm doing it like this (wsSource is Sheet1):

  lRow = wsSource.Columns("A").Find(1, SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Row
  wsSource.Range("B1:N" & lRow).Copy

I was trying to do it so Sheet1 is "emptied" when the file is opened and edited when file is closed - so that if new rows are added or information updated, it gets into Sheet1 every time.

I've tried several stuff I found online but couldn't make it to work. My main problem is adding the specified rows one after the others but I'm also struggling to reset Sheet1 every time the file is opened and automatically running the macro when file is closed.

Any help would be really appreciated.

Hopefully this will get you started. Both subs need to be pasted in VBE under ThisWorkBook rather module or sheet(n).

The first sub will execute when the workbook is open.
Are you sure you want to clear your sheet under these circumstances?
You will never have access to your table (without workarounds) since it will clear when opening every time .

If this is what you need, see the below method to clear a dynamic range (Col A - D down to last cell used in Col A) on Sheet1 every time the workbook that houses this code is opened.

Private Sub Workbook_Open()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim ClearRange As Range

Application.ScreenUpdating = False
    With ws
        .Visible = xlSheetVisible
            .Range("A1:D" & .Range("A" & .Rows.Count).End(xlUp).Row).ClearContents
        .Visible = xlSheetHidden
    End With
Application.ScreenUpdating = True

End Sub

Next, the below sub will only execute before the book is closing. This is where the bulk of your code will go to build your table.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Application.ScreenUpdating = False
    ThisWorkbook.Sheets(1).Visible = xlSheetVisible
        'Code goes here
    ThisWorkbook.Sheets(1).Visible = xlSheetHidden
Application.ScreenUpdating = True

End Sub

You will need to qualify your objects (Ranges, Cells, etc.) directly ( which sheet ) since the code will not be housed in a sheet. The first sub uses a With block to qualify ranges and the second sub directly qualifies all objects with ThisWorkbook.Sheets(1).

Lastly, I recommend writing your code to build your table inside a module (qualify ranges!) so you can test and debug without needing to close/open your book continuously. Once you have the code working in a module, you can copy and paste the code directly into the 2nd sub here and you are good to go!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM