我是VBA的新手,一直在网上搜索并观看YouTube教程,但我在编写下面的代码并让它工作时遇到了麻烦。 我正在使用同一工作簿中的两个电子表格。任何帮助将不胜感激。请理解我是初学者,需要一些指导。没有必要贬低评论。我已经在这方面工作了两个多星期而且无法理解。
我有一张标有“模板”的表格,单元格A1中有学生姓名。学生的名字将会改变,但名称的位置将始终在此单元格中。
在我的第二个标有“评估”的电子表格中,我需要在A栏中运行一个循环来查找学生姓名。
如果在搜索过程中找到学生姓名,那么我需要复制列AC中与找到姓名的行对应的任何信息。
然后需要将复制的任何内容粘贴到第A行第61-70行的第一个电子表格“模板”中,并自动添加适合复制行所需的任何其他行。
Option Explicit
Sub Test()
Dim StudentName As String '(StudentName is a unique identifier)
Dim Template As Worksheet '(this is the worksheet I'm pulling data into)
Dim Evaluations As Worksheet '(this is the sheet I'm pulling data from)
Dim finalrow As Integer
Dim i As Integer
Set Template = Sheets("Evaluation Form Template")
Set Evaluations = Sheets("Evaluations")
'this is where i want to cut and paste to
'getting an error here
Range("A61:A70").ClearContents
'This is the value I am looking for: getting an error here
StudentName = Sheets("Template").Range("A1").Value
'this is the sheet I am searching my value in Column A
finalrow = Sheets("Evaluations").Range("A10000").End(xlUp).Row
'once it runs the loop if the student name was found in Column A then I need it to copy and paste any information in Column 29/AC
'into my Template sheet in Column A row 61
For i = 2 To finalrow
If Cells(i, 1) = StudentName Then
Range(Cells(i, 29)).Copy
Sheets("template").Range("A61").End(xldown).Offset(1, 0).PasteSpecialxlPasteFormulasAndNumberFormats
End If
Next i
End Sub
答案 0 :(得分:0)
我可以看到一些可能存在问题的事情。您已将某些变量设置到工作表中,但您并未使用它们。例如,您是否尝试清除模板表中的内容,如下所示:
Template.Range("A61:A70").ClearContents
你可以得到这样的学生名字:
StudentName = Template.Range("A1").Value
'or shorter version
StudentName = Template.[A1]
在你的循环中,你在哪个表格中循环:
If Evaluations.Cells(i,1) = StudentName then
最后你不应该复制,你可以设置一个等于另一个的值来填充像这样的单元格:
Template.Range("A61").End(xldown).Offset(1,0) = Evaluations.Range(Cells(i,1),Cells(i,29))
取消这一点是为了确保您完全符合获取信息的位置以及您发送信息的位置。最后一件事,我不确定这是否意味着这样,但您使用Evaluation Form Template
作为Template
的工作表,但使用名称Template
作为在其他地方的参考。它们应该是一样的吗?
答案 1 :(得分:0)
1)你已经宣布了你的床单,但你没有使用它。
src = 'condition'+todayCondition+'Img.png';
document.getElementById('todayWeatherIcon').src = src;
然后写 - Set Template = ThisWorkbook.Sheets("Evaluation Form Template")
Set Evaluations = ThisWorkbook.Sheets("Evaluations")
而不是 - Template.Range("A1").Value
我认为您收到错误是因为您没有指定工作表:
写 - Sheets("Template").Range("A1").Value
而不是 - Template.Range("A61:A70").ClearContents
2)如果学生的名字是唯一的,您应该使用Range("A61:A70").ClearContents
方法而不是循环所有行。它会快得多。
返回一个Range对象,该对象表示第一个单元格 找到信息。 https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel
Range.Find
3)在开头添加以下行,它将使您的代码更快
Dim name_rg As range
{...}
' ~ Search name of the student ~
Set name_rg = Evaluation.columns(1).Find(Template.[a1])
If Not name_rg Is Nothing then
Template.[a61] = Evaluation.cells(name_rg.row, 29)
Else
MsgBox("No student found")
End If
4)在代码结束时,清除内存并将屏幕更新恢复为Application.ScreenUpdating = False
:
True
〜您的代码应如下所示:
Set name_rg = Nothing
Set Template = Nothing
Set Evaluations = Nothing
Application.ScreenUpdating = True
编辑:
如果 Option Explicit
Sub Test()
Application.ScreenUpdating = False
Dim StudentName As String
Dim Template As Worksheet
Dim Evaluations As Worksheet
Dim finalrow As Integer
Dim i As Integer
Dim name_rg As range
Set Template = ThisWorkbook.Sheets("Evaluation Form Template")
Set Evaluations = ThisWorkbook.Sheets("Evaluations")
Template.Range("A61:A70").ClearContents
' ~ Search name of the student ~
Set name_rg = Evaluation.columns(1).Find(Template.[a1])
If Not name_rg Is Nothing then
Template.[a61] = Evaluation.cells(name_rg.row, 29)
Else
MsgBox("No student found")
End If
Set name_rg = Nothing
Set Template = Nothing
Set Evaluations = Nothing
Application.ScreenUpdating = True
End Sub
中有多名学生,您需要执行Template
而不是使用For Loop
解决方案。在修改之下:
Range.Find