gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:39:33 29 4
gpt4 key购买 nike

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

这是代码,它专门与 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 解决方案,请在 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

关于vba - 我的 VBA IsNumeric 函数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34075947/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com