如何从单元格中读取十进制数字并复制具有该单元格编号的所有行 - EXCEL

How to read decimal number from cell and copy all rows that have that cell number - EXCEL

我正在尝试从一个单元格中读取一个数字(这个数字发生变化,所以我已经引用了该单元格),然后将它复制到一个新的 sheet。读取或复制没有问题。我遇到的问题是从另一个单元格读取的数字不适用于十进制数。我可以读取包含所有整数的单元格并复制它们各自的行,但不能复制小数。我尝试了一种解决方案,将数字四舍五入为整数,但后来发现这无法满足我的解决方案所需的准确性。

代码片段如下:

With ws.Result.Range ("A1:F" & .Cells(.Rows.Count, "F").End(xlUp).Row
.AutoFilter Field := 6, Criteria1 := wsResult.Range("J1") ' checks row F to see 
whether the number in cell J1 matches any in row F

If Application.WorksheetFunction.Subtotal(103, .Columns(1)>1 Then .
Offset(1). Resize (.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy 
Destination:= Main.Range("A22") ' Paste all rows starting at A22 on the main page.

我知道其余代码按计划工作,纯粹只是能够获得 是十进制工作。

如有任何帮助,我们将不胜感激。

要解决任何十进制格式问题,您可以尝试以下代码(注释中的解释):

With wsResult ' be sure 'wsResult' is properly set to a valid 'WorkSheet' object before reaching this line
    With .Range("A1:F" & .Cells(.Rows.Count, "F").End(xlUp).Row) 'reference its columns A:F cells from row 1 (header) down to last not empty one in column "F"
        .AutoFilter field:=6, Criteria1:=">=" & Round(.Range("J1").Value2, 3) - 0.0001, Operator:=xlAnd, Criteria2:="<=" & Round(.Range("J1").Value2, 3) + 0.0001 ' filter referenced cells on 6th column with content in a range of 0.0001 from referenced range "J1" cell content rounded to three decimals
        If Application.WorksheetFunction.Subtotal(103, .Columns(1)) > 1 Then .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=Main.Range("A22") ' if any filtered cell other than the header then copy and paste them to 'Main' worksheet starting from its A22 cell
    End With
    .AutoFilterMode = False
End With

如果您的实际单元格内容小数点分隔符是一个点 (.),那么请务必将从 "J1" 单元格内容中读取的逗号 (,) 小数点分隔符替换为通过更改一个点:

        .AutoFilter field:=6, Criteria1:=">=" & Round(.Range("J1").Value2, 3) - 0.0001, Operator:=xlAnd, Criteria2:="<=" & Round(.Range("J1").Value2, 3) + 0.0001 ' filter referenced cells on 6th column with content in a range of 0.0001 from referenced range "J1" cell content rounded to three decimals

        .AutoFilter field:=6, Criteria1:=">=" & Replace(Round(.Range("J1").Value2, 3) - 0.0001, ",", "."), Operator:=xlAnd, Criteria2:="<=" & Replace(Round(.Range("J1").Value2, 3) + 0.0001, ",", ".") ' filter referenced cells on 6th column with content in a range of 0.0001 from referenced range "J1" cell content rounded to three decimals

作为旁注,请注意 .Range("J1") 指向引用范围的第一行和第 10 列中的单元格。 在这种情况下,您的引用范围从引用的 wsResult 工作表的单元格 "A1" 开始,因此相当于 wsResult.Range("J1").