Writing a key with VBA code inserts an Excel slash header

Source: Internet
Author: User
Tags exit

Sometimes, we want to add a slash header in Excel, but Excel does not provide the function of making slash head, each time to draw a slash manually, very troublesome. Is there a solution? In fact, we can use VBA code can write a function, a key insert Excel slash header.

With the command button control in the Control Toolbox, draw a command button on the worksheet, change the "Caption" property to "one-click Insert Slash Header", and double-click the button to write code in the "click" Event, where the key code is as follows.

Key code:

' Get row and column headings

Dim Icol, IRow as String

Icol=inputbox ("Please enter row header for slash cell", row heading)

Irow=inputbox ("Please enter column headings for slash cells", column headings)

Selection.value = Icol + "" + IRow

' Judge if the string is in line with the rules

If Len (Icol) = 0 or len (irow) = 0 Then

MsgBox ("The title entered is empty!")

Exit Sub

End If

' Determines whether a selection has only one cell

If selection.count <> 1 Then

MsgBox ("Please select a cell and do this again!")

Exit Sub

End If

' Set the slash on top left to right

With Selection.borders (Xldiagonaldown)

. LineStyle = xlcontinuous

. Weight = Xlthin

. ColorIndex = xlautomatic

End With

Once the code is written, when you need to insert a slash header, select the cell that you want to insert a slash header in (if the selected cell is more than one, the system will give the error prompt), click the "one-click Insert Slash Header" button, you will be prompted to enter the header row, column headings, respectively, enter the header row header (for example, week) and column headings (for example, classes). Very convenient.

Computer Tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.