2015-12-03 58 views
2

我找不到這段代碼有什麼問題,每當我嘗試將它改爲某種我認爲會更好的工作時,它就會顯示爲錯誤。非常感謝您的幫助!我的VBA IsNumeric函數有什麼問題?

這是代碼,它專門用於使用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 
+0

從'Num1'除非你* explicitly'設置NUM1通過'昏暗NUM1一個Range對象範圍設定NUM1 = ws1.Cells(我, 「F」)' –

+0

你不」刪除'.value'不需要設置變量。你不用'next'來關閉'while',但是用'loop'關閉。你不增加'我'。你不需要'<> false' ...編輯:哦,'next i'不是你如何遞增,而是'i = i + 1'你怎麼處理'n'。 – findwindow

回答

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 
+0

如果你設置了紙張,你不應該限定這本書嗎? – findwindow

+0

@findwindow - 是的,這是*真正*最佳實踐...現在編輯。 –

+0

謝謝@Jeeped!昨晚睡眠不足! –