Best Practice for VBA

vikas_newports

Board Regular
Joined
May 26, 2016
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hello
I am new to VBA and I am learning myself at my pace, after many days of working I created a macro/code to automate my daily task but unfortunately, it was rejected by my Boss
He Said you used filenames in your code if someone changes the file names will you code work?
Moreover, he said, you are using the main folder for all types of data (raw/source data and Helping data)
So I am going to rewrite the code. Is it fruitful to re-write or should I convince him for my Actual work?
How can I handle the errors
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It is difficult to give you advice without seeing your code. Can you post a copy?
 
Upvote 0
I'm willing to have a look at it although I can't promise a solution.
 
Upvote 0
Hello
I am new to VBA and I am learning myself at my pace, after many days of working I created a macro/code to automate my daily task but unfortunately, it was rejected by my Boss
He Said you used filenames in your code if someone changes the file names will you code work?
Moreover, he said, you are using the main folder for all types of data (raw/source data and Helping data)
So I am going to rewrite the code. Is it fruitful to re-write or should I convince him for my Actual work?
How can I handle the errors

No one but you can answer "is it fruitful to rewrite?". And "how can I handle errors?" is much too general.

The following is how I allow users to specify input and output file names. Caveat: It will overwrite an existing output file.

VBA Code:
Dim fdIn As Long, fdOut As Long, path As String

On Error Resume Next
path = Application.GetOpenFilename(Title:="Open existing INPUT file")
If path = "" Or path = "False" Then Exit Sub
On Error GoTo 0
fdIn = FreeFile
Open path For Input Access Read As #fdIn

On Error Resume Next
path = Application.GetSaveAsFilename("", Title:="Open new OUTPUT file")
If path = "" Or path = "False" Then Exit Sub
On Error GoTo 0
fdOut = FreeFile
Open path For Output Access Write As #fdOut
 
Upvote 0
I used too much File names for almost every task like copy paste Filter ,formulas etc .
In my opinion its very hard to avoid filenames for working as we need to copy or filter data from different workbooks and extract some filterered data to save a new workbooks .. I think source data files names are issue and the Macro file name is too .
 
Upvote 0
You might be able to convince your boss if you structure your data and if you change your code from using absolute pathnames to relative ones.

Create subfolders for raw data, temp data, and reports. Use ThisWorkbook.Path to make pathnames relative to the program's location. Use filename suffixes like 20201115 to easily deal with input data and with output data over time.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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