使用VBA将数据从一个工作表复制到另一个工作表

时间:2014-05-28 23:09:26

标签: excel vba excel-vba

我在Excel中遇到了一些问题。我需要同步C列中的大括号{}中的值,并将它们放在F列中的用户ID。我希望将这些数据复制到同一工作簿中的新工作表。任何想法我怎么能做到这一点?你不必提供任何代码,但在正确的方向上推动会很棒。

E.g。在电子邮件

enter image description here

在新表

上成为了这个

enter image description here

2 个答案:

答案 0 :(得分:1)

如果有人需要帮助,这就是解决方案:

Sub CopyConditional()
    Dim wshS As Worksheet
    Dim WhichName As String

    Set wshS = ActiveWorkbook.Sheets("Emails")
    WhichName = "NewSheet"

    Const NameCol = "C"
    Const FirstRow = 1

    Dim LastRow As Long
    Dim SrcRow As Long
    Dim TrgRow As Long
    Dim wshT As Worksheet
    Dim cpt As String
    Dim user As String
    Dim computers() As String
    Dim computer As String

    On Error Resume Next
    Set wshT = Worksheets(WhichName)
    If wshT Is Nothing Then
        Set wshT = Worksheets.Add(After:=wshS)
        wshT.Name = WhichName
    End If
    On Error GoTo 0

    If wshT.Cells(1, NameCol).value = "" Then
        TrgRow = 1
    Else
        TrgRow = wshT.Cells(wshT.Rows.Count, NameCol).End(xlUp).Row + 1
    End If

    LastRow = wshS.Cells(wshS.Rows.Count, NameCol).End(xlUp).Row
    For SrcRow = FirstRow To LastRow
        cpt = wshS.Range("C" & SrcRow).value
        user = wshS.Range("F" & SrcRow).value

        If InStr(cpt, ":") Then
            cpt = Mid(cpt, InStr(1, cpt, ":") + 1, Len(cpt))
        End If

        If InStr(cpt, ";") Then
            computers = Split(cpt, ";")
            For i = 0 To UBound(computers)
                If computers(i) <> "" Then
                    wshT.Range("A" & TrgRow).value = user
                    wshT.Range("B" & TrgRow).value = Mid(Left(computers(i), Len(computers(i)) - 1), 2)
                    TrgRow = TrgRow + 1
                End If
            Next
        Else
            computer = cpt
            If computer <> "" Then
                wshT.Range("A" & TrgRow).value = user
                wshT.Range("B" & TrgRow).value = Mid(Left(computer, Len(computer) - 1), 2)
                TrgRow = TrgRow + 1
            End If
        End If

    Next SrcRow

End Sub

答案 1 :(得分:-2)

你没有问一个问题。基本上你要做的是

loop through the values in column F
  for each value, get the value in column C
  loop through all braced values in column C
    let braceValue = parse column C searching for {value}
    create a row in new worksheet with column F value, braceValue