Macro to refresh workbook, clear sheet, refer to path and import columns

Retroshift

Board Regular
Joined
Sep 20, 2016
Messages
119
Office Version
  1. 2019
Platform
  1. Windows
Hi,
In my worksheet, I would like to add a macro which does the following at once: 1) refresh the active workbook, 2) clear the contents of a specific worksheet, 3) import columns data from a closed workbook, 4) refer to the path of the closed workbook based on cell data in the open workbook, 5) identify the columns data until the last row, and 6) add a messagebox.
Below is what I got so far but it is not complete. Does anyone know how to make the macro work? Many thanks in advance

VBA Code:
Sub RefreshClearCopyColumnsData()

'First refresh the open workbook to update all the values

ActiveWorkbook.RefreshAll

'Then clear the contents of the worksheet in the open workbook

wkBk.Sheets("Sheet1").Cells.ClearContents

'Then get columns data from closed workbook into open workbook

Dim lastRow As Long
Dim myApp As Excel.Application
Dim wkBk As Workbook
Dim wkSht As Object

Set myApp = CreateObject("Excel.Application")

'Get path and Closed Workbook filename from (indirect?) cell A5 in open workbook

Set wkBk = myApp.Workbooks.Open("[(indirect) path reference from sheet2!A5 in open workbook]")

'Specify the columns until the last row with data

lastRow = wkBk.Sheets(1).Range("A,O,R,V,AD,AG,AH" & Rows.Count).End(xlUp).Row
wkBk.Sheets(1).Range("A1:A,O1:O,R1:R,V1:V,AD1:AD,AG1:AG,AH1:AH" & lastRow).Copy
myApp.DisplayAlerts = False
wkBk.Close
myApp.Quit
Set wkBk = Nothing
Set myApp = Nothing
Set wkBk = ActiveWorkbook
Set wkSht = wkBk.Sheets("Sheet1")
wkSht.Activate
Range("C2").Select
wkSht.Paste
Exit Sub

'Add messagebox

MsgBox “Data successfully imported”

End Sub
 
I meant I want to only keep the first 10 characters of the data in every cell of the single column O. For example:
if it imports "16/05/2022 11:00:00" into a cell in column O, the amount of characters should get cropped down to only the date (10 characters in this case) and not the time, so "16/05/2022".
And when a value of "16/05/2022" gets imported (merely the date (10 characters)), it is fine and it can stay there without any VBA action to it.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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: Macro - OzGrid Free Excel/VBA Help Forum
If you have posted the question at more 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 meant I want to only keep the first 10 characters of the data in every cell of the single column O. For example:
if it imports "16/05/2022 11:00:00" into a cell in column O, the amount of characters should get cropped down to only the date (10 characters in this case) and not the time, so "16/05/2022".
And when a value of "16/05/2022" gets imported (merely the date (10 characters)), it is fine and it can stay there without any VBA action to it.

You would need a separate subroutine dedicated to doing that, and run it after Sub RefreshClearCopyColumnsData completes. That is too far outside the scope of this thread so you should start a new thread for that.
 
Upvote 0
You would need a separate subroutine dedicated to doing that, and run it after Sub RefreshClearCopyColumnsData completes. That is too far outside the scope of this thread so you should start a new thread for that.
True. I started a separate thread on this.
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,703
Members
452,667
Latest member
vanessavalentino83

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