File name issue with VBA

pure vito

Board Regular
Joined
Oct 7, 2021
Messages
180
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have an issue i'm not sure how to resolve,

firstly i send out this document for others to use and then they send it back to me, the issue arises when the user sends the file back under a different name,

it stops this code from working because of these lines in the code "Windows("New Pick Form.xlsm").Activate" is there a way around this issue, its basically the name change that's causing the problem?

thanks in advance,


VBA Code:
Sub mac1pasteboh()
'
' mac1pasteboh Macro
'

'
    Windows("L461 PP1 BOH.xlsm").Activate
    Sheets("LSA").Select
    
    Windows("New Pick Form.xlsm").Activate
    Sheets("Sheet1").Select
    Range("A4").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("L461 PP1 BOH.xlsm").Activate
    Range("A2").Select
    Selection.End(xlDown).Select
    Range("A:A").Find("").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Windows("New Pick Form.xlsm").Activate
    Range("B4").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("L461 PP1 BOH.xlsm").Activate
    Range("B2").Select
    Selection.End(xlDown).Select
    Range("B:B").Find("").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Windows("New Pick Form.xlsm").Activate
    Range("F4").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("L461 PP1 BOH.xlsm").Activate
    Range("F2").Select
    Selection.End(xlDown).Select
    Range("F:F").Find("").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("O2").Select
    Selection.End(xlDown).Select
    Range("O:O").Find("").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Windows("New Pick Form.xlsm").Activate
    Range("H3").Select
    Application.CutCopyMode = False
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Is "New Pick Form.xlsm" supposed to be the name of the file itself, containing the code?

If so, you can easily capture the name dynamically at the beginning of your code, and easily reference it later in your code.
I would do this through a workbook variable.

So put this at the very beginning of your code:
VBA Code:
Dim wb as Workbook
Set wb = ActiveWorkbook

Then you can replace all instances of:
VBA Code:
Windows("New Pick Form.xlsm").Activate
in your code with:
VBA Code:
wb.Activate
 
Upvote 0
Solution
That's absolutely fantastic thank you so very much for taking the time, this may have seemed an easy task for you but i'm very new to this so thanks again.
 
Upvote 0
That's absolutely fantastic thank you so very much for taking the time, this may have seemed an easy task for you but i'm very new to this so thanks again.
No worries! Glad I was able to help!

Using object variables is not something I would expect noob VBA code writers to know (I know it took me a few years myself before I started using them).
As the saying goes "its easy when you know how".
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,685
Members
449,117
Latest member
Aaagu

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