选择要复制数据的单元格

时间:2017-02-10 18:38:10

标签: excel excel-vba listbox vba

我发现此代码将选中的行从多列多选ListBox复制到我的工作表。如何设置应将第一个选定行复制到的单元格?

我可以更改Excel列但不能更改行。它总是从第一个开始(例如A1或B1)

Dim Litem As Long, LbRows As Long, LbCols As Long
Dim bu As Boolean
Dim Lbloop As Long, Lbcopy As Long
Dim m As Variant

LbRows = ListBox2.ListCount - 1
LbCols = ListBox2.ColumnCount - 1

For Litem = 0 To LbRows
    If ListBox2.Selected(Litem) = True Then
        bu = True
        Exit For
    End If
Next

If bu = True Then
    With ActiveSheet.Range(sCell).End(xlUp)            'sCell is the selected Cell

        For Litem = 0 To LbRows
            If ListBox2.Selected(Litem) = True Then 'Row selected
                'Increment variable for row transfer range
                Lbcopy = Lbcopy + 1
                For Lbloop = 0 To LbCols
                    'Transfer selected row to relevant row of transfer range
                    .Cells(Lbcopy, Lbloop + 1) = ListBox2.List(Litem, Lbloop)

                Next Lbloop
            End If
        Next
        For m = 0 To LbCols
            With ActiveSheet.Range(sCell).End(xlUp)
            End With
        Next
    End With
Else
     MsgBox "Nichts ausgewählt", vbCritical
End If

1 个答案:

答案 0 :(得分:0)

在您的代码中,负责“循环遍历行”的变量是<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60"> <testsuites> <testsuite name="default"> <directory>./tests</directory> <exclude> <directory suffix=".php">./src/Internal</directory> </exclude> </testsuite> </testsuites> <filter> <whitelist> <directory suffix=".php">./src</directory> </whitelist> </filter> <logging> <log type="coverage-html" target="./log/codeCoverage" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/> <log type="testdox-html" target="./log/testdox.html"/> </logging> </phpunit> 。您正在代码的开头正确声明变量,但是第一次使用变量就在这里:

Lbcopy

此时,变量的值为'Increment variable for row transfer range Lbcopy = Lbcopy + 1 ,因为您尚未为其指定任何值 这意味着,当您第一次进入0循环时,您大致会说For

确定起始行的一种方法是在Lbcopy = 0 + 1之前为变量分配初始值,如下所示:

For Litem = 0 To LbRows