picking up workbook name in VBA

Yokiboha

Board Regular
Joined
Feb 21, 2007
Messages
207
I have a workbook with vba code that refers in many places to the name of the workbook.

I need to save the workbook with a different name but when I do I have to change the name in the code to the new name.

For exaple a book is named aaa.xls

I save it as bbb.xls

a part of the code reads:

Sheets("StockIn").Select
With Workbooks("aaa.xls").Sheets("StockIn"......

I need to change this so that the "aaa.xls" is the workbook name so that when the workbook is saved as "bbb.xls" I don'y have to change anything and the code will run.

Does that make sense? If so can anybody help?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,

Maybe in VBA do a change all (CTRL-H) of "Workbooks("aaa.xls")" to ThisWorkbook

Regards, Fazza
 
Upvote 0
Why refer to the workbook with a hardcoded name?

Perhaps you could use ThisWorkbook which will refer to the workbook the code is in.
 
Upvote 0
Hi guys, thanks for the replies, I have tried that but can't get it to work, comes up with type mismatch
 
Upvote 0
Without seeing your code, we can't troubleshoot.
Code:
Sub ShowWBName()
  MsgBox ThisWorkbook.FullName, , ThisWorkbook.Name
End Sub
 
Upvote 0
Can you post the code where you get the error?
 
Upvote 0
Code:
Sub Delrows()


With Workbooks(ThisWorkbook).Sheets("Salesdata")
        With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
            .AutoFilter Field:=1, Criteria1:="x"
            .Offset(1).EntireRow.Delete
            .AutoFilter
        End With
    End With


End Sub
 
Upvote 0
I don't think anybody suggested using it like that.

Though I'll admit we didn't give specifics but that's hard to do without more information.:)
Code:
With ThisWorkbook.Sheets("Salesdata")
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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