change worksheet name, a difficult one - vba

When I use excel to open a .txt file (a notepad file), the worksheet name is the file name of the notepad file that was opened by default. Therefore, the sheet name will be different when open a different notepad file. Downstream code need this worksheet name be a fixed one. Is there anyway to make change the sheet name to a fixed name such as "sheet1". By the way, codename can not be used, since the macro to use the data in the open file is not another workbook.
Thanks!

You don't need the codename not the worksheet name when you are opening .txt files from Excel. There will always be 1 sheet. So in your code you can always address that sheet as
wb.Sheets(1)
Where wb is the workbook object.
For your reference every .txt file that you open with VBA cannot have a common name unless you set it via code. And if you do that, you will have to still use wb.Sheets(1)
For example
wb.Sheets(1).Name = "Blah Blah"

Could you call your text file sheet1.txt? Would that solve your problem?

I am picturing that your macro opens the text file dynamically because you want to use excel's built in csv parsing. Perhaps sorting and filtering the data afterwards.
Siddarth gave you a good lead, but you shouldn't worry about the name or the sheet because as he said you have the worksheet object to use for your downstream code.
wb.Sheets(1)
Now, if you want to reference this sheet outside of the subroutine that you opened the file. Use a global variable for your
wb

Related

VBA - Copying cells across Workbooks

I'm writing a VBA program that changes the visuals of an excel database. At the end, I need to add a "header" (5 rows of text) at the top. The problem is, I cannot insert the text with VBA, since it contains letters (for ex. á, é...) that aren't compatible with VBA, I need to insert that text from another excel file.
The macro I have is stored in a standalone excel workbook, that also contains the cells of the header I need to copy into my database. The problem is, the name of the excel files I am working with varies. Is there a way I could switch between those 2 files and copy cells across them. Can I store the name of the excel file I am working with and later use it in the VBA code to switch between the workbooks?
Not sure if this 100% answers your question but hope it helps, you can open and store both workbooks as objects using:
Dim wb as Workbook, wb2 as Workbook
Set wb = Workbooks.Open("C:\User\Sample_Workbook_File_Path_1.xlsx")
Set wb2 = Workbooks.Open("C:\User\Sample_Workbook_File_Path_2.xlsx")
From there you can call values from either workbook using things like:
'to get the second workbooks excel file name into a worksheet: "Sample_Workbook_2"
wb.Worksheets("Sample_Worksheet").Range("A1").Value = wb2.Name
'to copy files
wb2.Worksheets("Second_Workbooks_Worksheet").Range("A2:A100").Copy _
wb.Worksheets("Sample_Worksheet").Range("A2")
'Alternatively you can store the entire workbooks path name instead of the file name using:
wb.Worksheets("Sample_Worksheet").Range("A1").Value = wb2.Path

Automatically Saving Excel Workbook into another workbook

I am new to macros and I was wondering if there was a way to auto save an excel workbook into another workbook, overwriting the data. Like if the file is located in different folder. It would automatically save that workbook to a new location every time data was changed. Also, how to avoid seeing message Are you sure you want to overwrite this file?
The Workbook SaveAs command will help you a lot. Combine that with Application.DisplayAlerts = False
Make sure you put this in either a function or an Excel button.

Open new workbook, and run macro, using button

I have a workbook that runs a large multitude of macros, all based on which button is clicked.
I need one of the macro to open a new workbook, and import two files into the workbook (that part I can do).
The second part is that I need it to run a particular macro, in the new workbook. The macro would be stored in the original workbook.
I've seen some suggestions that I need to use APPLICATION.RUN, which has the parameters of having to choose a workbook and name of the routine.
Does this method work?
Assuming it does:
For the workbook, do I need to specify the file path, or just the workbook name?
For the macro, do I need to make the macro public?
Do I need to specify the module it is in?
Are there any other parameters I need to specify to get this method to work?
Is there another method that might work, if APPLICATION.RUN does not work?
What you need to use is :
Application.Run "'FileName.xlsm'!MacroName", "Parameters"
You don't need to specify the path if your workbook is already open, you don't need to specify the module as you can't have doubloons names for different procedures, and there are no other parameters needed.
The procedure doesn't need to be public (let me know if it does), and for alternative methods in VBA, there is none (as the Call method only work in the same workbook), but there are some in VB if this doesn't work out.
Here is a very short example that you can adapt:
Sub demo()
Dim Original As Workbook
Dim Created As Workbook
Set Original = ThisWorkbook
' create a new workbook
Workbooks.Add
Set Created = ActiveWorkbook
' go back
Original.Activate
'make a change in the newly created workbook
Created.Sheets("Sheet1").Range("A1").Value = "whatever"
' save the newly created workbook
Created.Save
Created.Close
End Sub

Vb.net Updating excel formula with references to other workbooks

I am trying to update some formulas from one workbook, to another workbook. Everything is working great until I run into a formula that has a reference to another workbook. For example a formula like this =IF(ISERROR(W!Var1),0,W!Var2) It will prompt me to open this workbook, I am assuming so that it can evaluate the formula. So my question is this. Is there a way for me to handle these situations on the fly, so if there is a workbook reference needed it will prompt me and then save it to memory? Because if I have more than one cell that contains these formulas it will prompt me to open the referenced workbook for every cell that contains the link. Alternatively, is there a way that I can just push my formula into the cell without having excel evaluate it?
So in my code I have this line which works for any value that doesn't contain a workbook reference. TheRange.RefersToRange.FormulaR1C1 = RangeFormula
Any help is greatly appreciated.
I understand that you refer to Worksheets (each of the "tabs" in a given Excel file), the Workbook is the whole file. The popping-up message appears when the referred Worksheet cannot be found. Example: range.Value = "=sheet5!A3" (in a Workbook having just "sheet1", "sheet2" and "sheet3"). If you want to avoid this message (although bear in mind that the Worksheet is not there and thus the calculations will be wrong anyway), you can write:
excelApp.DisplayAlerts = False
Where excelApp is the Excel.Application you are currently using.

How do I reference to another (open or closed) workbook, and pull values back, in VBA? - Excel 2007

Basically I need to gather a fair few figures from another workbook (Which is found and can be opened by a UserForm, therefore the location and names are variable). I need to use VBA for this as I also need to populate a chart with this data. I would prefer to not have to open the other workbook in order to do it, but if it is far easier then its OK.
The UserForm is done and works fine, and I have the code I need to populate the chart, however I can't get VBA to pull back the data from the other workbook and assign it to the variables that I need it to.
Any ideas on how I can get VBA to do this? It would be greatly appreciated.
You will have to open the file in one way or another if you want to access the data within it. Obviously, one way is to open it in your Excel application instance, e.g.:-
(untested code)
Dim wbk As Workbook
Set wbk = Workbooks.Open("C:\myworkbook.xls")
' now you can manipulate the data in the workbook anyway you want, e.g. '
Dim x As Variant
x = wbk.Worksheets("Sheet1").Range("A6").Value
Call wbk.Worksheets("Sheet2").Range("A1:G100").Copy
Call ThisWorbook.Worksheets("Target").Range("A1").PasteSpecial(xlPasteValues)
Application.CutCopyMode = False
' etc '
Call wbk.Close(False)
Another way to do it would be to use the Excel ADODB provider to open a connection to the file and then use SQL to select data from the sheet you want, but since you are anyway working from within Excel I don't believe there is any reason to do this rather than just open the workbook. Note that there are optional parameters for the Workbooks.Open() method to open the workbook as read-only, etc.