我的VBA IsNumeric函数有什么问题?

我无法find这段代码有什么问题,每当我尝试将其改变为我认为会更好的东西时,它就会显示为错误。 非常感谢您的帮助!

这是代码,它专门用于使用isnumeric函数,我在Mac上使用Excel 2016。

Dim ws1 As Worksheet Dim ws2 As Worksheet Set ws1 = Sheets("Sheet1") Set ws2 = Sheets("Sheet2") Set i = 1 Set n = 1 Do While ws1.Cell(i, "F") <> "End" Num1 = ws1.Cell(i, "F") If IsNumeric(Num1.value) <> False And Num1 <> "" Set ws2.Cell(n, "B") = ws1.Cell(i, "F") n = n + 1 End If Next i 

也许你根本不需要VBA。 对于非vba解决scheme,请在Sheet2单元格B1中input此公式,然后向下拖动所需行数(在Sheet1列F中)。

=IF(AND(NOT(ISNUMBER(Sheet1!F1)),Sheet1!F1=""),Sheet1!F1,"")

对于VBA解决scheme,我清理了一下你的代码,以避免很多语法错误。 另外,请注意以下几点:

  1. 在模块中始终使用Option Explicit并声明所有variablestypes
  2. 始终使用variables限定对象

(1和2是最佳实践,但不是必需的,留下的东西可能会产生意想不到的结果)。

 Option Explicit '... Sub Name ... Dim wb as Workbook Dim ws1 As Worksheet Dim ws2 As Worksheet Dim Num1 as Variant Set wb = ThisWorkbook 'or Workbooks("myBook") Set ws1 = wb.Sheets("Sheet1") Set ws2 = wb.Sheets("Sheet2") Dim i as Long, n as Long i = 1 'no need to "Set" numerical integers n = 1 Do While ws1.Cells(i, "F") <> "End" Num1 = ws1.Cells(i, "F").Value2 'set this to the value2 property of the cell If Not IsNumeric(Num1) And Num1 <> "" 'remove .Value from variable ws2.Cells(n, "B").Value = ws1.Cells(i, "F").Value 'set the cells Value property equal to each ... again, Set will not work here n = n + 1 i = i + 1 'need to increment i as well End If Loop 'not Next I, since you are using a Do While Loop