我有这样的数据:(我不知道如何正确地发布excel表,为此我进行了Apple-o-gize的操作。我也真的希望这些不是真实的电子邮件)
A B
1 String Email
2 Astring1 Aemail@nothingA.xom
3 Astring2 Aemail@nothingA.xom
4 Astring3 Aemail@nothingA.xom
5 Bstring1 Bemail@nothingB.xom
6 Bstring2 Bemail@nothingB.xom
7 Bstring3 Bemail@nothingB.xom
8 Bstring4 Bemail@nothingB.xom
9 Cstring1 Cemail@nothingC.xom
10 Cstring2 Cemail@nothingC.xom
我正在尝试根据电子邮件字段将范围设置为变量。
即 对于“ A”,下面的myRng为A2:A4,而“ B”为A5:A8,依此类推
Set myRng = Range("A1:A" & [the last row where the e-mails are the same])
我不认为For ... Next或Do循环会起作用,或者至少我不知道如何使它们起作用。
非常感谢您的帮助-谢谢!
答案 0 :(得分:1)
这是我所做的:
确定每个电子邮件ID的计数。使用countif:
=COUNTIF($B$2:$B$10,C2)
使用匹配项确定电子邮件地址出现的第一个单元格
=MATCH(C2,$B$2:$B$10,0)
创建地址范围
=ADDRESS(E2,1,4)&":"&ADDRESS(E2+F2-1,1,4)
您的excel文件如下所示:
A B C E F G
1 String Email Unique Emails count Cell start Range
2 Astring1 Aemail@nothingA.xom Aemail@nothingA.xom 3 2 A2:A4
3 Astring2 Aemail@nothingA.xom Bemail@nothingB.xom 4 5 A5:A8
4 Astring3 Aemail@nothingA.xom Cemail@nothingC.xom 2 9 A9:A10
5 Bstring1 Bemail@nothingB.xom
6 Bstring2 Bemail@nothingB.xom
7 Bstring3 Bemail@nothingB.xom
8 Bstring4 Bemail@nothingB.xom
9 Cstring1 Cemail@nothingC.xom
10 Cstring2 Cemail@nothingC.xom
下一步-VBA:
A2:A4
A5:A8
Sub Ranges_Macro()
Dim i As Integer
Dim Range_List As Range
Set Range_List = Sheet1.Range("Range_List")
For i = 1 To Range_List.Count
Dim myRng As Range
Dim Range_String As String
Range_List.Offset(i - 1, 0).Activate
Range_String = ActiveCell.Value
Set myRng = Range(Range_String)
Next i
End Sub
答案 1 :(得分:0)
您可以在B列中获得唯一项,然后对其进行处理。这是一个简单的示例,将唯一项放置在C列中
Sub UsingCollection()
Dim cUnique As Collection
Dim Rng As Range
Dim Cell As Range
Dim sh As Worksheet
Dim vNum As Variant
Set sh = ThisWorkbook.Sheets("Sheet1")
Set Rng = sh.Range("B2", sh.Range("B2").End(xlDown))
Set cUnique = New Collection
On Error Resume Next
For Each Cell In Rng.Cells
cUnique.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
For Each vNum In cUnique
Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = vNum
Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = "Do some thing with " & vNum
Next vNum
End Sub