繁体   English   中英

VBA使用用户窗体按钮更改工作表上的选定行

[英]Vba change selected rows on worksheet with a userform button

我制作了一个用户表单,其中包含一天中收到的电话。 单击按钮时,需要将所选行的状态更改为在工作表和列表框上完成。 我设法更改了列表框,但没有更改工作表上的sourcerow。

你能帮助我吗?

Private Sub CommandButton2_Click()

Dim lItem As Long
Dim row As Integer
Dim col As Integer

col = 5

    For lItem = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(lItem) Then

                With ListBox1
                    row = .ListIndex
                    .List(row, col) = "Finished"

                End With

        End If
    Next lItem

End Sub


Private Sub showdata_Click()

ListBox1.RowSource = ""
Dim j As Byte



For i = 2 To Application.WorksheetFunction.CountA(ActiveSheet.Range("A:A"))

If ActiveSheet.Cells(i, "E").Value = comselectnaam.Value And 
(ActiveSheet.Cells(i, "F").Value = "Not started" Or ActiveSheet.Cells(i, 
 "F").Value = "In progress") Then

        With ListBox1
            .AddItem
            .List(j, 0) = Cells(i, 1)
            .List(j, 1) = Cells(i, 2)
            .List(j, 2) = Cells(i, 3)
            .List(j, 3) = Cells(i, 4)
            .List(j, 4) = Cells(i, 5)
            .List(j, 5) = Cells(i, 6)
            j = j + 1
        End With

   End If
 Next

 End Sub

由于您加载的列表等于该行,因此没有偏移量吗? 同样,我看不到j仅是增量。 为什么是ja字节? 我从2开始,您应该真正找到lastrow逻辑,而不是我认为您在这里所做的。 j和i之间有关系吗,我有代码将列表加载为与用来引用电子表格行的整数i相同的代码,这里是一个4列列表框,该列表框引用了一个名为电子表格的电子表格的所有人,所有引用到col和row是双打(如LastRow和i)

'Lat row logic only shown here for context, here using col one as reference
LastRow = DATAsheet.Cells(DATAsheet.Rows.Count, 1).End(xlUp).Row ' count rows based on ID columns

With Me.ListBox2
    .Clear 'clear to set up the list
    .ColumnCount = 4
    .ColumnWidths = "60;70;65;150"
    For i = 0 To LastRow - 2 'data starts in Row2 i is now out of sync by 2 with spreadsheet rows account for this below
        If LastRow >= 2 Then 'data starts in row two
            .AddItem
            .List(i, 0) = DATAsheet.Cells(i + 2, 1) 'Column of the ID, data starts in row 2
            .List(i, 1) = DATAsheet.Cells(i + 2, 4) 'Column of the Date, data starts in row 2
            .List(i, 2) = DATAsheet.Cells(i + 2, 2) ' Column of motor Size, data starts in row 2
            .List(i, 3) = DATAsheet.Cells(i + 2, 3) ' Column of motor SN, data starts in row 2
        Else ' Do nothing
        End If
    Next i
End With

由于您现在上面的代码的行偏移列表为2,因此我将在您的代码中按如下方式使用它(您可能有所不同,j和i之间的关系是什么?)

For lItem = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(lItem) Then 'a list item is selected
                    'offset that list item row number by spreadsheet row reference, you know this from building the list
                    selRow = lItem + 2 'selRow avoid anything that looks reserved like row
'you are changing the spreadsheet value, not the list column value, if you are you need a two point index to change the list item
                    ActiveSheet.Cells(selRow, "F").Value = "Finished"

        End If
 Next lItem

您是否要更改工作表上的报告状态,还是要更改列表框中的报告状态? 如果是工作表,则下次您以这种方式加载它时,列表框将更改。

我还强烈建议您不要使用活动工作表,除非您在工作簿中只有一个工作表,并且一次只能打开一个工作簿。 如果没有,您会遇到麻烦。 这个怎么做? 您可以将所有Dims放在模块的顶部,并且根据容器的不同,它们将仅作用于该模块,表单或工作表。 如果找到,则在私有订阅中声明多次。

'top of module
Option Explicit
Dim statusWS as Worksheet

'In your code or set a function to intitialize this modules variables when you have a lot of them
Set statusWS = Thisworkbook.Worksheets("yourworksheetname")
'replace all of your activesheet with statusWS

我希望这有帮助。 干杯,新秀

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM