代码之家  ›  专栏  ›  技术社区  ›  Sevpoint

Excel VBA:获取另一列中的数据列表

  •  0
  • Sevpoint  · 技术社区  · 7 年前

    我目前正在创建一个自动化系统来自动化一些任务。基本上,我有以下数据:

    enter image description here

    我的目标是把所有的帐户转移到另一张纸上(第2张)。

    问题:我似乎不能显示银行名称和他们下面的帐号。因为银行名称总是空的。

    银行和帐号可以增长,在这种情况下,我希望它是动态的。但是,当我试图在上一家银行添加一个帐号时,它停止粘贴额外的帐号。如果代码也可以改进?

    总而言之,我想得到一家银行的账号列表。在得到它之后,我将在它循环到另一个银行和帐号之前做一些其他的任务。但我还没有包括在下面的代码中:

    Sub test1()
    
    Dim lRow As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Range("B2").Select
    
    For i = 2 To lRow
    
        ActiveSheet.Cells(i, 2).Select
    
        If ActiveCell.Offset(1, -1).Value = "" Then
    
                ActiveCell.Copy
                Sheets("Sheet2").Select
                ActiveSheet.Paste
                ActiveCell.Offset(1.1).Select
                Sheets("Sheet1").Select
    
    
        Else
            ActiveCell.Copy
            Sheets("Sheet2").Select
            ActiveSheet.Paste
            ActiveCell.Offset(1.1).Select
            Sheets("Sheet1").Select
    
            'I need to to insert other steps here
            MsgBox "New Bank. Need to do other steps"
    
        End If
    
    Next i
    
    End Sub
    

    期望结果:

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  1
  •   shrivallabha.redij    7 年前

    Public Sub CopyToSecondSheet()
        Dim wksSource As Worksheet: Set wksSource = ThisWorkbook.Sheets("Sheet1")
        Dim wksDestin As Worksheet: Set wksDestin = ThisWorkbook.Sheets("Sheet2")
        Dim i As Long
        Dim strBankName as String
        Application.ScreenUpdating = False
        wksDestin.Range("A1:A" & wksDestin.Range("A" & wksDestin.Rows.Count).End(xlUp).Row).Delete xlUp
        For i = 2 To wksSource.Range("B" & wksSource.Rows.Count).End(xlUp).Row
            If Len(wksSource.Range("A" & i).Value) > 0 Then
                If Len(strBankName) > 0 Then Msgbox "Finished copying records for : " & strBankName, vbOKOnly
                strBankName = wksSource.Range("A" & i).Value
                wksSource.Range("A" & i).Copy wksDestin.Range("A" & wksDestin.Rows.Count).End(xlUp).Offset(1, 0)
            End If
            wksSource.Range("B" & i).Copy wksDestin.Range("A" & wksDestin.Rows.Count).End(xlUp).Offset(1, 0)
        Next
        Msgbox "Update completed!", vbInformation
        Application.ScreenUpdating = True
    End Sub
    
    推荐文章