也许您根本不需要 VBA。对于非 vba 解决方案,在 Sheet2 单元格 B1 中输入此公式,然后根据需要向下拖动尽可能多的行(在 Sheet1 列 F 中)。
=IF(AND(NOT(ISNUMBER(Sheet1!F1)),Sheet1!F1=""),Sheet1!F1,"")
对于 VBA 解决方案,我对您的代码进行了一些清理,以发现许多已关闭的语法错误。另外,请注意以下事项:
- 始终
Option Explicit
在您的模块中使用并声明所有变量类型
- 始终使用变量限定对象
(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