VB code to realize the method of controlling EXCEL text formatting

WBOY
Release: 2024-01-23 18:09:13
forward
517 people have browsed it

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!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!