查找页面并为单元格分配值

时间:2018-10-06 21:19:01

标签: excel vba excel-vba

我有一个Excel文件,共有20张

我需要在excel上搜索并找到一些表格,然后将其添加到M1 Cell中

能请你帮我吗?

3 个答案:

答案 0 :(得分:0)

Application.ScreenUpdating = False
On Error Resume Next
Application.DisplayAlerts = False
döngü:
For i = 1 To Worksheets.Count
    If Worksheets(i).Name = "ABC" Then
    Sheets("ABC").Select
    Range("M1").Select
    ActiveCell.FormulaR1C1 = "abc@hotmail.com"
    If Worksheets(i).Name = "ABC2" Then
    Sheets("ABC2").Select
    Range("M1").Select
    ActiveCell.FormulaR1C1 = "abc2@hotmail.com"
    GoTo döngü:
pass:
Next i
Application.ScreenUpdating = True
MsgBox "mail assign done"
End Sub

答案 1 :(得分:0)

您可以简单地使用以下子项。

Sub WriteEmail()
On Error GoTo HarunErrHandler

    Sheets("ABC").Range("M1") = "abc@hotmail.com"
    Sheets("ABC2").Range("M1") = "abc2@hotmail.com"

Exit Sub
HarunErrHandler:
MsgBox "No such sheet found.", vbInformation, "Info"
'MsgBox("Error Number: " & Err.Number & vbCrLf & Err.Description, vbCritical, "Error")
End Sub

答案 2 :(得分:0)

非常感谢Harun,我对您的评论做了一些改动。

以下代码效果很好。

非常感谢您

Sub WriteEmail()
    Sheets("ABC").Range("M1") = "abc@hotmail.com"
    On Error Resume Next
    Sheets("ABC2").Range("M1") = "abc2@hotmail.com"
    On Error Resume Next
    Sheets("ABC3").Range("M1") = "abc3@hotmail.com"
    On Error Resume Next
    MsgBox ("process done")
End Sub