I am trying to execute the conditional hyper link command. Command is as follows:
=IF(B4="ADD NEW CATTLE",HYPERLINK("Format-1","CLICK HERE"),"")
When B4 cell contains the string "ADD NEW CATTLE" and the user clicks on the cell B5 another worksheet file named as "Format-1" of the same excel file should open but whenever I click on B5 cell I get an error message saying: "Cannot open the specified file". Please help.
If your workbook name is testbook, please try:
=IF(B4="ADD NEW CATTLE",HYPERLINK("[testbook.xlsx]'Format-1'!A1","CLICK HERE"),"")
or adjust the A1 reference to suit.
Related
I have cell A1 on a sheet named "Data" that looks for input which when received, looks for a sheet name that matches it. If it finds a match, it opens that sheet. I'm trying to clear the value entered in A1 of "Data" after that second sheet has been opened however I'm getting a runtime error that appears to still be looking for the data used to open the second sheet. Here is the code for the "Data" sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
Sheets(Target.Value).Activate
End Sub
I tried using a variable in place of Target.Value hoping that after deleting the actual data in A1, the variable would satisfy the code.
I've ran the following line on at the end of the block above as well as on the sheet which gets activated but either way I get an error.
Sheets("Data").Range("A1:A1").ClearContents
The error is "Run-time error 9 Subscript out of range.
How do I clear the contents of A1 on "Data" so it's ready to receive another request to open another sheet when I'm done with the last one?
This is the correct syntax for a single cell:
Sheets("Data").Range("A1").ClearContents
...however using a range of A1:A1 should also work, so if you're getting Subscript Out Of Range then there is likely no worksheet named Data.
If you have multiple workbooks open then by default, it's looking for the Active Workbook (whose name in contained in ActiveWorkbook.Name.) So if you're going to be working with multiple workbooks, you should explicitly state the workbook name as well:
Workbooks("Book1").Sheets("Sheet1").Range("A1").ClearContents
I didn't need to clear the value after all. I added a couple lines to ensure when returning to the "Data" sheet, cell A1 was selected. This allows for a new value to be entered replacing the previous value and serves the purpose.
Just a slight change to your code. "Is Nothing" does not work. The below code works fine for me.
If Intersect(Target, Range("A1")) = "" Then Exit Sub
Sheets(Target.Value).Activate
Target.ClearContents
In a Excel workbook I'm using a cells value as a parameter when getting data from an external data sorurce through "WHERE somedata=?". I then assign "Parameter 1" to a cell reference.
This works great but when I save the workbook and then open it up again the reference to the cell is deleted. This makes Excel top crash when I try to open the workbook again.
Is this a BUG?
I'm currently working on a script in WorkbookA that notifies of a change in another Workbook (WorkbookB). I would like to add the functionality of being taken to that Workbook if the user would like to see the change. Currently, I'm running the code:
SelRangeA(iRow, 2) = "=HYPERLINK(""[C:\..\WorkbookB.csv]Sheet1!B4"",""CLICK HERE"")"
Which displays the proper Hyperlink in the spreadsheet : Click Here with contents:
=HYPERLINK("[C:\..\WorkbookB.csv]Sheet1!B4","CLICK HERE")
However when I follow the link, it opens the requested Workbook with an error:
Reference is not valid.
Any insight on how to properly reference the required cell? Thanks!
A CSV file won't contain a "Sheet1". The "sheet" name is derived from the name of the file.
So your code needs to be:
=HYPERLINK("[C:\..\WorkbookB.csv]'" & filename & "'!B4","CLICK HERE")
where filename has been set to the base part of the CSV file's filename (i.e. it needs to equate to "WorkbookB" in your example).
One way to do this is to define a name (Formulas->Name Manager) whenever certain cells get changed. For example if the name was defined as "changedcell" you would set the hyperlink as follows:
=HYPERLINK("[C:..\WorkbookB.csv#changedcell]","CLICK HERE")
Just make sure the scope of the name is set to full workbook if there are multiple worksheets.
If the link is in another workbook and you want Excel to open it if it isn't already open, you need to specify both the address and the sub-address (kind of like an anchor in an HTML link):
SelRangeA(iRow, 2) = "=HYPERLINK(""C:\Foo.xlsx#[C:\Foo.xlsx]Sheet1!A1"")"
EDIT: Note that for a .csv file, the worksheet name will default to the file name on open:
SelRangeA(iRow, 2) = "=HYPERLINK(""C:\Foo.csv#[C:\Foo.csv]Foo!A1"")"
'^^^
So I'm trying to create a tracking file in Excel and in cell M5, I have a file location which links to a supporting document and as such, contains some duplicated information so I want to try and pull certain fields (B8 in this example) from the external files referenced in M5.
I have tried =([M5]Sheet1!B8) which works to a degree but this brings up a dialogue box and I have to select the file location manually which is too manual for the purpose.
I have also looked into using the INDIRECT function but can't guarantee that both files will be open at the same time, so would prefer another option if possible.
Any suggestions would be hugely appreciated!!
Here is formula you can use to get individual cell values from a closed workbook.
It is a User Defined Function (UDF) and you call it like this:
=GetClosedCell(A3,B3,C3)
The 1st parameter is the workbook path and name.
The 2nd parameter is the worksheet name.
The 3rd parameter is the address of the cell.
Place this function in a standard code module:
Public Function GetClosedCell(ByVal FileSpec$, ByVal SheetName$, ByVal RangeAddress$)
Const CNX = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[];Extended Properties=""Excel 12.0;imex=1;hdr=no;"""
Const QRY = "SELECT * FROM [.$|:|]"
On Error GoTo errorh
RangeAddress = Range(RangeAddress)(1, 1).Address(0, 0)
With CreateObject("adodb.recordset")
.Open Replace(Replace(QRY, "|", RangeAddress), ".", SheetName), Replace(CNX, "[]", FileSpec)
GetClosedCell = .Fields(0)
End With
Exit Function
errorh:
GetClosedCell = "ERROR: " & Err & " " & Err.Description
End Function
Here is how it would look on a worksheet:
It is possible to grab data from an external file without opening it. For Excel to get a data item, it needs to know:
the file location
the name of the file within that location
the name of the worksheet within that file
the address of the cell within that worksheet
We enter the required info into some cell, say cell C3 in a very specific format:
'C:\TestFolder\[ABC.xls]xxx'!R9C2
note the single quotes!
then running this short macro:
Public Sub GrabData()
Dim r1 As Range, r2 As Range
Set r1 = Range("C3")
Set r2 = Range("C4")
r2.Value = ExecuteExcel4Macro(r1.Value)
End Sub
will retrieve the data and place it in cell C4
Macros are very easy to install and use:
ALT-F11 brings up the VBE window
ALT-I
ALT-M opens a fresh module
paste the stuff in and close the VBE window
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE window as above
clear the code out
close the VBE window
To use the macro from Excel:
ALT-F8
Select the macro
Touch RUN
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
Macros must be enabled for this to work!
I import an External text file (.csv) to my excel worksheet named "SourceData".
The connection is called "Data". Every time I refresh my workbook, excel will ask me to choose my source file. My source files same data structure, and they are automatically generated by other database:
REPL_STATS_010314130000.CSV
REPL_STATS_030314060001.CSV
....
My question is:
How can I get the file name of my external csv file that currently imported to my worksheet "SourceData" so when I refresh the connection, This name is displayed in a cell (for example A1) in another sheet named "Summary"
For example: after I click refresh all, choosing file "REPL_STATS_010314130000.CSV" to update my data source, then cell A1 will display "REPL_STATS_010314130000.CSV"
I try to search for a solution a few days already, but I can't get it work.
Can you suggest form VBA code that can get this information?
Thank you in advance!
Thanks to Tim Williams suggestion I write the following code, and put this code in Sheet1(SourceData):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ConSource As String
ConSource = Worksheets("SourceData").QueryTables("Data").Connection
Worksheets("Summary").Range("A1").Value = "Source: " & Right(ConSource, Len(ConSource) - InStrRev(ConSource, "\"))
ConSource = ""
End Sub