2

我找不到这段代码有什么问题,每次我尝试将其更改为我认为会更好的东西时,它都会显示为错误。非常感谢您的帮助!

这是代码,它专门用于使用 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
4

1 回答 1

3

也许您根本不需要 VBA。对于非 vba 解决方案,在 Sheet2 单元格 B1 中输入此公式,然后根据需要向下拖动尽可能多的行(在 Sheet1 列 F 中)。

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

对于 VBA 解决方案,我对您的代码进行了一些清理,以发现许多已关闭的语法错误。另外,请注意以下事项:

  1. 始终Option Explicit在您的模块中使用并声明所有变量类型
  2. 始终使用变量限定对象

(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
于 2015-12-03T21:04:31.403 回答