破坏了新工作簿的超链接

时间:2017-03-09 21:43:50

标签: excel vba excel-vba hyperlink excel-2010

我是Excel的新手,所以我希望这是有道理的。下面的代码显示了在单击用户表单上的按钮时在特定工作簿(与当前工作簿分开)上创建的新工作表。虽然,我在单独的工作簿上创建的工作表的超链接似乎被打破了。我究竟做错了什么?什么都有帮助,谢谢!

Dim LastRow As Long, ws As Worksheet
Set ws = Sheets("Employee Information")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1

If Me.cbStores.Value = "Northern" Then
Dim newWB As Workbook
Dim thisWB As Workbook
Set thisWB = ThisWorkbook
Set newWB = GetOrCreateWB("EmployeeTemplates", "C:\Users\...\Folder") '<--| Opening EmployeeTemplates wb
thisWB.Sheets("Template").Copy after:=newWB.Sheets(1)
With ActiveSheet '<--| the just pasted worksheet becomes the active one
    .Name = AddEmployeeUF.txtFirstname.Text + AddEmployeeUF.txtMiddleinitial.Text + AddEmployeeUF.txtLastname.Text + "Template" '<--| Name it
    ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), Address:="", SubAddress:=.Name & "!A1", TextToDisplay:="View" '<--| hyperlink to new sheet
End With
End If

2 个答案:

答案 0 :(得分:1)

答案01

此答案使用文件路径,例如&#34; C:\ Users \ Me \ Desktop \ newfile.xlsx&#34;

With ActiveSheet '<--| the just pasted worksheet becomes the active one
    .Name = AddEmployeeUF.txtFirstname.Text + _
            AddEmployeeUF.txtMiddleinitial.Text + _
            AddEmployeeUF.txtLastname.Text + "Template" '<--| Name it

    ' the hyperlink SubAddress needs a valid file path or hyperlink to
    ' work like "C:\User\me\Desktop\newfile.xlsx" 
    ' .Name & "!A1" references a cell not the file location on the computer 
    ' or network
    'ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), _
    '                  Address:="", SubAddress:=.Name & "!A1", _
    '                  TextToDisplay:="View" '<--| hyperlink to new sheet

    ' you need something like this
    ' as long as newWB.Path property is set you should be good
    ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), _
                      Address:="", SubAddress:=newWB.Path, _
                      TextToDisplay:="View" '<--| hyperlink to new sheet
End With

回答02

这个答案使用了您最初想要的参考。我找不到堆栈溢出问题的链接但我测试了代码并且它有效。我喜欢这个选项,但只有工作簿在同一个Excel应用程序中才能使用。如果您打开了两个应用程序,它将无法工作,因为在应用程序中没有对新工作簿的引用。

With ActiveSheet '<--| the just pasted worksheet becomes the active one
    .Name = AddEmployeeUF.txtFirstname.Text + _
            AddEmployeeUF.txtMiddleinitial.Text + _
            AddEmployeeUF.txtLastname.Text + "Template" '<--| Name it

    ' the hyperlink SubAddress needs a valid file path or hyperlink to
    ' work like "C:\User\me\Desktop\newfile.xlsx" 
    ' .Name & "!A1" references a cell not the file location on the computer 
    ' or network
    'ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), _
    '                  Address:="", SubAddress:=.Name & "!A1", _
    '                  TextToDisplay:="View" '<--| hyperlink to new sheet

    ' you need something like this
    ' as long as newWB.Path property is set you should be good
    ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), _
                      Address:="", SubAddress:="'" & .Name & "'!A1", _
                      TextToDisplay:="View" '<--| hyperlink to new sheet
End With

答案 1 :(得分:0)

有人帮我找到了合适的解决方案,所以这就是:

public class ValidCombinations {
    Map<Integer, String> mapping = new HashMap<Integer, String>();

    public void run() {
            String s = "123123123";

            /*Convert String to int[]*/
            char[] cArray = s.toCharArray();
            int[] input = new int[cArray.length];

            for (int i=0; i<cArray.length; i++) {
                    input[i] = Character.getNumericValue(cArray[i]);
            }

            Set<String> output = new HashSet<String>();

            for (int i='A'; i<='Z'; i++) {
                    mapping.put(i - 'A' + 1, String.valueOf((char)i));
            }

            for (int i=0; i<input.length; i++) {
                    if (mapping.containsKey(input[i])) {
                            output.add(precombine(i, input) + mapping.get(input[i]) + postcombine(i, input));
                            if (i+1<input.length) {
                                    if (mapping.containsKey(input[i]*10 + input[i+1])) {
                                            output.add(precombine(i, input) + mapping.get(input[i]*10 + input[i+1]) + postcombine(i+1, input));
                                    }
                            }
                    }
            }

            System.out.println(output);
    }

    public String precombine(int i, int[] input) {
            String residue="";

            for (int m=0; m<i; m++) {
                    residue += mapping.get(input[m]);
            }

            return residue;
    }

    public String postcombine(int i, int[] input) {
            String residue="";

            for (int k=i+1; k<input.length; k++) {
                    residue += mapping.get(input[k]);
            }

            return residue;
    }

    public static void main(String[] args) {
            ValidCombinations v = new ValidCombinations();
            v.run();
    }