excel 循环的结果未被IF [closed]验证

7y4bm7vi  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(105)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
3天前关闭。
Improve this question
我在Sheet1中搜索条件“System Name”,我可以找到它,我可以使用“Debug.Print”进行确认,但if语句不接受它作为值

Sub Hostnames()
'
' Hostnames Macro
'

Dim lastrow As Long, erow As Long
lastrow = Sheet1.Cells(rows.Count, 1).End(xlUp).row

For i = 2 To lastrow
    If Sheet1.Cells(i, 2) = "System Name" Then
    Sheet1.Cells(i, 5).Copy
    erow = Sheet2.Cells(rows.Count, 1).End(xlUp).Offset(1, 0).row
    Sheet1.Paste destination:=Worksheets(“Sheet2”).Cells(erow, 1)
    End If

Next i
Application.CutCopyMode = False
Sheet2.Columns().AutoFit
Range(“A1”).Select

End Sub

打印,例如:

z2acfund

z2acfund1#

愚蠢的问题,但你的工作表中的值是相同的文本大小写,你正在寻找对不对?“系统名称”和“系统名称”将不相等的比较,如果语句会给予假。

ccrfmcuu

ccrfmcuu2#

我相信您已经关闭了剪切/复制模式,并且从未打开它。
给予看吧。为了方便起见,我更改了工作表引用,我个人发现在子例程开始时设置对象会更好一些。

Sub Hostnames()
'
' Hostnames Macro
'

    Dim lastRow As Long, eRow As Long
    Dim firstSheet As Worksheet, secondSheet As Worksheet
    ' Set worksheets objects
    Set firstSheet = ActiveWorkbook.Sheets("Sheet1")
    Set secondSheet = ActiveWorkbook.Sheets("Sheet2")
    ' Get last row of first sheet.
    lastRow = firstSheet.Cells(firstSheet.Rows.Count, 2).End(xlUp).Row
    ' Ensure you can cut/copy/paste
    Application.CutCopyMode = True
    Debug.Print lastRow
    For i = 2 To lastRow
        If firstSheet.Cells(i, 2) = "System Name" Then
            firstSheet.Cells(i, 5).Copy
            eRow = secondSheet.Cells(secondSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            firstSheet.Paste Destination:=secondSheet.Cells(eRow, 1)
        End If
    Next i
    ' Turn off cut/copy mode
    Application.CutCopyMode = False
    ' Autofit columns for both sheets
    secondSheet.Columns().AutoFit
End Sub

相关问题