I want to make a link from active cell in workbook 1 which can I use it in workbook 2. I use the following code which assigned to a button:
With ActiveSheet
.Hyperlinks.Add Range("F6"), _
.Parent.FullName & "#'" & .Name & "'!" & "$A$1", TextToDisplay:="link"
End With
This code made a link with full path and I can use it in any workbook but I need some changes which I could to:
Make the active cell hyperlink not cell A1 which specified in code.
The value in the active cell become text to display arg of hyperlink function.
Thanks
PS after Vityata answere: how can i change Range("F6") to activecell adress?
In order to obtain the active cell value and address, change your code the corresponding places with the following:
ActiveCell.Address
ActiveCell.Value
I find it just to close this topic.
Sub Button36_Click()
Dim newRange As Range
Set newRange = Range(ActiveCell, ActiveCell.Offset(numRows, numCols))
With ActiveSheet
.Hyperlinks.Add Anchor:=newRange, _
Address:=.Parent.FullName & "#'" & .Name & "'!" & ActiveCell.Address, TextToDisplay:=ActiveCell.Text
End With
End Sub
try this
Sub add_links_Input_Column()
Dim lRow As Long
Dim ColHead As String
ColHead = InputBox("Enter Column Letter", "Identify Column", [c1].Value)
If ColHead = "" Then Exit Sub
With ActiveSheet
lRow = .Range(ColHead & .Rows.Count).End(xlUp).Row
For Each c In .Range(ColHead & "2:" & ColHead & lRow)
ActiveSheet.Hyperlinks.Add anchor:=c, Address:=c.Value
Next
End With
End Sub
Related
At some point I need to consolidate data (simply sum duplicated items). I would like to parameterize Sources:= argument - actually everything works the way I amused when using option after single quote mark. When using src variable I'm getting: Cannot open consolidation source file (picture attached). Looking forward for some tips.
Here is the code:
Sub Format()
' Keyboard Shortcut: Ctrl+Shift+C
'Definition
Dim wb As Workbook
Dim ws As Worksheet
Dim rg As Range
Dim lRow As Integer
Dim src As String
Set wb = ActiveWorkbook
Set ws = ThisWorkbook.ActiveSheet
'Format data
Range("H2").Select
ActiveCell.FormulaR1C1 = "=RC[-5]&""*""&RC[-4]&""*""&RC[-3]&""*""&RC[-2]"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=RC[-7]"
' Find the last non-blank cell in column B(2)
lRow = Cells(Rows.Count, 2).End(xlUp).Row
Set rg = Range("H2", Cells(lRow, 9))
' AutoFill of formated data
Range("H2:I2").Select
Selection.AutoFill Destination:=rg, Type:=xlFillDefault
'Consolidate
src = """" & "'" & wb.Path & "\[" & wb.Name & "]" & ws.Name & "'!R2C8:R" & lRow & "C9" & """"
Debug.Print src
'Range("K2").Consolidate Sources:= _
"'C:\Users\pl1aji0\Documents\Projekty\USA\2020\L034.00080 Innovation\700\BPM\[test.xlsx]Sheet1'!R2C8:R11C9" _
, Function:=xlSum, TopRow:=False, LeftColumn:=True, CreateLinks:=False
Range("K2").Consolidate Sources:= _
src _
, Function:=xlSum, TopRow:=False, LeftColumn:=True, CreateLinks:=False
End Sub
I have two files, one the file in which I want to run the macro in and another external file.
Within the file that the macro is running in (henceforth the "master" file), there is something that looks like this:
The code that I have so far is this:
Sub test()
For i = 1 To 3
If Not Range("B" & i).Value = "X" Then
Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
Range("B" & i).Value = "X" 'update the check
Range("D2").Copy 'this is the tricky part - this is what is needed. The formula links needs to be broken so that only the values remain
Range("D2").PasteSpecial xlPasteValues
ActiveWorkbook.SaveCopyAs "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm" 'the problem with SaveCopyAs is that the formula originally is now overwritten.
'thus I need some way to refer back to the 'master' workbook, the one where the formula has not yet been overwritten
End If
Next i
End Sub
What I want to achieve is that the macro will loop through and check to see if a workbook has been created with the names in column A. Then, it will update the value in "C2". Finally, a copy is saved -- and the formula is overwriten to its value, rather than remain a formula. This is the difficulty in that I cannot simply save a copy of the workbook -- the formula would have been overwritten after the run of the macro.
This is what happens in Type3.xlsm after running the macro. As you can see, the value in "D2" is 1, whereas it should be 3.
I have also considered this method:
Sub test2()
For i = 1 To 3
If Not Range("B" & i).Value = "X" Then
Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
Range("B" & i).Value = "X" 'update the check
Set wboor = ActiveWorkbook
fileaddress = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
Range("D2").Copy 'this is the tricky part - this is what is needed. The formula links needs to be broken so that only the values remain
Range("D2").PasteSpecial xlPasteValues
wboor.SaveCopyAs "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm" 'Perhaps I can save a copy first? Then close the workbook, so the formula is preserved
wboor.Close
Workbooks.Open Filename:=fileaddress 'but then, how do I call the original file, and then loop the macro to run again?
End If
Next i
End Sub
Any suggestions/help would be appreciated!
Not sure it was necessary to start a new question, but anyway try this.
Sub test()
Dim wb As Workbook, s As String, i As Long
For i = 1 To 3
If Not Range("B" & i).Value = "X" Then
Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
Range("B" & i).Value = "X" 'update the check
s = "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm"
ActiveWorkbook.SaveCopyAs s
Set wb = Workbooks.Open(s)
wb.Sheets(1).UsedRange.Value = wb.Sheets(1).UsedRange.Value
wb.Close True
End If
Next i
End Sub
You could save a copy of the formula, and put it back into the workbook each time. ie:
Option Explicit
Sub test()
With ThisWorkbook.ActiveSheet
Dim formulaText As String
formulaText = .Range("D2").Formula
Dim i As Long
For i = 1 To 3
If Not .Range("B" & i).Value = "X" Then
.Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
.Range("B" & i).Value = "X" 'update the check
.Range("D2").Copy
.Range("D2").PasteSpecial xlPasteValues
ActiveWorkbook.SaveCopyAs "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm"
.Range("D2").Formula = formulaText
End If
Next i
End With
End Sub
I have looked at many posts and cannot seem to get this right. I have a userform that take a codename and then creates a sheet with that name, then adds a link to the new sheet as the last cell in Column B on another sheet. I have used 3 different methods to insert the hyperlink, but all of them just return a blank cell, while if I change the value to any string, it works.
Dim sh As Worksheet
Dim codename As String
Dim lastrow As Long
Dim cont As Worksheet
On Error Resume Next
Application.ScreenUpdating = False
codename = InputBox("What is the codename?")
Sheets("XXX").Visible = True
Sheets("XXX").Copy After:=Worksheets("YYY")
ActiveWindow.ActiveSheet.name = codename
Sheets("XXX").Visible = False
Worksheets(YYY).Activate
lastrow = Sheets("YYY).Range("B" & Rows.Count).End(xlUp).Row + 1
ActiveSheet.Range("B" & lastrow).End(xlUp).Offset(1).Hyperlinks.Add Anchor:=ActiveCell, Address:="", SubAddress:=sh & "!A1", TextToDisplay:=codename
ActiveSheet.Range("B" & lastrow).End(xlUp).Offset(2).Activate
ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:="", SubAddress:=sh.name & "!A1", TextToDisplay:=codename
ActiveSheet.Range("B" & lastrow).End(xlUp).Offset(3) = codename
ActiveSheet.Range("B" & lastrow).End(xlUp).Offset(4).Hyperlinks.Add Anchor:=Sheets(codename).Cells(1, 1), _
Address:="", SubAddress:=sh, TextToDisplay:=codename
Application.ScreenUpdating = True
I know I have 4 iterations of essentially the same thing. The point is is that no matter if I use 1 of them, or all 4, I get 3 blank cells and (codename) as plaintext, showing that I am obviously missing something easy that I for the life of me cannot figure out. Thanks to all responses.
The reason the links aren't working might be because you use the worksheet object sh without declaring it, and specifically, without declaring it as the new sheet.
In my solution I've only tested it using the .Add method that I included out-commented.
Sub test()
Dim sh As Worksheet, nsh As Worksheet ' sh = YYY, nsh = codename
Dim nrng As Range
Dim codename As String
Dim lastrow As Long
Dim cont As Worksheet
codename = InputBox("What is the codename?")
Set sh = Sheets("YYY")
Sheets("XXX").Visible = True
Sheets("XXX").Copy After:=Worksheets("YYY")
ActiveWindow.ActiveSheet.Name = codename
Sheets("XXX").Visible = False
'Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = codename ' if needed
sh.Activate
lastrow = sh.Range("B" & Rows.Count).End(xlUp).Row + 1
sh.Hyperlinks.Add _
Anchor:=sh.Range("B" & lastrow), _
Address:="", _
SubAddress:="'" & codename & "'!A1", _
TextToDisplay:=codename
End Sub
Shamelessly stolen from myself.
Sub Tester()
DoHyperlink Sheets("Sheet1").Range("F10"), _
Sheets("Sheet 2").Range("E12"), _
"Click Me"
End Sub
'assumes rngFrom and rngTo are in the same workbook...
Sub DoHyperlink(rngFrom As Range, rngTo As Range, LinkText As String)
rngFrom.Parent.Hyperlinks.Add Anchor:=rngFrom, Address:="", _
SubAddress:="'" & rngTo.Parent.Name & "'!" & rngTo.Address(), _
TextToDisplay:=LinkText
End Sub
If I get you right, you are trying to do this... But don't understand why it's "YYY" (not dynamic).
Option Explicit
Sub AddSheetAndLinkIt()
Dim codename As String
Dim oWS As Worksheet, oRng As Range
codename = InputBox("What is the codename?")
' Check if codename already exists
On Error Resume Next
Set oWS = ThisWorkbook.Worksheets(codename)
If Not oWS Is Nothing Then
MsgBox "The worksheet for """ & codename & """ already exists! You cannot create it again.", vbExclamation + vbOKOnly
Exit Sub
End If
' Copy worksheet "XXX" and add hyperlink to "YYY"
Set oWS = ThisWorkbook.Worksheets("YYY")
Set oRng = oWS.Range("B" & Rows.Count).End(xlUp)
ThisWorkbook.Worksheets("XXX").Copy After:=oWS
With ThisWorkbook.Worksheets("XXX (2)")
.Name = codename
.Visible = True
.Activate
End With
oWS.Hyperlinks.Add oRng, "", "'" & codename & "'!A1", "Go to " & codename, codename
Set oRng = Nothing
Set oWS = Nothing
End Sub
I have an Excel macro that copies and pastes line items from a source file to an Excel-based form. It opens a form template and saves each line item as it's own file then loops through the remaining rows. Right now I have a file path built into the code that refers to the form template needed, but I need the user to be able to choose which file they want to use as their template. I have code for both of these processes but I haven't been able to combine them. My example below results in a Compile Error: Variable not defined.
Here's what I have so far:
Option Explicit
Sub CopyToForm()
Dim wbSource As Workbook, wbForm As Workbook
Dim wsSource As Worksheet, wsForm As Worksheet
Dim formpath As String, foldertosavepath As String
Dim lrow As Long, i As Integer
Set wbSource = ThisWorkbook '~~> Write your code in Indication Tool.xls
Set wsSource = wbSource.Sheets("Indication Tool") '~~> Put the source sheet name
'~~> This opens the Processing Form template.
formpath = "C:\File path.xls"
'~~> Prompts user with Open File Dialog Box
strCancel = "N"
strWorkbookNameAndPath = Application.GetOpenFilename _
(FileFilter:=strFilt, _
FilterIndex:=intFilterIndex, _
Title:=strDialogueFileTitle)
'~~> Exits If No File Selected
If strWorkbookNameAndPath = "" Then
MsgBox ("No Filename Selected")
strCancel = "Y"
Exit Sub
ElseIf strWorkbookNameAndPath = "False" Then
MsgBox ("You Clicked The Cancel Button")
strCancel = "Y"
Exit Sub
End If
Workbooks.Open strWorkbookNameAndPath
'~~> This declares path where the Individual forms will be saved.
foldertosavepath = "C:\File path\Forms\"
With wsSource
'~~> Counts how many rows are in the Indication Tool
lrow = .Range("B" & .Rows.Count).End(xlUp).Row
If lrow < 18 Then MsgBox "No data for transfer": Exit Sub
For i = 18 To lrow
Set wbForm = Workbooks.Open(formpath) '~~> open the form
Set wsForm = wbForm.Sheets("Processing Form") '~~> Declare which worksheet to activate
'~~> Proceed with the copying
.Range("B" & i).Copy wsForm.Range("F7:K7")
.Range("C" & i).Copy wsForm.Range("D8")
.Range("C" & i).Copy wsForm.Range("D30")
.Range("D" & i).Copy wsForm.Range("H29")
.Range("E" & i).Copy wsForm.Range("E29")
.Range("F" & i).Copy wsForm.Range("D33")
.Range("G" & i).Copy wsForm.Range("J30:K30")
.Range("H" & i).Copy wsForm.Range("P33")
.Range("I" & i).Copy wsForm.Range("L33:N33")
.Range("L" & i).Copy wsForm.Range("H32")
.Range("R" & i).Copy wsForm.Range("D87")
.Range("C2:F2").Copy wsForm.Range("J101:M101")
.Range("C3:M3").Copy wsForm.Range("E102:O102")
'~~> Save the form using the client name
wbForm.SaveAs foldertosavepath & .Range("B" & i).Value & ".xls"
'~~> These steps are for formatting, as I haven't figured out how paste values only
Set wbForm = Workbooks.Open(formpath)
Cells.Select
Selection.Copy
wsForm.Activate
'~~> This allows the format to be pasted into the updated Form
wsForm.Unprotect
Cells.Select
Selection.PasteSpecial Paste:=xlPasteFormats
wsForm.Protect
ActiveWorkbook.Save
ActiveWorkbook.Close
wbForm.Close ([SaveChanges:=False])
Set wbForm = Nothing
Set wsForm = Nothing
Next
End With
End Sub
When I debug the error, Sub CopyToForm() is highlighted in yellow and strCancel = is selected. Is there a way to set the user-chosen file as the formpath? Thanks in advance for your help, this has been a thorn in my side for quite some time.
Option Explicit is declared, and strCancel isn't declared as a variable.
Add Dim strCancel As String to your code
I need to add a function in my worksheet using macros. I need to pass a variable to the Excel function since myWorkbook and sheetName, to which it will reference, are variable.
MyRange = Workbooks(myWorkbook).Sheets(sheetName).Range("H11:H32")
theFormula = "=SUM(" & MyRange & ")"
Range("B2").Select
ActiveCell.FormulaR1C1 = theFormula
I get error 13: Type Mismatch.
I am not an avid VBA programmer, and this is giving me a big headache. I tried defining theFormula as String but no hope.
You need to convert the Range object to a string representation of the range, for example to set the formula to =SUM($H$11:$H$32) you would;
Dim MyRange As Range
set MyRange = Workbooks(myWorkbook).Sheets(sheetName).Range("H11:H32")
theFormula = "=SUM(" & MyRange.Address(ReferenceStyle:=xlR1C1) & ")"
Range("B2").Select
ActiveCell.FormulaR1C1 = theFormula
Or if you dont want R1C1 notation you can;
ActiveCell.Formula = "=SUM(H11:H32)"
Sub MakeSum(sBookName As String, sSheetName As String)
Dim rMyRange As Range
Set rMyRange = Workbooks(sBookName).Sheets(sSheetName).Range("H11:H32")
If rMyRange.Parent.Parent.Name = ActiveWorkbook.Name Then
With ActiveSheet
'same sheet, so just use address
If rMyRange.Parent.Name = .Name Then
.Range("B2").Formula = "=SUM(" & rMyRange.Address & ")"
Else
'same workbook, different sheet, so prepend sheet name
'single quotes prevent error when there's a space in the sheet name
.Range("B2").Formula = "=SUM('" & rMyRange.Parent.Name & "'!" & rMyRange.Address & ")"
End If
End With
Else
'not the same workbook, use external address
ActiveSheet.Range("B2").Formula = "=SUM(" & rMyRange.Address(, , , True) & ")"
End If
End Sub
Progragmatically this wont work - you are saying the cell formula should equal "=SUM(" & MyRange & ")" - however when is this actually evaluated ?
does theFormula =SUM(" & MyRange & ")
work?