Home >Software Tutorial >Office Software >VB code to realize the method of controlling EXCEL text formatting

VB code to realize the method of controlling EXCEL text formatting

WBOY
WBOYforward
2024-01-23 18:09:13570browse

VB code to realize the method of controlling EXCEL text formatting

How to use VB code to control the format of text when entering text into EXCEL

To access EXCEL from VB, you first need to reference the Microsoft Excel type library in the project:

Select the "Reference" column from the "Project" menu; select Microsoft Excel 11.0 Object Library (EXCEL2003), and then select "OK". Indicates that the EXCEL type library should be referenced in the project.

Then the code to access and set the font is as follows:

Dim xlApp As Excel.Application

Dim xlBook As Excel.WorkBook

Dim xlSheet As Excel.Worksheet

Dim FileName, SheetName As String

FileName = "d:\data.xls" 'excel workbook path and name

SheetName = "sheet1" 'The name of the worksheet that needs to be set

Set xlApp = CreateObject("Excel.Application") 'Create EXCEL object

Set xlBook = xlApp.Workbooks.Open(FileName) 'Open an existing EXCEL workbook file

xlApp.Visible = True 'Set the EXCEL object to be visible (or invisible)

Set xlSheet = xlBook.Worksheets(SheetName) 'Set the active worksheet

With xlSheet.Range("C13:D19").Font 'Select the set area.

.Name = "official script" 'These specific assignments can be set according to your own needs.

.FontStyle = "Bold"

.Size = 16

.Strikethrough = False

.Superscript = False

.Subscript = False

.OutlineFont = False

.Shadow = False

.Underline = xlUnderlineStyleNone

.ColorIndex = xlAutomatic

End With

xlBook.Close (True) 'Close the workbook

xlApp.Quit 'End EXCEL object

Set xlApp = Nothing 'Release xlApp object

VB problem limits that the text box can only input 10 characters and automatically enters every two characters

private sub text1_onchange()

if len(text1.text)>14 '10 characters 4 asterisks then text1.text=left(text1.text,14)

netstring=replace(text1.text,"*","") 'Remove the added asterisk

while len(netstring)>2 then

display=display & "*" & left(netstring,2)

netstring = mid(netstring,3)

wend

display = display & "*" & left(netstring,2)

text1.text=display

end sub

The above is the detailed content of VB code to realize the method of controlling EXCEL text formatting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete