将分隔的Excel数据分隔成2列

我在Excel中的A列中有一些电子邮件地址。 有很多行,但是名称与每个电子邮件地址都在同一个单元格中,所以它看起来像这样:

johnsmith:johnsmith@gmail.com adamsmith:adam1i2@gmail.com CoryAdam:Cory1991@gmail.com 

有没有办法(在Excel中)分隔数据,以便我可以维护我的行,但在一列中的名称和在另一列中的电子邮件地址? 我也能自动做到这一点,在每天左右结束时说?

根据你对@ user496607的回答的评论,你的数据实际上是

 johnsmith:johnsmith@gmail.com adamsmith:adam1i2@gmail.com CoryAdam:Cory1991@gmail.com 

A列。 它是否正确?

每次打开工作簿时,是否都希望Excel自动将其分为A列和B列?

这个Sub做拆分

 Sub SplitNames(sh As Worksheet) Dim rng As Range Dim dat As Variant Dim i As Long, j As Long, s As String Set rng = sh.Range(sh.Cells(1, 1), sh.Cells(sh.Rows.Count, 1).End(xlUp)) 'rng.Select dat = rng.Formula If IsArray(dat) Then ReDim Preserve dat(1 To UBound(dat, 1), 1 To 2) For i = LBound(dat, 1) To UBound(dat, 1) s = dat(i, 1) j = InStr(dat(i, 1), ":") If Left(s, 1) <> "=" And j > 0 Then dat(i, 2) = Mid(s, j + 1) dat(i, 1) = Left(s, j - 1) End If Next rng.Resize(, 2).Formula = dat End If End Sub 

这可能会被另一个Sub调用,触发你想要的。 例如,这将打开Workbook (将其放在ThisWorkbook模块中)

 Private Sub Workbook_Open() Dim sh As Worksheet For Each sh In Me.Worksheets SplitNames sh Next End Sub 

假设列C3中存在电子邮件,您可以尝试以下操作(在D3列中编写公式):

=LEFT(C3,FIND("@",C3)-1)

然后,您可以将相同的公式应用于所有行。

这是一个使用文本到列的自动化解决scheme。 只要您的数据始终在单元格A1中开始,就像您所说的一样:

 Private Sub Workbook_Open() Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp)).Select Selection.textToColumns Destination:=Range("A1"), DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ :=":" End Sub 

将它放在ThisWorkbook模块中,每次打开工作簿时都会运行。