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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Fazza

MrExcel MVP
Joined
May 17, 2006
Messages
9,368
Hi,

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

Regards, Fazza
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
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

Yokiboha

Board Regular
Joined
Feb 21, 2007
Messages
207
Hi guys, thanks for the replies, I have tried that but can't get it to work, comes up with type mismatch
 
Upvote 0

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,181
Office Version
  1. 365
Platform
  1. Windows
Without seeing your code, we can't troubleshoot.
Code:
Sub ShowWBName()
  MsgBox ThisWorkbook.FullName, , ThisWorkbook.Name
End Sub
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Can you post the code where you get the error?
 
Upvote 0

Yokiboha

Board Regular
Joined
Feb 21, 2007
Messages
207
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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
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,190,852
Messages
5,983,234
Members
439,832
Latest member
Kephart87

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
Top