分配给工作表和按钮时,为什么我的模块中可用的VBA代码无法按预期工作

时间:2018-07-11 23:39:42

标签: excel vba excel-vba

我有一本工作簿,本质上是一个自动测试,标记和反馈工具,用于学生的主题测试结束。在“ 701Test”表上,他们通过下拉列表输入教学组,然后从后续列表中选择。他们回答多项选择题,并在完成后按一个按钮。该按钮会将他们带到“结果”页面,该页面为每个问题提供评分,为错误答案提供反馈并给出总分。然后,他们点击完成按钮,这将在我的文档文件夹中生成标记表的PDF副本,然后将副本通过电子邮件发送给自己和Schools电子邮件帐户。在这一点上,我还想将最终成绩发布到中央注册表中的学生记录中,使用遍历学生列表的方式查找名称和偏移量,以便从“结果”页面发布成绩值,最后返回测试页面。我为模块中的代码编写了最后一部分,它可以完美执行,但是当添加到主代码中并从按钮运行时,循环部分无法执行,但返回测试页确实有效,但是不会记录任何错误循环失败。

这里是“结果”页面代码的全文,底部的“ With Central reg”位是问题所在,任何帮助将不胜感激。

Private Sub CommandButton1_Click()

  Dim IsCreated As Boolean
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object
  Dim cell As Range
  Dim Students As Range

  Title = Range("D1").Value
  sname = Range("B2").Value
  PdfFile = CreateObject("WScript.Shell").SpecialFolders("MyDocuments") & "\" & sname & Title & ".pdf"


  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With

  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0

  With OutlApp.CreateItem(0)

    .Subject = Title
    .to = Range("B2").Value  ' <-- Put email of the recipient here"
    .CC = "" ' <-- Put email of 'copy to' recipient here
    .Body = "Hi," & vbLf & vbLf _
          & "Yr 7 701 EOT test attached in PDF format." & vbLf & vbLf _
          & "Regards," & vbLf _
          & "KDS ICT Dept" & vbLf & vbLf
    .Attachments.Add PdfFile

    Application.Visible = True
    .Display
  End With

  If IsCreated Then OutlApp.Quit

  Set OutlApp = Nothing


  With CentralReg


    For Each cell In Range("A2:A250")
    If cell = Range("Results!B2").Value Then
    cell.Offset(0, 4).Activate
    ActiveCell.Value = Range("Results!B27").Value
    End If
    Next


End With

End Sub 

3 个答案:

答案 0 :(得分:1)

我相信您正在尝试引用CentralReg这是一个工作表,这意味着您应该对它进行限定。

此外,您不应使类似于VBE中定义的对象/属性的变量变暗。尝试使用MyCell代替cell(好的做法,不是必需的)。

我假设您要查看CentralReg中工作表Column A上的值是否等于工作表Result B2。如果满足此条件,则您的MyCell将具有等于表Result B27

的值
Dim MyCell As Range
Dim Result, NewValue as Variant
Result = ThisWorkbook.Sheets("Result").Range("B2")
NewValue = ThisWorkbook.Sheets("Result").Range("B27")

With ThisWorkbook.Sheets("CentralReg")
    For Each MyCell In .Range("A2:A250")
        If MyCell = Result Then MyCell.Offset(, 4) = NewValue
    Next MyCell
End With

答案 1 :(得分:0)

with语句毫无用处,因为在构造中实际上没有使用它。

删除using SendGrid; using SendGrid.Helpers.Mail; using System.Collections.Generic; using System.Threading.Tasks; namespace Somenamespace.Utils { public class SMTPUtils { public async void SendEmail(string address, string toName, string fromName, string fromAddress, string subject, string body) { SendGridMessage msg = new SendGridMessage(); msg.SetFrom(new EmailAddress(fromAddress, fromName)); var recipients = new List<EmailAddress> { new EmailAddress(address, toName), }; msg.AddTos(recipients); msg.SetSubject(subject); msg.AddContent(MimeType.Text, body); await Execute(msg); } private async Task Execute(SendGridMessage msg) { var client = new SendGridClient(yourAPIKey); var response = await client.SendEmailAsync(msg); } } } protected void BtnSubmit_Click(object sender, EventArgs e) { string body = "some message body"; SMTPUtils smtp = new SMTPUtils(); smtp.SendEmail("someone@somewhere.com", "Bill Jo Bob Tex Jr.", "fromsomeone@somewhere.com", "noreply@example.com", "subject", body); } with CentralReg,它将起作用。

或者,如果CentralReg像是一张纸,那么您需要在代码前加上End with,这样:.变成这样:Range("A2:A250"),依此类推,{{1} }告诉代码,它与您的.Range("A2:A250")构造所围绕的内容有关

答案 2 :(得分:-1)

在行中是否可以修复使用单元格的任何问题。

If Cell.Value= Range("Results!B2").Value Then 

此外,在CentralReg之前应该执行的“ With”操作是什么?有什么事吗那里应该有Set语句将您链接到Excel Sheet吗?否则,我看不出它是如何找到For Loop循环经过的范围的。