VBA用戶表單。文本框作為單元格的值

[英]VBA Userforms. Textbox as number value to cell


I've been searching for a solution to this for a while and have been unsuccessful in finding. Essentially, I have the user of my document populating fields in a user form that my code then puts into the appropriate cells. One of these input fields is a number but, no matter how I instruct excel to format the cell, it still is text in the end and I can't perform any calculations. I'm sure this is probably a pretty simple fix but I've been unsuccessful so far in finding a solution. As always, any help is appreciated!

我一直在尋找解決這個問題的方法,並且一直沒有成功找到。基本上,我讓我的文檔的用戶在用戶表單中填充字段,然后我的代碼將放入適當的單元格中。其中一個輸入字段是一個數字但是,無論我如何指示excel格式化單元格,它仍然是文本到底,我無法執行任何計算。我相信這可能是一個非常簡單的修復,但到目前為止我找不到解決辦法。一如既往,任何幫助表示贊賞!

thanks!

謝謝!

1 个解决方案

#1


6  

You need to convert the value from text to a numeric type before putting it into a cell.

在將值放入單元格之前,需要將值從文本轉換為數字類型。

Assuming your textbox is called txtNumber and you're outputting to cell Output!A1, the code would be:

假設你的文本框被稱為txtNumber並且你輸出到單元格輸出!A1,代碼將是:

Sheets("Output").Range("A1") = CDbl(txtNumber)

Other conversion functions you might want to use are CLng (convert to Long), CInt (convert to Integer), CDec (convert to decimal, useful for currency values).

您可能想要使用的其他轉換函數是CLng(轉換為Long),CInt(轉換為Integer),CDec(轉換為十進制,對貨幣值有用)。

This code will raise an error if the user types a non-numeric text value into the field. You should validate on the textbox's Change event and disable the OK button if invalid data is inputted.

如果用戶在字段中鍵入非數字文本值,則此代碼將引發錯誤。您應該在文本框的Change事件上進行驗證,如果輸入了無效數據,則禁用OK按鈕。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/03/72517525fb958adb2091fcb46c79e11e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com