My Excel contains three columns of data

王林
Release: 2024-01-23 09:06:07
forward
743 people have browsed it

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!

Related labels:
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!