search
HomeSoftware TutorialOffice SoftwareMy Excel contains three columns of data

My Excel contains three columns of data

Jan 23, 2024 am 09:06 AM
vb array sorting problem

My Excel contains three columns of data

I have an Excel with three columns of data. I want to implement sorting function through VB

Sub Sort1()

ActiveSheet.Sort.SortFields.Clear

ActiveSheet.Sort.SortFields.Add Key:=Range("A2:A" & Range("A1").CurrentRegion.Rows.Count) _

, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

ActiveSheet.Sort.SortFields.Add Key:=Range("B2:B" & Range("A1").CurrentRegion.Rows.Count) _

, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

With ActiveSheet.Sort

.SetRange Range("A1:C" & Range("A1").CurrentRegion.Rows.Count)

.Header = xlYes

.MatchCase = False

.Orientation = xlTopToBottom

.SortMethod = xlPinYin

.Apply

End With

End Sub

VB array sorting problem

If you enter a total of 12 numbers from 0 to 11 in column A of the excel table, enter 12 random numbers correspondingly in column B, and then sort by column B, the numbers in column A will become random. Arranged

The same idea is used here, except that the two-dimensional table is replaced by a two-dimensional array. The code is as follows:

Dim Matrix(0 To 11, 0 To 1) As Single

Dim i As Integer, j As Integer

Dim sngTemp As Single

Randomize

'Initialize the array so that the first number of each "row" is the line number, and the second number is a randomly sized number

For i = 0 To 11

Matrix(i, 0) = i

Matrix(i, 1) = Rnd(100)

Next

Text1.Text = """

'Since the array is small, bubble sorting is used here to filter out the Nth largest number and display it in Text1

For i = 0 To 11

For j = i 1 To 11

If Matrix(j, 1) > Matrix(i, 1) Then

sngTemp = Matrix(i, 1)

Matrix(i, 1) = Matrix(j, 1)

Matrix(j, 1) = sngTemp

sngTemp = Matrix(i, 0)

Matrix(i, 0) = Matrix(j, 0)

Matrix(j, 0) = sngTemp

End If

Next

Text1.Text = Text1.Text & vbCrLf & Matrix(i, 0)

Next

EXCEL VBA macro language sorting problem with pictures

Question 1

Private Sub CommandButton1_Click()

Dim i, j As Long

Sheets("sheet2").Cells.ClearContents

Sheets("sheet1").Cells.Copy

Sheets("sheet2").Select

Sheets("sheet2").Range("A1").Select

ActiveSheet.Paste

i = 3

Do While Sheets("sheet2").Range("A" & i).Value """

For j = 2 To i - 1

If Sheets("sheet2").Range("A" & i).Value > Sheets("sheet2").Range("A" & j).Value Then

Else

Sheets("sheet2").Rows(i & ":" & i).Cut

Sheets("sheet2").Rows(j & ":" & j).Insert Shift:=xlDown

Exit For

End If

Next j

i = i 1

Loop

End Sub

Question 2

Private Sub CommandButton1_Click()

Dim i, j As Long

Sheets("sheet2").Cells.ClearContents

Sheets("sheet1").Cells.Copy

Sheets("sheet2").Select

Sheets("sheet2").Range("A1").Select

ActiveSheet.Paste

i = 3

Do While Sheets("sheet2").Range("A" & i).Value """

For j = 2 To i - 1

If Sheets("sheet2").Range("C" & i).Value & Sheets("sheet2").Range("D" & i).Value > Sheets("sheet2").Range(" C" & j).Value & Sheets("sheet2").Range("D" & j).Value Then

Else

Sheets("sheet2").Rows(i & ":" & i).Cut

Sheets("sheet2").Rows(j & ":" & j).Insert Shift:=xlDown

Exit For

End If

Next j

i = i 1

Loop

End Sub

The above is the detailed content of My Excel contains three columns of data. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
How to do a running total in Excel (Cumulative Sum formula)How to do a running total in Excel (Cumulative Sum formula)Apr 21, 2025 am 11:16 AM

This concise guide demonstrates how to efficiently calculate running totals (cumulative sums) in Excel using a SUM formula with strategically placed absolute and relative cell references. A running total displays the accumulated sum of a dataset as

Excel LEN function: count characters in cellExcel LEN function: count characters in cellApr 21, 2025 am 10:10 AM

Are you looking for an Excel formula to count characters in a cell? If so, then you have certainly landed up on the right page. This short tutorial will teach you how you can use the LEN function to count characters in Excel, with or with

How to count words in Excel - formula examplesHow to count words in Excel - formula examplesApr 21, 2025 am 09:59 AM

This tutorial demonstrates how to count words in Excel using a combination of the LEN, SUBSTITUTE, and TRIM functions. It offers both case-sensitive and case-insensitive formulas for counting total words or specific words/text within a cell or range

Excel: Split string by delimiter or pattern, separate text and numbersExcel: Split string by delimiter or pattern, separate text and numbersApr 21, 2025 am 09:19 AM

This tutorial demonstrates how to efficiently split cells in Excel using both formulas and the advanced "Split Text" feature. Learn to separate text using commas, spaces, or any delimiter, and to split alphanumeric strings into their text

Excel Solver tutorial with step-by-step examplesExcel Solver tutorial with step-by-step examplesApr 21, 2025 am 09:15 AM

This tutorial explains how to locate and utilize the Solver add-in within various Excel versions, ranging from 2003 to 365. Step-by-step instructions and practical examples demonstrate Solver's application in optimizing solutions for linear programm

Excel Format Painter and other ways to copy formattingExcel Format Painter and other ways to copy formattingApr 20, 2025 am 10:29 AM

This tutorial demonstrates three efficient methods for copying formatting in Excel: Format Painter, Fill Handle, and Paste Special. These techniques are compatible with all Excel versions (2007-365). Proper formatting enhances worksheet presentation

Clear formatting in Excel: how to remove all formats in a cellClear formatting in Excel: how to remove all formats in a cellApr 20, 2025 am 10:12 AM

This tutorial offers two efficient methods for removing formatting from Excel spreadsheets. Dealing with extensive formatting in large spreadsheets can be cumbersome. These techniques streamline the process. Manually removing formatting is ineffici

Excel table: comprehensive tutorial with examplesExcel table: comprehensive tutorial with examplesApr 20, 2025 am 09:48 AM

This tutorial unveils the power of Excel tables, often overlooked yet incredibly efficient tools for data management. Discover how tables simplify tasks like dynamic named ranges, formula updates, and data formatting, saving you significant time and

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment