When I used this formula,
Sub IndexingSheets()
Sheets(1).Range("B3").Formula = _
"=HYPERLINK(""#" & ThisWorkbook.Sheets(2).Name & "!A5"", ""TextToDisplay"")"
Sheets(2).Range("A5").Formula = _
"=HYPERLINK(""#" & ThisWorkbook.Sheets(1).Name & "!B3"", ""TextToDisplay"")"
End Sub
it creates a hyperlink but when I click it, it shows reference is not valid. Then when I changed the formula in the formula bar, I added the filename and it worked so it will now look like this:
=HYPERLINK("[Book1.xls]'Sheet2 123!A5", "gg")
But originally it is just like this:
=HYPERLINK("#Sheet2 123!G20", "gg")
How can I alter my original code which I shown on the topmost so that this:
=HYPERLINK("[Book1.xls]'Sheet2 123!A5", "gg")
will be shown or will be the format in the formula bar? my problem is how can i put the filename now without just copying the filename itself and not just pasting it in the code?
I just want to clarify that there's a messagebox that says you should input the 2nd name of the sheet. so the name of the sheet is not just "Sheet2" anymore.
For instance you put in the msgbox "123", the name of the sheet would now become "Sheet2 123". the messagebox is already present, no need to create a code for it.
Try this. You need to put single-quotes around the sheet names, to account for the possible spaces in sheet names. If you omit single-quotes, then the sheet names must not contain any spaces.
Sub IndexingSheets()
With ThisWorkbook
.Sheets(1).Range("B3").Formula = _
"=HYPERLINK(""#'" & .Sheets(2).Name & "'!A5"", ""Jump to Sheet2!A5"")"
.Sheets(2).Range("A5").Formula = _
"=HYPERLINK(""#'" & .Sheets(1).Name & "'!B3"", ""Jump to Sheet1!B3"")"
End With
End Sub
To include the Workbook name (not necessary, I don't think):
Sub IndexingSheets()
With ThisWorkbook
.Sheets(1).Range("B3").Formula = _
"=HYPERLINK(""#'[" & .Name & "]" & .Sheets(2).Name & "'!A5"", _
""Jump to Sheet2!A5"")"
.Sheets(2).Range("A5").Formula = _
"=HYPERLINK(""#'[" & .Name & "]" & .Sheets(1).Name & "'!B3"", _
""Jump to Sheet1!B3"")"
End With
End Sub
And, an alternate method of achieving the same -- this one includes the Workbook name, too:
Sub IndexingSheetsWithFileName()
With ThisWorkbook
.Sheets(1).Hyperlinks.Add Anchor:=.Sheets(1).Range("B3"), _
Address:="", SubAddress:= _
"'[" & .Name & "]" & .Sheets(2).Name & "'!A5", _
TextToDisplay:="Jump to Sheet2!A5"
.Sheets(2).Hyperlinks.Add Anchor:=.Sheets(2).Range("A5"), _
Address:="", SubAddress:= _
"'[" & .Name & "]" & .Sheets(1).Name & "'!B3", _
TextToDisplay:="Jump to Sheet1!B3"
End With
End Sub
Related
I have a Macro that i have managed to put together (its rough, and im new to VBA but it does what i want - for the most part) It currently prints the active sheet to PDF and names it based on cell values. I want to adapt this to print 2 sheets into a single file (if its separate files, thats more than ok!) The cell Value naming bit can be changed at the top which i can do, but its calling for the export to pdf bit that im having an issue with.
I have tried reading up on the Activeworkbook functions but im not having much luck. I have tried calling for a sheet array, but it doesnt like the exportasfixedformat Type:= and im kind of new to that part too. It likes it in the original code, but not when i try and change the ActiveWorkbook.ActiveSheet, it spits it.
It would finalise my calculator :) Any help would be greatly appreciated.
Code:
Sub GetFilePath_Click()
Dim FileAndLocation As Variant
Dim strFilename As String
strFilename = Sheets("Leave Loading").Range("F13") & ", " & Sheets("Leave Loading").Range("F12") & " - " & Sheets("Leave Loading").Range("F14") & "- " & "Leave Loading" & ".pdf"
FileAndLocation = Application.GetSaveAsFilename _
(InitialFileName:=strPathLocation & strFilename, _
filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select a Location to Save")
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFilename, OpenAfterPublish:=True
End Sub
Thank you in advance!
Option Explicit
Sub GetFilePath_Click()
Dim FileAndLocation As Variant
Dim strFilename As String, strPathLocation As String
strPathLocation = ""
With Sheets("Leave Loading")
strFilename = .Range("F13") & ", " & .Range("F12") & " - " _
& .Range("F14") & "- Leave Loading" & ".pdf"
End With
FileAndLocation = Application.GetSaveAsFilename _
(InitialFileName:=strPathLocation & strFilename, _
filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select a Location to Save")
Sheets(Array("Sheet2", "Sheet4")).Select
Sheets("Sheet2").Activate
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFilename, OpenAfterPublish:=True
End Sub
Could you kindly help me with this code? I am a beginner..
Sub ExportSheetstoPDF()
Dim ws As Worksheet
Dim mywsname As String
For Each ws In Worksheets
ws.Select
mywsname = ws.Name
Activesheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:="Z:\Incent_2022\ORDINARIA\RETAIL-WHS\Schede Obiettivi\Q1\" & "Schede Obiettivi Retail Classico Q1 22_" & mywsname & “.pdf”, _
Next ws
End Sub
Your problem is that the line
Activesheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:="Z:\Incent_2022\ORDINARIA\RETAIL-WHS\Schede Obiettivi\Q1\" & "Schede Obiettivi Retail Classico Q1 22_" & mywsname & “.pdf”, _
has the continuation character _ at the end of it, but nothing continues on the next line.
Remove the ,_ from the end of the line
Also the “.pdf” needs to be ".pdf" i.e. using ordinary double-quotes and not ones copied out of an Office document ("intelligent" double-quotes)
I am using cmd.transferspreadsheet in Excel VBA to import an Excel sheet into an Access Table. Every time I run this code, the data sent to access is 1 iteration out of date. I have zeroed in on two columns one that has a tier assignment (column 4) and one that has a string for the time and person who is submitting (column 13). In stepping through the code, I am printing the values of these 2 columns to the immediate window. I am doing that both by a range reference and an object reference. Both give me the correct answer, but when I go into Access, I see the data that was there before I changed it. What am I doing wrong?!?!?!
Sub SendTiersToDB()
'sends the data from this file to the access database
Dim fPathName As String
Dim dbTblTiers As String
Dim strSubmit As String
Dim tblXLTiers As ListObject
Dim strXLTiers As String
Dim appDB As New Access.Application
Set tblXLTiers = Sheet7.ListObjects(1)
fPathName = "\\MERCH\Assortment Planning\Databases\New_AP_Database.accdb"
strXLTiers = tblXLTiers.DataBodyRange.Address
dbTblTiers = "Tbl_Tiers"
'Fill In Subbmission Data
strSubmit = "Last Submitted: " & Now & " by " & Environ("username")
tblXLTiers.ListColumns(13).DataBodyRange.Value = strSubmit
tblXLTiers.ListColumns(13).DataBodyRange.Calculate
'and insert the new store records
Debug.Print "By Range " & Sheet7.Range("D2").Value
Debug.Print "By Range " & Sheet7.Range("M2").Value
Debug.Print "By Object " & tblXLTiers.DataBodyRange(1, 4)
Debug.Print "By Object " & tblXLTiers.DataBodyRange(1, 13)
appDB.OpenCurrentDatabase fPathName
appDB.DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=dbTblTiers, _
Filename:="https://theexcelfilepath/file.xlsb", _
HasFieldNames:=True, _
Range:=Sheet7.Name & "$" & "A1:O9277"
End Sub
Thank you for the comments! I woke up at 4AM with the same thought.
I added a line to save the file prior to the DoCmd line, and that did the trick!
'Fill In Subbmission Data
strSubmit = "Last Submitted: " & Now & " by " & Environ("username")
tblXLTiers.ListColumns(13).DataBodyRange.Value = strSubmit
tblXLTiers.ListColumns(13).DataBodyRange.Calculate
ThisWorkbook.Save
'and insert the new store records
appDB.OpenCurrentDatabase fPathName
appDB.DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=dbTblTiers, _
Filename:="https://theexcelfilepath/file.xlsb", _
HasFieldNames:=True, _
Range:=Sheet7.Name & "$" & "A1:O9277"
I am trying to save a pdf with the name being info in a cell, but the name involves multiple cells. the cells have formulas inputted because they pull from a seperate sheet in my spreadsheet. how would i could about doing this? this is the formulas in the cells that i want to pull the name for the pdf from:
=VLOOKUP('Work Order'!F26,'Fort McMurray File Services'!A2:AJ1515,6,FALSE)
=VLOOKUP('Work Order'!F26,'Fort McMurray File Services'!A2:AJ1515,6,FALSE)
And this is the code i am using in vba:
Private Sub filename_cellvalue()
'Update 20141112
Dim Path As String
Dim filename As String
Path = "C:\Users\meghan lewis\Desktop\MASS DEMO"
filename = .Range("C7").Value & .Range("D7").Value & _
.Range("E7").Value & .Range("F7").Value & _
.Range("G7").Value & .Range("H7").Value & .Range("I7").Value
ActiveWorkbook.SaveAs filename:=Path & filename & ".PDF", FileFormat:=xlNormal
End Sub
Sub SaveAsPDF()
Dim fName As String
With Worksheets("WORK ORDER")
fName = .Range("C7").Value & .Range("D7").Value & _
.Range("E7").Value & .Range("F7").Value & _
.Range("G7").Value & .Range("H7").Value & _
.Range("I7").Value
End With
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
"C:\Users\meghan lewis\Desktop\MASS DEMO & fName", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
When I try to save it saves the pdf as mass demo rather than pulling from the cells. Please advise.
How does your code even compile? You have unqualified range addresses like .Range("C7").Value, .Range("D7").Value etc in Sub filename_cellvalue
In Sub SaveAsPDF(), change "C:\Users\meghan lewis\Desktop\MASS DEMO & fName" to "C:\Users\meghan lewis\Desktop\MASS DEMO" & fName
fname within quotes will behave as a String and not a variable.
![Data Set][1]I have a macro to GetValues from a Closed Workbook. The macros work and gets me the data, but I am missing how to make it to get only any data with the account P 15178.
The source data contains 5 accounts but I only need to get one account. The Account Number are listed in column "A" of the source data.
This is what I have so far:
Sub test()
GetValuesFromAClosedWorkbook "H:\VBA", "DNAV.xlsx", "DNAV", "A1:F250"
End Sub
Sub GetValuesFromAClosedWorkbook(fPath As String, _
fName As String, sName, cellRange As String)
With ActiveSheet.Range(cellRange)
.FormulaArray = "='" & fPath & "\[" & fName & "]" _
& sName & "'!" & cellRange
.Value = .Value
End With
End Sub
Data Set:
This answer is based solely the image of your dataset. If anything defers from that, you will need to tweak the code.
Sub test()
GetValuesFromAClosedWorkbook "H:\VBA", "DNAV.xlsx", "DNAV", "A1:F7" 'since your product only goes to row 7, this should be good. If your data is not sorted this way all the time, you will need a whole other solution.
End Sub
Sub GetValuesFromAClosedWorkbook(fPath As String, _
fName As String, sName, cellRange As String)
With ActiveSheet.Range(cellRange).Offset(6) 'offsetting by 6 rows should get the formula starting on cell A7
.FormulaArray = "='" & fPath & "\[" & fName & "]" _
& sName & "'!" & cellRange
.Value = .Value
End With
End Sub
Here's what you can try:
Sub GetValuesFromClosedWorkbook(fpath as string, fname as String, _
sname as String, cellRange as String, criteria as String)
Activesheet.AutoFilterMode = False
With Activesheet.Range(cellRange)
.FormulaArray = "='" & fpath & "\[" & fname & "]" & sname & "'!" & cellRange
.Value = .Value
.AutoFilter Field:=1, Criteria1:= "<>" & criteria
.Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
Activesheet.AutoFilterMode = False
End Sub
Then call your sub like this:
GetValuesFromClosedWorkbook "H:\VBA", "DNAV.xlsx", "DNAV", "A1:F250", "P 15178"
Not tested, no way to test in my phone :) so i leave it to you.
Entirelybased on your screen shot.
Edit it to suit your needs.