当单击特定单元格时,如何将一个单元格的信息复制到另一个工作表上的单元格?

时间:2017-05-01 18:02:53

标签: excel vba excel-vba

我一直试图创建这个宏,这样,当我点击某个单元格(在Excel上)时,另一个工作表上的信息可以复制到指定的单元格上。例如。如果我单击Worksheet1中的A1,则工作表3上的B20:C20将复制到B20:Worksheet2的C20。任何人都可以帮我创建必要的宏吗?

1 个答案:

答案 0 :(得分:0)

尝试按照以下示例操作:

private static bool TryGetName(string input, string defaultValue, out string name)
{
    var stringsBeforeIntroduction = new List<string>
    {
        "I am called ",
        "My name is ",
        "People call me ",
        "You can call me ",
        "You can refer to me as "
    };

    var charactersAfterName = new char[]
    {
        ' ', ',', '.', '\n', ';', ':'
    };

    var firstPrefixFound = stringsBeforeIntroduction.FirstOrDefault(prefix =>
        input.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) > -1);

    if (firstPrefixFound != null)
    {
        // Calculate the indexes of the start and end of the name
        int prefixLength = firstPrefixFound.Length;
        int startName = input.IndexOf(firstPrefixFound, 
            StringComparison.OrdinalIgnoreCase) + prefixLength;
        int endOfName = input.IndexOfAny(charactersAfterName, startName) - startName;

        if (endOfName < 0) endOfName = input.Length - startName;

        // Assign the name that we found
        name = input.Substring(startName, endOfName);
    }
    else
    {
        name = defaultValue;
        return false;
    }

    return true;
}