One of the common Code for VBA operations Excel Classic

Source: Internet
Author: User
Tags trim

Mycolumn=target.column Gets the column number of the active cell

Myrow=target.row gets the line number of the active cell

The name of the worksheet. UsedRange.Rows.Count Specify the total number of rows used by the worksheet

The name of the worksheet. UsedRange.Columns.Count Specify the total number of columns used by the worksheet

The name of the worksheet. Cells (line number, column number). Value gets the values of a cell on the specified worksheet

Worksheets (m). Cells (i, J). Value uses a common ordinal method to call the data of a cell in a worksheet

For I=1 to Worksheets.count cycle each worksheet in the current workbook

Here's a comprehensive use of code like this:

If (target.column = 1) Then

For j = 1 to Sheet2.UsedRange.Rows.Count

If (Trim (Sheet1.cells (Target.Row, 1). Value) = Trim (Sheet2.cells (J, 1). Value)) Then

Sheet1.cells (Target.Row, 2). Value = Sheet2.cells (j, 2). Value

End If

Next J

End If

The code below is a function that calls excel in a VBA environment.

Worksheets (1). Range (Cells (6, 1), Cells (6, 1)) = Application.WorksheetFunction.Sum (Range (Cells (1, 1), Cells (4, 2))

Worksheets (1). Range (Cells (6, 2), Cells (6, 2)) = Application.WorksheetFunction.Average (Worksheets (1). Range (Cells (1, 1), Cells (4, 2)) ' The best way to invoke the

Worksheets (1). Range ("C6") = Application.max (Worksheets ("Sheet1"). Range ("A1:b4"))

Worksheets (1). Range ("D6") = Application.min (Range ("A1:B4"))

Worksheets ("Sheet1"). Range ("E6") = Worksheetfunction.median (Range ("A1:B4"))

Here's another special code:

Excel checks that the user presses F1 or F3 to automate the code for a process

Application.onkey key:= "{F1}", procedure:= "MYAUTOINPUT1"

Application.onkey key:= "{F3}", procedure:= "MyAutoInput2"

Here's the complete code:

Private Sub Workbook_Open ()

Application.onkey key:= "{F1}", procedure:= "MYAUTOINPUT1"

Application.onkey key:= "{F3}", procedure:= "MyAutoInput2"

End Sub

Private Sub Workbook_sheetselectionchange (ByVal Sh as Object, ByVal Target as Range)

Myrow = Target.Row

End Sub

Public Myrow as Integer

Sub MYAUTOINPUT1 ()

ActiveSheet.Cells (Myrow, 4). Value = 200

End Sub

Sub MyAutoInput2 ()

ActiveSheet.Cells (Myrow, 4). Value = 300

End Sub

Finally, put up a set of code to return the path to the Excel file, and note that the path returned is not the same:

' MsgBox Application.path

ActiveSheet.Cells (1, 1). Value = Application.path

' MsgBox Thisworkbook.path

ActiveSheet.Cells (2, 1). Value = Thisworkbook.path

' MsgBox Application.defaultfilepath

ActiveSheet.Cells (3, 1). Value = Application.defaultfilepath

' MsgBox Application.ActiveWorkbook.Path

ActiveSheet.Cells (4, 1). Value = Application.ActiveWorkbook.Path

' MsgBox Application.ActiveWorkbook.FullName

ActiveSheet.Cells (5, 1). Value = Application.ActiveWorkbook.FullName

' MsgBox Application.ActiveWorkbook.Name

ActiveSheet.Cells (6, 1). Value = Application.ActiveWorkbook.Name

The code is classic enough to be used by learners who are adept at using intelligent generic code. Later, there is time to have the opportunity to post more code for everyone.

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.