如果不是空,则在工作表中循环单元格并将值复制到另一个工作表

时间:2018-04-22 22:16:45

标签: excel vba excel-vba

我有2张CustomerSupportStandatdMDForm

我正试图在CustomerSupport

中循环遍历单元格I5到I84

如果单元格不为空(包含任何名称)

从此行逐个单元格复制到StandardMDForm工作表

中的特定单元格目标

然后打印出StandardMDForm

这是我的代码

Sub copyAndPrintMDs()

Dim i As Long

    'Loop through Rows from 5 to 84
    For i = 5 To 84

        'Check if Doctor Name is Available
        If Not IsEmpty(Worksheets("CustomerSupport").Cells(i, "I").Value) Then

        'Doctor Details
        Worksheets("StandardMDForm").Range("B4").Value = Worksheets("CustomerSupport").Range(i, "I").Value 'Doctor Name
        Worksheets("StandardMDForm").Range("B5").Value = Worksheets("CustomerSupport").Range(i, "G").Value 'Potentiality
        Worksheets("StandardMDForm").Range("B6").Value = Worksheets("CustomerSupport").Range(i, "H").Value 'Speciality
        Worksheets("StandardMDForm").Range("B7").Value = Worksheets("CustomerSupport").Range(i, "E").Value 'City/Area
        Worksheets("StandardMDForm").Range("B9").Value = Worksheets("CustomerSupport").Range(i, "J").Value 'Req. Type
        Worksheets("StandardMDForm").Range("E5").Value = Worksheets("CustomerSupport").Range(i, "C").Value 'ID
        Worksheets("StandardMDForm").Range("E6").Value = Worksheets("CustomerSupport").Range(i, "F").Value 'Clinic
        Worksheets("StandardMDForm").Range("E7").Value = Worksheets("CustomerSupport").Range(i, "D").Value 'Rel. Pharmacy
        Worksheets("StandardMDForm").Range("B10").Value = Worksheets("CustomerSupport").Range(i, "K").Value 'Req. Details

        'Brand 1
        Worksheets("StandardMDForm").Range("B14").Value = Worksheets("CustomerSupport").Range(i, "L").Value 'Brand Name
        Worksheets("StandardMDForm").Range("B15").Value = Worksheets("CustomerSupport").Range(i, "M").Value 'Sales
        Worksheets("StandardMDForm").Range("B18").Value = Worksheets("CustomerSupport").Range(i, "N").Value 'Value

        'Brand 2
        Worksheets("StandardMDForm").Range("C14").Value = Worksheets("CustomerSupport").Range(i, "O").Value
        Worksheets("StandardMDForm").Range("C15").Value = Worksheets("CustomerSupport").Range(i, "P").Value
        Worksheets("StandardMDForm").Range("C18").Value = Worksheets("CustomerSupport").Range(i, "Q").Value

        'Brand 3
        Worksheets("StandardMDForm").Range("D14").Value = Worksheets("CustomerSupport").Range(i, "R").Value
        Worksheets("StandardMDForm").Range("D15").Value = Worksheets("CustomerSupport").Range(i, "S").Value
        Worksheets("StandardMDForm").Range("D18").Value = Worksheets("CustomerSupport").Range(i, "T").Value

        'Brand 4
        Worksheets("StandardMDForm").Range("E14").Value = Worksheets("CustomerSupport").Range(i, "U").Value
        Worksheets("StandardMDForm").Range("E15").Value = Worksheets("CustomerSupport").Range(i, "V").Value
        Worksheets("StandardMDForm").Range("E18").Value = Worksheets("CustomerSupport").Range(i, "W").Value

        'Brand 5
        Worksheets("StandardMDForm").Range("F14").Value = Worksheets("CustomerSupport").Range(i, "X").Value
        Worksheets("StandardMDForm").Range("F15").Value = Worksheets("CustomerSupport").Range(i, "Y").Value
        Worksheets("StandardMDForm").Range("F18").Value = Worksheets("CustomerSupport").Range(i, "Z").Value

        'Print MD Sheet
        Sheets("StandardMDForm").PrintOut

        End If

    Next

End Sub

它的错误是什么?

1 个答案:

答案 0 :(得分:0)

您在.Range定义中使用.Cells( row,column )语法。

更改全部,

while(cursor.moveToNext()) {
    // create newObject
    questionList.add(question);
}
cursor.close(); //remember to close the cursor
return questionList;

...到,

while(cursor.moveToNext()) {
    // create newObject
    questionList.add(question);
}
cursor.close(); //remember to close the cursor
return questionList;