为所有邮件ID

时间:2016-04-07 07:29:36

标签: excel vba excel-vba macros

我刚接触Excel VBA,我开始编写一个代码,执行得很好,但我需要一个建议如何编写一个函数,我不需要为所有人编写代码&#34; ID&#34;。< / p>

例如: 我有主要工作表有ID(1000x,10000,2000X,20000)。 我想只搜索带有数字而不是字母的ID,并将其与具有相同ID的另一个工作表进行比较,如果那么获取相应的ID第3列数据并将它们全部放入主工作表中。

我有主要工作表(&#34; Tabelle1&#34;),其中包含Coloumn A中的所有ID(10000,20000),我希望在ID为10000的B列中输入ID为10000.有时我有10000四次 。想要将信息粘贴到另一个工作表(&#34; Test_2&#34;),我想收集所有10000和相应的数据。

Sub Update()
If MsgBox("Are you sure that you wish to Update New Measurement ?", vbYesNo, "Confirm") = vbYes Then
Dim erow As Long, erow1 As Long, i As Long
erow1 = Sheets("Tabelle1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To erow1
If Sheets("Tabelle1").Cells(i, 2) <> "10000" Then
Sheets("Tabelle1").Range(Sheets("Tabelle1").Cells(i, 1), Sheets("Tabelle1").Cells(i, 2)).Copy
Sheets("Test_2").Activate
erow = Sheets("Test_2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

ActiveSheet.Paste Destination:=Sheets("Test_2").Range(Cells(erow, 1), Cells(erow, 2))
Sheets("Test_2").Activate

End If
Next i
Application.CutCopyMode = False


For i = 1 To erow
Totalstrings = Totalstrings & Cells(i, 2) & "" + vbCrLf
Next i
Totalstrings = Left(Totalstrings, Len(Totalstrings) - 1)
Range("C5") = Totalstrings


Range("C5").Select
Selection.Copy
Sheets("BSM_STF_iO").Select
Range("C5").Select
ActiveSheet.Paste
 MsgBox "New measurements  have been Updated !"
End If

End Sub

实施例

在BSM中:STM:IO

A B
ID
1000X
10000个
10001个
......

Tabelle1中的

          B C
          ID
          1000 abc
          1000 xyz
          10001百万           2000 def
&#34; 我想只比较&#34;&#34; BSM:STM:Io&#34;与&#34; tabelle1&#34;。示例从&#34; BSM_STM_io&#34;中获取第一个值10000与tabele相比较,取得相应的Coloumn&#34; C&#34; in&#34; tablle1&#34;并将其放入1000 BSM_STM的单个单元格中:Io

  • A,B,C在工作表中是coloumn

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

让我们假设工作表&#34; BSM_STF_iO&#34;包含以A2开头的A列中的ID信息,工作表Tabelle1包含从B2开始的B列中所需的concetenation信息(例如:列B:ID,列C:要发送的信息)。下面的代码将会忘记内容并写入BSM_STF_iO表。

Sub test1()
Worksheets("BSM_STF_iO").Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
    a = onlyDigits(Range("A" & i).Value)
    With Worksheets("Tabelle1")
        destlastrow = .Range("B" & Rows.Count).End(xlUp).Row
        For j = 2 To destlastrow
            If a = Trim(.Range("B" & j).Value) Then
                If out <> "" Then
                    out = out & ", " & .Range("C" & j).Value
                Else
                    out = .Range("C" & j).Value
                End If
            End If
        Next j
        Cells(i, 2) = out
        out = ""
    End With
Next i
End Sub

以及来自How to find numbers from a string?

的函数
Function onlyDigits(s As String) As String
    Dim retval As String
    Dim i As Integer
    retval = ""
    For i = 1 To Len(s)
        If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
            retval = retval + Mid(s, i, 1)
        End If
    Next
    onlyDigits = retval
End Function