- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 VBA 新手,在尝试编写的宏中使用 If 语句时遇到了一些困难。每个月我都会收到一份 Excel 报告,其中列出了我们公司的哪些员工执行了某些任务。我正在编写的宏旨在将每个员工的数据复制并粘贴到主工作簿中。
我遇到的问题是定义我需要复制的范围。正如您将在代码中看到的那样,员工列在 B 列中。我首先在 B 列中搜索员工。如果他们不存在,宏复制并粘贴(无)在主工作簿中他们的名字下.如果找到他们的名字,它会将他们名字下面的行设置为第一个变量。
这是我遇到问题的地方。下一步是找到列出的下一个员工,并将上面的行设置为第二个变量。然后我使用第一个和第二个变量来复制和粘贴该范围的行。我正在使用 If 语句循环并查找列出的下一个员工。但是,我的嵌套 If 语句在我的第二个 Else if 语句之后结束。有谁知道我可以写得更好的方法吗?我尝试使用 Select Case 语句,但语法不正确。
Sub EmployeeActivity()
Dim Employee1 As Integer, Employee2 As Integer, Employee3 As Integer, Employee4 As Integer
Dim EmployeeRange As Range, rngSelectFind As Range, rngPasteFind As Range
Windows("Activities Report.xlsm").Activate
Set rngSelectFind = Columns("B:B").Find(What:="Employee 1", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee1 = rngSelectFind.Row + 1
ElseIf rngSelectFind Is Nothing Then
Set rngSelectFind = Columns("B:B").Find(What:="(none)", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
Consultant3 = rngSelectFind.Row
End If
Set rngSelectFind = Columns("B:B").Find(What:="Employee 2", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
ElseIf rngSelectFind Is Nothing Then
Set rngSelectFind = Columns("B:B").Find(What:="Employee 3", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
End If
ElseIf rngSelectFind Is Nothing Then
Set rngSelectFind = Columns("B:B").Find(What:="(none)", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
End If
End If
If Employee1 > 0 And Employee2 > 0 Then
Set EmployeeRange = Range(Cells(Employee1, 2), Cells(Employee2, 7))
ElseIf Employee3 > 0 Then
Set EmployeeRange = Range(Cells(Employee3, 2), Cells(Employee3, 7))
End If
EmployeeRange.Select
Selection.Copy
Windows("Monthly Activity Report.xlsm").Activate
Sheets("April '13").Activate
Set rngPasteFind = Columns("A:A").Find(What:="Employee Activities", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngPasteFind Is Nothing Then
Employee4 = rngPasteFind.Row + 1
End If
Range(Cells(Employee4, 1), Cells(Employee4, 6)).Select
Selection.Insert (xlShiftDown)
End Sub
最佳答案
我注意到的几件事。
.Activate
和 Selection
.直接使用对象。您可能想查看 THIS .Find
然后迎合您找不到匹配项的情况。您已经在几个地方这样做了,但后来又错过了一些。 Employee1
, Employee2
等为Integer
.在 Excel 2007+ 中,这可能会给您一个错误,因为 Excel 2007+ 支持 1048576 行。使用 Long
反而。 EmployeeRange
当您不打算将其粘贴到任何地方时?我看到您声明了 Paste
虽然范围... Sub EmployeeActivity()
Dim Employee1 As Long, Employee2 As Long, Employee3 As Long, Employee4 As Long
Dim EmployeeRange As Range, rngSelectFind As Range, rngPasteFind As Range
Dim wb As Workbook, ws As Worksheet
Dim wb1 As Workbook, ws1 As Workbook
'~~> Change path as applicable
Set wb = Workbooks.Open("C:\Activities Report.xlsm")
'~~> Change this to the relevant sheet
Set ws = wb.Sheets("Sheet1")
'~~> Change path as applicable
Set wb1 = Workbooks.Open("C:\Monthly Activity Report.xlsm")
Set ws1 = wb.Sheets("April '13")
With ws
Set rngSelectFind = .Columns("B:B").Find(What:="Employee 1", _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee1 = rngSelectFind.Row + 1
Else
Set rngSelectFind = .Columns("B:B").Find(What:="(none)", _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Consultant3 = rngSelectFind.Row
End If
End If
Set rngSelectFind = Nothing
Set rngSelectFind = .Columns("B:B").Find(What:="Employee 2", _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
Else
Set rngSelectFind = .Columns("B:B").Find(What:="Employee 3", _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
Else
Set rngSelectFind = .Columns("B:B").Find(What:="(none)", _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
End If
End If
End If
If Employee1 > 0 And Employee2 > 0 Then
Set EmployeeRange = .Range(.Cells(Employee1, 2), _
.Cells(Employee2, 7))
ElseIf Employee3 > 0 Then
Set EmployeeRange = .Range(.Cells(Employee3, 2), _
.Cells(Employee3, 7))
End If
End With
'~~> I am not sure why are you copying this range???
If Not EmployeeRange Is Nothing Then EmployeeRange.Copy
With ws1
Set rngPasteFind = .Columns("A:A").Find(What:="Employee Activities", _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
If Not rngPasteFind Is Nothing Then
Employee4 = rngPasteFind.Row + 1
.Range(.Cells(Employee4, 1), .Cells(Employee4, 6)).Insert (xlShiftDown)
End If
End With
End Sub
.Find
可以接受参数的函数。这样你就可以大大减少上面的代码;)
.Find
在代码中一次又一次。
Sub EmployeeActivity()
Dim Employee1 As Long, Employee2 As Long
Dim Employee3 As Long, Employee4 As Long
Dim EmployeeRange As Range, rngSelectFind As Range, rngPasteFind As Range
Dim wb As Workbook, ws As Worksheet
Dim wb1 As Workbook, ws1 As Workbook
'~~> Change path as applicable
Set wb = Workbooks.Open("C:\Activities Report.xlsm")
'~~> Change this to the relevant sheet
Set ws = wb.Sheets("Sheet1")
'~~> Change path as applicable
Set wb1 = Workbooks.Open("C:\Monthly Activity Report.xlsm")
Set ws1 = wb.Sheets("April '13")
With ws
Employee1 = GetRow(ws, 2, "Employee 1")
If Employee1 <> 0 Then
Employee1 = Employee1 + 1
Else
Consultant3 = GetRow(ws, 2, "(none)")
End If
'
'And So on
'
End Sub
Function GetRow(wks As Worksheet, ColNo As Long, SearchString As String) As Long
Dim rng As Range
Set rng = wks.Columns(ColNo).Find(What:=SearchString, _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
If rng Is Nothing Then
GetRow = 0
Else
GetRow = rng.Row
End If
End Function
关于vba - 嵌套 If 语句有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16406905/
创建一个“海盗对话”,可以选择左手或右手。我希望它对“左”和“右”的不同拼写做出积极的回答(正如您将在代码中看到的那样),但是,当我为所有非“右”或“左”的输入添加最终的“else”代码时,它给了我一
With 语句 对一个对象执行一系列的语句。 With object statements End With 参数 object 必需的部分
While...Wend 语句 当指定的条件为 True 时,执行一系列的语句。 While condition  ; Version [stat
所以我正在处理的代码有一个小问题。 while True: r = input("Line: ") n = r.split() if r == " ":
我有一个对象数组: var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number"
int main() { int f=fun(); ... } int fun() { return 1; return 2; } 在上面的程序中,当从main函数中调用一个
我的项目中有很多 if 语句、嵌套 if 语句和 if-else 语句,我正在考虑将它们更改为 switch 语句。其中一些将具有嵌套的 switch 语句。我知道就编译而言,switch 语句通常更
Rem 语句 包含程序中的解释性注释。 Rem comment 或 ' comment comment 参数是需要包含的注释文本。在 Rem 关键字和 comment 之间应有一个空格。
ReDim 语句 在过程级中声明动态数组变量并分配或重新分配存储空间。 ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
Randomize 语句 初始化随机数生成器。 Randomize [number] number 参数可以是任何有效的数值表达式。 说明 Randomize 使用 number 参数初始
Public 语句 定义公有变量并分配存储空间。在 Class 块中定义私有变量。 Public varname[([subscripts])][, varname[([subscripts])
Sub 语句 声明 Sub 过程的名称、参数以及构成其主体的代码。 [Public [Default]| Private] Sub name [( arglist )]
Set 语句 将对象引用赋给一个variable或property,或者将对象引用与事件关联。 Set objectvar = {objectexpression | New classname
我有这个代码块,有时第一个 if 语句先运行,有时第二个 if 语句先运行。我不确定为什么会这样,因为我认为 javascript 是同步的。 for (let i = 0; i < dataObje
这是一个 javascript 代码,我想把它写成这样:如果此人回答是,则回复“那很酷”,如果此人回答否,则回复“我会让你开心”,如果此人回答的问题包含"is"或“否”,请说“仅键入”是或否,没有任何
这是我的任务,我尝试仅使用简短的 if 语句来完成此任务,我得到的唯一错误是使用“(0.5<=ratio<2 )”,除此之外,构造正确吗? Scanner scn = new Scanner(
有没有办法在 select 语句中使用 if 语句? 我不能在这个中使用 Case 语句。实际上我正在使用 iReport 并且我有一个参数。我想要做的是,如果用户没有输入某个参数,它将选择所有实例。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: If vs. Switch Speed 我将以 C++ 为例,但我要问的问题不是针对特定语言的。我的意思是一
Property Set 语句 在 Class 块中,声明名称、参数和代码,这些构成了将引用设置到对象的 Property 过程的主体。 [Public | Private] Pro
Property Let 语句 在 Class 块中,声明名称、参数和代码等,它们构成了赋值(设置)的 Property 过程的主体。 [Public | Private] Prop
我是一名优秀的程序员,十分优秀!