使用UserForm中的数据将值从一个表格复制到另一个表格

我有一个用户窗体,其中包含您可以填写的以下值:

TextBoxLopnummer.Value TextBoxFragestallare.Value TextBoxMottagare.Value TextBoxDatum.Value 

图片:

在这里输入图像说明

当有人填写date值: TextBoxDatum.Value我想要在整个工作簿中search这个值,并粘贴该单元格所在的整行,在“单击”单元格A15中粘贴。 请注意,该值可以在工作簿中的不同工作表中显示,并在同一工作表内出现多次。 所以在单元格A15和下面可以有很多行。

我已经开始实施这一点,但我真的不知道如何完成它:

 'in the rows below I wanna write so that ".Value=copies the value from the sheets where it finds eg. the date". emptyRow = WorksheetFunction.CountA(ws.Range("A:A")) + 14 Cells(emptyRow, 1).Value = Cells(emptyRow, 2).Value = Cells(emptyRow, 3).Value = Cells(emptyRow, 4).Value = Cells(emptyRow, 5).Value = Cells(emptyRow, 6).Value = Cells(emptyRow, 7).Value = Cells(emptyRow, 8).Value = 

请注意,您可以同时search更多的date,有4个标准,你可以search,看到上面的图片。 当您填写2个标准时,代码应该将这两个与整个工作簿中具有相同标准的行匹配并复制该行等。

此外,TextBoxLopnummer将始终在单元格A2中,在其中正在search的表单中向下。单元格B2中的TextBoxFragestallare,单元格C2中的TextBoxMottagare,单元格D2中的TextBoxDatum。

我怎样才能继续解决我的问题?

这将使你获得你想要做的大部分事情。 基于你原来的问题中的意见,我相信这是你所需要的。

处理:

  • 在UserForm代码上具有searchbutton的单击事件。 在这个例子中,它是Button1。 根据自己的需要命名。

  • 每次运行前清除目标表单(每个请求)

  • 根据每个值的索引与要search的列号相匹配的文本框值设置一个数组

  • 遍历每个工作表,除了目标工作表之外。

  • 一次一行,将适当列的值与匹配它的数组索引进行比较。

  • 如果find匹配,则“匹配”variables设置为true

  • 循环访问数组中剩余的TextBoxes值,如果它们中的任何一个不匹配,则将“match”variables设置为false,并将该文本框上的循环分解为失败。

  • 如果通过search工作表的行的循环结束时“匹配”为真,则列1到8将循环,将search的表中的值设置为目标表。

  • 下一行完成循环

  • 下一个工作表完成循环

可能的问题检查:

  • 您可能必须进行一些date转换,但是如果工作表上的date格式与用户表单上的date格式相同,则应该起作用。

  • 如果工作表中的文本具有0.0或更改的小数位数,则数字可能会提供类似的问题。

  • 如果发生这样的问题,只需使用你的本地窗口,并通过你的代码来看它执行。 你会收到类似的错误可能是types不匹配。 通过使用Locals窗口进行debugging,您将知道需要格式化哪些特定值,以便将其与文本框进行比较。 如果经过太久,请打个折点。

未经testing:评论有问题。

 Private Sub button1_click() Dim ws As Worksheet Dim lastRow As Long, lRow As Long, tRow As Long Dim tempValue As String Dim targetSheet As String Dim tempList(1 To 4) As String Dim i As Long Dim match As Boolean match = False 'Set TargetSheet and clear the previous contents targetSheet = "Lägg in Ärende" tRow = 15 lastRow = Sheets(targetSheet).Range("A" & Rows.count).End(xlUp).row Sheets(targetSheet).Range("A15:H" & lastRow).ClearContents 'Set an array of strings, based on the index matching the column to search for each tempList(1) = TextBoxLopnummer.Text 'Column "A" (1) tempList(2) = TextBoxFragestallare.Text 'Column "B" (2) tempList(3) = TextBoxMottagare.Text 'Column "C" (3) tempList(4) = TextBoxDatum.Text 'Column "D" (4) 'Search through each worksheet For Each ws In Worksheets If ws.name <> targetSheet Then 'Get last row of sheet lastRow = ws.Range("A" & Rows.count).End(xlUp).row 'Search through the sheet For lRow = 2 To lastRow 'Using the array of values from the TextBoxes, 'Each column number matches the index of the array. 'Only testing the array values that have text in them, 'If any don't match the loop is broken and returns to main search. For i = 1 To 4 If tempList(i) <> "" Then If ws.Cells(lRow, i).Text = tempList(i) Then match = True Else match = False Exit For 'If any of the values is false, exit i loop End If End If Next i 'If there was a match, copy the data from Searched ws to targetSheet If match = True Then 'Get the first Empty row on target sheet For lCol = 1 To 8 Sheets(targetSheet).Cells(tRow, lCol).Value = ws.Cells(lRow, lCol).Value Next lCol tRow = tRow + 1 End If Next lRow End If Next ws End Sub