我正在尝试从单元格中读取一个数字(此数字会更改,因此我引用了单元格),然后将其复制到新工作表中。阅读或复制没有问题。我遇到的问题是从另一个单元格读取的数字不适用于十进制数字。我可以读取包含所有整数的单元格并复制它们各自的行但不是小数。我尝试了一种解决方案,将数字四舍五入为整数,但后来发现这对我解决方案所需的准确性不起作用。
下面是一段代码:
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.
我知道其余代码按计划工作,纯粹只是能够获得数字 是十进制的。
非常感谢任何帮助。
答案 0 :(得分:0)
要解决任何小数格式问题,您可以尝试以下代码(注释中的解释):
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")
。