不写入单元格的值,但弹出窗口

时间:2017-08-28 12:43:38

标签: excel-vba for-loop if-statement vba excel

需要关于以下代码的帮助,这些代码不会向单元格写入值,而是显示msgbox

Sub SPD()
    Dim Col1, col2, col3, i, j, LRow, LCol As Long
    Dim st1, st2, st3 As String
    i = 0
    j = 0
    ThisWorkbook.Sheets.Add
    st1 = "Salus Hosting"
    LRow = Workbooks("Data").Sheets("Data").Cells(Rows.Count, 2).End(xlUp).Row

    For i = 2 To LRow
        If ThisWorkbook.Sheets("Data").Cells(i, 10).Value = "Salus Hosting" And ThisWorkbook.Sheets("Data").Cells(i, 30).Value = "EC2" Then
            MsgBox ThisWorkbook.Sheets("Data").Cells(i, 10).Value & ThisWorkbook.Sheets("Data").Cells(i, 30)
            ThisWorkbook.Sheets("Sheet1").Cells(i, 1).Value = ThisWorkbook.Sheets("Data").Cells(i, 10).Value
            ThisWorkbook.Sheets("Sheet1").Cells(i, 2).Value = ThisWorkbook.Sheets("Data").Cells(i, 17).Value
            ThisWorkbook.Sheets("Sheet1").Cells(i, 3).Value = ThisWorkbook.Sheets("Data").Cells(i, 20).Value
            ThisWorkbook.Sheets("Sheet1").Cells(i, 4).Value = ThisWorkbook.Sheets("Data").Cells(i, 21).Value
            ThisWorkbook.Sheets("Sheet1").Cells(i, 5).Value = ThisWorkbook.Sheets("Data").Cells(i, 29).Value
        End If
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

假设要将值写入正在添加的工作表,以下更改可能有所帮助。

Sub SPD()
Dim Ws as Worksheet     ''(changed)
Dim Col1, col2, col3, i, j, LRow, LCol As Long
Dim st1, st2, st3 As String
i = 0
j = 0
Set Ws = ThisWorkbook.Sheets.Add      ''(changed)
st1 = "Salus Hosting"
LRow = Workbooks("Data").Sheets("Data").Cells(Rows.Count, 2).End(xlUp).Row

For i = 2 To LRow
    If ThisWorkbook.Sheets("Data").Cells(i, 10).Value = "Salus Hosting" And ThisWorkbook.Sheets("Data").Cells(i, 30).Value = "EC2" Then
        MsgBox ThisWorkbook.Sheets("Data").Cells(i, 10).Value & ThisWorkbook.Sheets("Data").Cells(i, 30)
        Ws.Cells(i, 1).Value = ThisWorkbook.Sheets("Data").Cells(i, 10).Value   ''(changed)
        Ws.Cells(i, 2).Value = ThisWorkbook.Sheets("Data").Cells(i, 17).Value   ''(changed)
        Ws.Cells(i, 3).Value = ThisWorkbook.Sheets("Data").Cells(i, 20).Value   ''(changed)
        Ws.Cells(i, 4).Value = ThisWorkbook.Sheets("Data").Cells(i, 21).Value   ''(changed)
        Ws.Cells(i, 5).Value = ThisWorkbook.Sheets("Data").Cells(i, 29).Value   ''(changed)
    End If
Next i
End Sub