EXCEL搜索数据并连续完成数据

时间:2018-01-15 03:43:02

标签: excel vba excel-vba

大家好!我想问一下,我将这个宏代码用于将数据编码到excel中的另一个工作表

Sub RoundedRectangle1_Click()
Dim i, lastrow As Long
If ActiveSheet.Range("d6") = "" Or ActiveSheet.Range("g6") = "" Or ActiveSheet.Range("c9") = "" Then
    MsgBox "Please complete all fields!"
    Exit Sub
End If

lastrow = Sheets("database").Cells(Cells.Rows.Count, 2).End(xlUp).Row + 1
i = 9
Do While Cells(i, 3) <> "" And i < 29
        Sheets("Database").Cells(lastrow, 2) = ActiveSheet.Range("g6") ' Date
        Sheets("Database").Cells(lastrow, 3) = ActiveSheet.Range("d6") ' Ref
        Sheets("Database").Cells(lastrow, 4) = ActiveSheet.Cells(i, 3) ' Code
        Sheets("Database").Cells(lastrow, 5) = ActiveSheet.Cells(i, 4) ' Description
        Sheets("Database").Cells(lastrow, 6) = ActiveSheet.Cells(i, 5) ' U/M
        Sheets("Database").Cells(lastrow, 7) = ActiveSheet.Cells(i, 6) ' Qty
        Sheets("Database").Cells(lastrow, 8) = ActiveSheet.Cells(i, 7) ' Price
        Sheets("Database").Cells(lastrow, 9) = "IN" ' Transaction
        i = i + 1
        lastrow = lastrow + 1
Loop
MsgBox "Saved Succesfully!"
ThisWorkbook.Save
Call RoundedRectangle2_Click
End Sub

我想要实现的是已经有一个初始数据已经在目标表中编码,宏将只找到它并将填写其各自列的数据

1 个答案:

答案 0 :(得分:0)

继续使用您的代码,您将丢失:

  1. 在每个范围的末尾添加.Value,例如ActiveSheet.Range("g6").Value = ""Sheets("Database").Cells(lastrow, 2).Value = ActiveSheet.Range("g6").Value

  2. 我不明白你为什么要使用i<29我想如果29是lastrow在像j = lastrow之前创建一个新变量然后在while Cells(i, 3) <> "" And i < j中使用try { FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE); OutputStreamWriter outputWriter=new OutputStreamWriter(fileout); outputWriter.write(textmsg.getText().toString()); outputWriter.close(); //display file saved message Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } }

  3. 您的数据是否需要比较ID或字段才能输入正确的数据,或者只是按照字母顺序排列?