Close VBA and Return to last Data row of Data

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
My code pulls data from one sheet to another. I'm not sure how I to close the srcSht (its bolded in the code) at the end. As well as return to the last row of data on the destSHT, since it goes to the very bottom once the data has been copied over.
Rich (BB code):
Sub Macro()
    Dim destSht As Worksheet, srcSht As Worksheet
    Dim src_ColCnt As Integer, dest_ColCnt As Integer
   
    Set srcSht = Workbooks.Open("D:\data.xlsx").Sheets("Sheet1")
    Set destSht = Workbooks.Open("D:\report.xlsx").Sheets("Sheet1")
   
    dest_ColCnt = destSht.Range("A1").End(xlToRight).Column
    src_ColCnt = srcSht.Range("A1").End(xlToRight).Column
    src_RCnt = srcSht.Range("A1").End(xlDown).Row - 1
   
    For i = 1 To src_ColCnt
        Header = srcSht.Cells(1, i)
        For j = 1 To dest_ColCnt
            If destSht.Cells(1, j).Value = Header Then
                For r = 1 To src_RCnt
                    destSht.Cells(r + 1, j).Value = srcSht.Cells(r + 1, i).Value
                Next r
            End If
        Next j
    Next i
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I'm not sure how I to close the srcSht (its bolded in the code)
You don't close sheets. You close workbooks.

So, you would close it like:
VBA Code:
Windows("data.xlsx").Close

As well as return to the last row of data on the destSHT, since it goes to the very bottom once the data has been copied over.
You could do that like this:
VBA Code:
destSHT.Activate
Range("A1").SpecialCells(xlLastCell).Select
 
Upvote 0
You don't close sheets. You close workbooks.

So, you would close it like:
VBA Code:
Windows("data.xlsx").Close


You could do that like this:
VBA Code:
destSHT.Activate
Range("A1").SpecialCells(xlLastCell).Select
VBA Code:
Windows([B]"data.xlsx")[/B].Close
->I'm automating a process, so I'm trying to reduce as much manual work as possible. is there a way to close it automatically without inputting the name everytime?
 
Upvote 0
I'm automating a process, so I'm trying to reduce as much manual work as possible. is there a way to close it automatically without inputting the name everytime?
Create a workbook variable to store the name in, much like you have already done with the worksheet variables, i.e.
VBA Code:
Dim wb as Workbook
Set wb = ....
Then you can close it later just like this:
VBA Code:
wb.Close

See here for more information on workbook variables: Set Workbook
 
Upvote 0
Hi !​

Yes obviously from your worksheet reference : srcSht.Parent.Close …​
Sorry, I'm new to VBA. You put the ellipsis after "srcSht.Parent.Close" , what would I have to write after?
 
Upvote 0
Try first as it is ! As if the source sheet is not modified this is enough. See also the VBA help for Workbook.Close …​
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,834
Members
449,192
Latest member
mcgeeaudrey

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