Enhancing my current VBA

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I have a very peculiar request. I currently run a process where the date to use is driven off the folder name (the date to run it for). I am looking for a way to remove the human element of someone typing in the day and including it somehow in my VBA or the use of a formula? see below for an example. Currently I need to modify the yellow shaded. in hindsight the worksheet that runs the code is in the folder anyways so could it just use that to drive the date to run other worksheets that reside in that same folder as well?

Book1
AB
1PathD:\ME\Documents\Files\House\Home\12.20.20\Monthly Credit Card breakdown.xlsx
2Run Date PRERun Date Post
312/20/202012/20/2020
4
5
6
7
Sheet1
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
VBA Code:
Sub Pre()
Dim fromPath As String
Dim Cus$
Dim MyDate$
Dim Fd$
Dim For$
Dim wkb As Workbook, wkbFrom As Workbook
Dim rws As Long
Dim i As Long
Dim lr As Long
Dim StartTime As Double
Dim TimeTaken As String
   
StartTime = Timer

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False

'   Check if WKB is open if open (close). if not continue
If WorkbookIsOpen(fromPath & "_ALL.xlsm") Then
Workbooks(fromPath & "_ALL.xlsm").Close SaveChanges:=True
End If

'   Check if WKB is open if open (close). if not continue
If WorkbookIsOpen(fromPath & "POST_ALL.xlsm") Then
Workbooks(fromPath & "POST_ALL.xlsm").Close SaveChanges:=True
End If

'   Get path from cell B1 on Main tab
fromPath = Sheets("MAIN").Range("B1")

'   Make sure there is a backslash at the end of the from path
If Right(fromPath, 1) <> "\" Then fromPath = fromPath & "\"

Set wkb = ThisWorkbook
Set wkbFrom = Workbooks.Open(fromPath & "_ALL.xlsm")

'  Clearing all data compare & Main tab
Workbooks("COMPARSION").Sheets("MAIN").Range("J6:K8").ClearContents
Workbooks("COMPARSION").Sheets("COMPARE").Range("B2:BE43").ClearContents

'  Deleting all data, prep/post /files data and saving
With Sheets("Post Rel")
   .Range("A2:AP" & .Range("A" & rows.count).End(xlDown).Row).Delete
End With

With Sheets("T PRE")
   .Range("A2:X" & .Range("A" & rows.count).End(xlDown).Row).Delete
End With

With Sheets("T Post")
   .Range("A2:X" & .Range("A" & rows.count).End(xlDown).Row).Delete
End With
 
Upvote 0
let me simplify the ask
VBA Code:
Sub Pre()
Dim fromPath As String
Dim Cus$
Dim MyDate$
Dim Fd$
Dim For$
Dim wkb As Workbook, wkbFrom As Workbook
Dim rws As Long
Dim i As Long
Dim lr As Long
Dim StartTime As Double
Dim TimeTaken As String
  
StartTime = Timer

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False

'   Check if WKB is open if open (close). if not continue
If WorkbookIsOpen(fromPath & "_ALL.xlsm") Then
Workbooks(fromPath & "_ALL.xlsm").Close SaveChanges:=True
End If

'   Check if WKB is open if open (close). if not continue
If WorkbookIsOpen(fromPath & "POST_ALL.xlsm") Then
Workbooks(fromPath & "POST_ALL.xlsm").Close SaveChanges:=True
End If

'   Get path from cell B1 on Main tab
fromPath = Sheets("MAIN").Range("B1")

'   Make sure there is a backslash at the end of the from path
If Right(fromPath, 1) <> "\" Then fromPath = fromPath & "\"

Set wkb = ThisWorkbook
Set wkbFrom = Workbooks.Open(fromPath & "_ALL.xlsm")

'  Clearing all data compare & Main tab
Workbooks("COMPARSION").Sheets("MAIN").Range("J6:K8").ClearContents
Workbooks("COMPARSION").Sheets("COMPARE").Range("B2:BE43").ClearContents

'  Deleting all data, prep/post /files data and saving
With Sheets("Post Rel")
   .Range("A2:AP" & .Range("A" & rows.count).End(xlDown).Row).Delete
End With

With Sheets("T PRE")
   .Range("A2:X" & .Range("A" & rows.count).End(xlDown).Row).Delete
End With

With Sheets("T Post")
   .Range("A2:X" & .Range("A" & rows.count).End(xlDown).Row).Delete
End With
Let me simplify the ask. the issue i have seen is at times someone may forget to modify B1 and that overrides prior data (prior folders). So if there is a way for the files to run in the folder where the VBA is. The folder will have 3 integral files

  1. VBA code worksheet
  2. Worksheet that runs data and copies to a new sheet
  3. Worksheet that runs data
 
Upvote 0
You have the complete workbook name in B1, then your code is determining if B1 has a "\" at the end. Then you are adding another workbook name at the end of that to open.
 
Upvote 0
You have the complete workbook name in B1, then your code is determining if B1 has a "\" at the end. Then you are adding another workbook name at the end of that to open.
That is a typo when mocking up a draft to remove confidential data. it is just the folder path you are correct
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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