Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于将名称转换为电子邮件的Excel宏_Excel_Vba - Fatal编程技术网

用于将名称转换为电子邮件的Excel宏

用于将名称转换为电子邮件的Excel宏,excel,vba,Excel,Vba,我知道这个问题的某些部分以前曾被问过,但我正试图让它成为一个“一站式”的宏。我经常有名字的列表(第一个,最后一个)。我需要根据公司的命名惯例,取下这些名字并创建电子邮件 例如: FirstLast@company.com F。Last@company.com FLast@company.com “我的宏”有一个输入框,用于询问命名约定的格式和公司域。我不是VBS专家,所以我需要一些帮助。这是我到目前为止所拥有的。这是我试图完成的一个小摘录 Sub email() Dim format

我知道这个问题的某些部分以前曾被问过,但我正试图让它成为一个“一站式”的宏。我经常有名字的列表(第一个,最后一个)。我需要根据公司的命名惯例,取下这些名字并创建电子邮件

例如:

  • FirstLast@company.com
  • F。Last@company.com
  • FLast@company.com
“我的宏”有一个输入框,用于询问命名约定的格式和公司域。我不是VBS专家,所以我需要一些帮助。这是我到目前为止所拥有的。这是我试图完成的一个小摘录

Sub email()
    Dim format
    format = Application.InputBox("Set Email Format", "Format", "first.last", Type:=2)
    Dim fqdn
    fqdn = Application.InputBox("Enter Email Domain", "Domain", Type:=2)


    If format = "first.last" Then
        ActiveCell.FormulaR1C1 = _
            "=RC[-2]&'.'&RC[-1]&fqdn"
    ElseIf format = "flast" Then
        ActiveCell.FormulaR1C1 = _
            "=CONCATENATE(LEFT(RC[-2],1), RC[-1], fqdn)"
    End If
End Sub
问题之一是将fqdn变量添加到公式中。我遇到的另一个问题是,我在尝试运行对象“Range”的方法“FormulaR1C1”时,不断遇到“运行时错误“1004”:失败。似乎找不到根本原因。我尝试过添加范围值,但没有任何效果

我知道你想做什么, 改为做一个函数

    Function email(firstName As String, lastName As String, convention As String,domain As String)
    'domain = "@company.com"
    'convention values : firstlast, flast, f.last

      Select case convention 
           Case "firstlast" 
               email = firstName & lastName & domain 
           Case "flast"
               email = left(firstName,1) & lastNAme & domain
           Case Else
               email = left(firstName,1) & "." & lastNAme & domain
       End Select
    End Function

'use like this : Dave | cruz | = email("dave","cruz","firstlast") 

    Sub UpdateEmail()
         LastRow = sheet("name").Cells(sheet("name").Rows.Count, "A").End(xlUp).Row
        Dim format
        formatType = Application.InputBox("Set Email Format")
        Dim fqdn
        fqdn = Application.InputBox("Enter Email Domain", "Domain", Type:=2)
     dim c
    for each c in range( "A" & lastrow) 'your range
       c = email( c.offset(,-1),c.offset(,-2),"@" & fqdn, formatType)
     next 
        End If
    End Sub

如果我是你,我会为此编写一个自定义函数:

Public Function FormatEmail(FirstName As String, LastName As String, Domain As String, Optional ByVal UseDot As Boolean = True) As String
    Dim Dot As String
    If UseDot Then
        Dot = "."
    Else
        Dot = ""
    End If
    FormatEmail = Left(FirstName, 1) & Dot & LastName & "@" & Domain
End Function
然后像这样使用它:

=FormatEmail("Bill","Gates","microsoft.com",0)

'returns BGates@microsoft.com

=FormatEmail("Bill","Gates","microsoft.com",1)

'returns B.Gates@microsoft.com

请尝试
ActiveCell.FormulaR1C1==RC[-2]&“&RC[-1]&“fqdn”
。无效。“编译错误:预期:语句结束”(突出显示第一个公式上的句点。使用复选框确定格式可能更方便用户?我同意@findwindow我可能稍后再尝试。目前,我希望获得一个工作宏,这是我最后一个建议的最简单方法:
ActiveCell.FormulaR1C1=“=RC[-2]&”“”&RC[-1]&“fqdn”“”
。如果不起作用,请在单元格(常规格式)中编写公式,然后转到选项-->公式,并单击“R1C1样式”。然后录制宏,用公式选择单元格,然后单击“ENTER”重新输入公式。然后查看生成的代码。公式在哪里FirstLast@company.com“选择“给了我一个编译错误。只是说”case“很好。为我的n00b状态道歉。我如何让它写入数据?对使用函数不太熟悉。在函数上方添加了我的输入框,我正在使用email=firstName&lastName&fqdn不要像公式一样使用它,在单元格中插入你的公式=email(A1;B1;“firstlast”,“@compagny.com”)您也可以在vba中使用它:customEmail=email(“dave”、“cruz”、“firstlast”和“@comp.com”)需要将其自动化。将公式插入单元格会破坏宏的用途。我知道完成工作的公式。只是希望将过程自动化,而不是插入公式并将其向下拖动500行。