简体   繁体   English

Excel VBA 2010-If-Then-ElseIf语句无法识别字符串变量

[英]Excel VBA 2010 - If-Then-ElseIf statement not recognizing string variables

I have an Excel VBA macro with an If-Then-ElseIf statement that is within a For-Next loop. 我有一个Excel VBA宏,其中包含一个For-Next循环中的If-Then-ElseIf语句。 I'll show the code before describing the issue. 在描述问题之前,我将显示代码。 I post the entire code just in case I'm wrong about thinking the problem lies within the If statement. 我发布了整个代码,以防万一我认为问题出在If语句之内。

Code: 码:

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

This macro copies data from another Excel workbook (let's call it the Copy Workbook) based on what's in column E of the Copy Workbook. 此宏根据“复制工作簿”的E列中的内容从另一个Excel工作簿(称为“复制工作簿”)复制数据。 Column E has 3 options for the user to choose: "Angle", "Vertical", and "n/a". E列有3个选项供用户选择:“角度”,“垂直”和“ n / a”。 I used data validation so the user must chose one of those 3 options from a drop down in all column E cells. 我使用了数据验证,因此用户必须从所有E列单元格的下拉菜单中选择这3个选项之一。 Based on that, the macro pastes data into columns in a separate workbook, let's call it the Paste Workbook. 基于此,宏将数据粘贴到单独工作簿中的列中,我们将其称为“粘贴工作簿”。

This code doesn't have errors, however all the data is being pasted into the wrong place in the Paste Workbook. 该代码没有错误,但是所有数据都被粘贴到“粘贴工作簿”中的错误位置。

I think the problem is with the lines of code: 我认为问题出在代码行:

If Cells(i, "E") = "Angle" Then

and

ElseIf Cells(i, "E") = "Vertical" Then

because when I'm in debug mode, these lines are skipped as though the string variables "Vertical" and "Angle" aren't there in column "E". 因为当我处于调试模式时,这些行会被跳过,就好像“ E”列中没有字符串变量“ Vertical”和“ Angle”一样。 That's why all the data is going into one place. 这就是为什么所有数据都集中在一个地方的原因。

I can't figure what is wrong with the code. 我不知道代码有什么问题。 I checked out column E in the Copy Workbook and there are no spelling/capitalization issues. 我签出了“复制工作簿”中的E列,并且没有拼写/大写问题。 Maybe a Case statement would be better for what I'm doing, but I'm not savvy with VBA and I don't know how to do that. 也许用Case语句对我所做的事情会更好,但是我对VBA并不了解,我也不知道该怎么做。

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

Here is the working code: 这是工作代码:

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

I forgot that the 'NextRow' for each type of data would be unique. 我忘记了每种数据类型的“ NextRow”都是唯一的。

Thanks for your help Tim. 感谢您的帮助,蒂姆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM