也許你根本不需要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
從'Num1'除非你* explicitly'設置NUM1通過'昏暗NUM1一個Range對象範圍設定NUM1 = ws1.Cells(我, 「F」)' –
你不」刪除'.value'不需要設置變量。你不用'next'來關閉'while',但是用'loop'關閉。你不增加'我'。你不需要'<> false' ...編輯:哦,'next i'不是你如何遞增,而是'i = i + 1'你怎麼處理'n'。 – findwindow