我有一个带有If-Then-ElseIf
语句的Excel VBA宏,该语句位于For-Next
循环内。在描述问题之前,我将展示代码。我发布了整个代码,以防万一我认为问题出在If语句中是错误的。
代码:
Option Explicit
Sub GetData()
Dim wsPasteTo As Worksheet, wbDATA As Workbook
Dim NextRow As Long, LastRow As Long, i As Long
Set wsPasteTo = ThisWorkbook.Sheets("ACP")
NextRow = wsPasteTo.Range("A" & Rows.Count).End(xlUp).Row + 2
Set wbDATA = Workbooks.Open("\\cmicro.com\Shares\Amb\Amb-Probes\DataLogs\CQS-03-033-2012 Coax Shelf Cut Log R2.6.xlsm", ReadOnly:=True)
Application.ScreenUpdating = False
With wbDATA.Sheets("ACP")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, "E") = "Angle" Then
.Range("K2:L" & LastRow).Copy
wsPasteTo.Range("I" & NextRow).PasteSpecial xlPasteValues
ElseIf Cells(i, "E") = "Vertical" Then
.Range("K2:L" & LastRow).Copy
wsPasteTo.Range("D" & NextRow).PasteSpecial xlPasteValues
Else
.Range("K2:L" & LastRow).Copy
wsPasteTo.Range("N" & NextRow).PasteSpecial xlPasteValues
End If
Next i
End With
Application.ScreenUpdating = True
wbDATA.Close False
End Sub
此宏根据“复制工作簿”的E列中的内容复制另一个Excel工作簿中的数据(让我们将其称为“复制工作簿”)。 E列有3个选项供用户选择:" Angle"," Vertical"和" n / a"。我使用了数据验证,因此用户必须从所有列E单元格的下拉列表中选择这3个选项中的一个。基于此,宏将数据粘贴到单独工作簿中的列中,让我们将其称为粘贴工作簿。
此代码没有错误,但所有数据都粘贴到粘贴工作簿中的错误位置。
我认为问题在于代码行:
If Cells(i, "E") = "Angle" Then
和
ElseIf Cells(i, "E") = "Vertical" Then
因为当我处于调试模式时,会跳过这些行,就好像字符串变量"垂直"和"角度"列#34; E"中没有。这就是所有数据进入一个地方的原因。
我无法弄清楚代码有什么问题。我查看了复制工作簿中的E列,没有拼写/大小写问题。也许一个Case声明对我正在做的事情会更好,但我对VBA并不精明,我也不知道如何做到这一点。
答案 0 :(得分:0)
Option Explicit
Sub GetData()
Dim wsPasteTo As Worksheet, wbDATA As Workbook
Dim NextRow As Long, LastRow As Long, i As Long
Dim val, col
Set wsPasteTo = ThisWorkbook.Sheets("ACP")
NextRow = wsPasteTo.Range("A" & Rows.Count).End(xlUp).Row + 2
Set wbDATA = Workbooks.Open( _
"\\cmicro.com\Shares\Amb\Amb-Probes\DataLogs\" & _
"CQS-03-033-2012 Coax Shelf Cut Log R2.6.xlsm", ReadOnly:=True)
Application.ScreenUpdating = False
With wbDATA.Sheets("ACP")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To LastRow
val = .Cells(i, "E").Value
Select Case val
Case "Angle": col = "I"
Case "Vertical": col = "D"
Case Else: col = "N"
End Select
wsPasteTo.Cells(NextRow, col).Resize(1,2).Value = _
.Cells(i, "K").Resize(1, 2).Value
NextRow = NextRow + 1 'or whatever....
Next i
End With
Application.ScreenUpdating = True
wbDATA.Close False
End Sub
答案 1 :(得分:0)
以下是工作代码:
Option Explicit
Sub GetData()
Dim wsPasteTo As Worksheet, wbDATA As Workbook
Dim NextRow As Long, LastRow As Long, i As Long
Dim val, col
Set wsPasteTo = ThisWorkbook.Sheets("ACP")
Set wbDATA = Workbooks.Open("\\cmicro.com\Shares\Amb\Amb-Probes\DataLogs\CQS-03-033-2012 Coax Shelf Cut Log R2.6.xlsm", ReadOnly:=True)
Application.ScreenUpdating = False
With wbDATA.Sheets("ACP")
LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
For i = 2 To LastRow
val = .Cells(i, "E").Value
Select Case val
Case "Angle": col = "I"
NextRow = wsPasteTo.Range("I" & Rows.Count).End(xlUp).Row + 1
Case "Vertical": col = "D"
NextRow = wsPasteTo.Range("D" & Rows.Count).End(xlUp).Row + 1
Case Else: col = "N"
NextRow = wsPasteTo.Range("N" & Rows.Count).End(xlUp).Row + 1
End Select
wsPasteTo.Cells(NextRow, col).Resize(1, 2).Value = .Cells(i, "K").Resize(1, 2).Value
Next i
End With
Application.ScreenUpdating = True
wbDATA.Close False
End Sub
我忘记了每种类型数据的'NextRow'都是唯一的。
感谢你的帮助蒂姆。