เขียน VBA ไปก็งงไป อ่านรหัสสินค้ามา เป็น 0001 แต่พอเอาลง excel มันกลับมองเป็นเลข 1 อย่างเดียว และก็ไม่อยากใส่เครื่องหมาย ' ข้างหน้า เพื่อให้ excel มองว่าเป็น text กว่าจะหาวิธีแก้ได้
ตามนี้เลย:
Sheets("mySheet").Range("A1").NumberFormat="@"
บังคับให้ cell A1 แสดงข้อมูลเป็น text ไม่ใช่ตัวเลข
โพสต์แนะนำ
แสดงบทความที่มีป้ายกำกับ VBA แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ VBA แสดงบทความทั้งหมด
วันพุธที่ 19 กันยายน พ.ศ. 2555
วันศุกร์ที่ 1 มิถุนายน พ.ศ. 2555
ฟังก์ชั่น Excel VBA SaveAs dialog และเขียน TextFile
Sub WriteFile(fname As String, txt As String) 'เขียน Text File
Dim fso As New FileSystemObject
Dim stream As TextStream
Set stream = fso.CreateTextFile(fname, True)
stream.Write (txt)
stream.Close
End Sub
Function SaveDialog() As String 'เปิด Dialog ถามชื่อ File
Dim ret As String
ret = Application.GetSaveAsFilename("Output.xml", _
"XML files (*.xml),*.xml", 1, "Save XML Output")
If ret <> "False" Then
SaveDialog = ret
End If
End Function
ถ้าจะใช้ FileSystemObject ต้อง reference Microsoft Scripting Runtime ด้วย โดยในหน้า VB ไปที่ Tool -> References...
MS Excel: Format Function
MS Excel: Format Function with Strings (VBA only)
In Excel, the Format function takes an expression and returns it as a formatted string.
The syntax for the Format function is:
Format ( expression, [ format ] )
expression is the value to format.
format is optional. It is the format to apply to the expression. You can either define your own format or use one of the named formats that Excel has predefined such as:
Format Explanation General Number Displays a number without thousand separators. Currency Displays thousand separators as well as two decimal places. Fixed Displays at least one digit to the left of the decimal place and two digits to the right of the decimal place. Standard Displays the thousand separators, at least one digit to the left of the decimal place, and two digits to the right of the decimal place. Percent Displays a percent value - that is, a number multiplied by 100 with a percent sign. Displays two digits to the right of the decimal place. Scientific Scientific notation. Yes/No Displays No if the number is 0. Displays Yes if the number is not 0. True/False Displays True if the number is 0. Displays False if the number is not 0. On/Off Displays Off if the number is 0. Displays On is the number is not 0. General Date Displays date based on your system settings Long Date Displays date based on your system's long date setting Medium Date Displays date based on your system's medium date setting Short Date Displays date based on your system's short date setting Long Time Displays time based on your system's long time setting Medium Time Displays time based on your system's medium time setting Short Time Displays time based on your system's short time setting
Applies To:
- Excel 2007, Excel 2003, Excel XP, Excel 2000
For Example:
Format("210.6", "#,##0.00") would return '210.60' Format("210.6", "Standard") would return '210.60' Format("0.981", "Percent") would return '98.10%' Format("1267.5", "Currency") would return '$1,267.50' Format("Sep 3, 2003", "Short Date") would return '9/3/2003'
VBA Code:
The Format function can only be used in VBA code. For example:
Dim LValue As String
LValue = Format("0.981", "Percent")
In this example, the variable called LValue would now contain the value of '98.10%'.
CREDIT: http://www.techonthenet.com
สมัครสมาชิก:
บทความ (Atom)