查找下一个空行并向其中写入数据

时间:2018-12-02 15:28:39

标签: excel vba excel-vba

我有这个工作代码,用于搜索MyVal变量,如果找不到,它将数据写入下一个空列。但是,我发现组合框不喜欢水平列表,因此我已将所有数据转换为垂直列表。我需要重新编写代码以搜索下一个空行,然后引用该行以将数据写入其中。 你们中的一位专家能否查看我现有的代码,并帮助我将其转换为搜索并写入下一个空行而不是下一个空列?

Sub Copy_To_Borrower_DBase()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim myVal As String
    Dim sourceRng As Range
    Dim lCol As Long
    MyNote = Sheets("Main").Range("F5").Value & " already exists, do you want to 
    overwrite?"
    myVal = Sheets("Main").Range("F5").Value ' dropdown list
    Set sourceRng = Worksheets("Borrower 
    Database").Range("5:5").Find(What:=myVal, LookAt:=xlWhole) 'locate column 
    where to copy from
    Set ws1 = ThisWorkbook.Sheets("Main")
    Set ws2 = ThisWorkbook.Sheets("Borrower Database")

    If Not sourceRng Is Nothing Then
        Answer = MsgBox(MyNote, vbCritical + vbYesNo, "Overwrite??")
        If Answer = vbNo Then
            Exit Sub
        Else
            With ws2
                Application.EnableEvents = False
                .Cells(5, sourceRng.Column).Value = ws1.Range("F5").Value 'Borrower Name
                .Range(.Cells(6, sourceRng.Column), .Cells(8, sourceRng.Column)).Value = 
                ws1.Range("G6:G8").Value 'Income, Credit Pmt and Car Pmt
                .Range(.Cells(9, sourceRng.Column), .Cells(10, sourceRng.Column)).Value 
                = ws1.Range("G11:G12").Value 'Ratios
                .Cells(11, sourceRng.Column).Value = ws1.Range("G15").Value 'Reserves
                .Cells(12, sourceRng.Column).Value = ws1.Range("D15").Value 'Credit 
                Score
                Application.EnableEvents = True
            End With
        End If
    Else
        With ws2
            Application.EnableEvents = False
            lCol = .Cells(5, .Columns.Count).End(xlToLeft).Offset(, 1).Column
            .Cells(5, lCol).Value = ws1.Range("F5").Value 'Borrower Name
            .Range(.Cells(6, lCol), .Cells(8, lCol)).Value = 
            ws1.Range("G6:G8").Value 'Income, Credit Pmt and Car Pmt
            .Range(.Cells(9, lCol), .Cells(10, lCol)).Value = 
            ws1.Range("G11:G12").Value 'Borrower Name
            .Cells(11, lCol).Value = ws1.Range("G15").Value 'Reserves
            Application.EnableEvents = True
        End With
    End If
End Sub

任何帮助将不胜感激:-) enter image description here

0 个答案:

没有答案