Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Excel:按行内容对列进行排序_Vba_Excel_Sorting - Fatal编程技术网

Vba Excel:按行内容对列进行排序

Vba Excel:按行内容对列进行排序,vba,excel,sorting,Vba,Excel,Sorting,这很难描述 我有一些专栏,比如三个: 10 20 20 20 22 24 24 24 26 我想得到的是: 10 XX XX 20 20 20 XX 22 XX 24 24 24 XX XX 26 其中XX是一个空单元格 有办法得到这个吗 再见, 托马斯你可以用ADO做很多事情 Dim cn As Object Dim rs As Object Dim strFile As String Dim strCon As String Dim strSQL As String Di

这很难描述

我有一些专栏,比如三个:

10 20 20 20 22 24 24 24 26 我想得到的是:

10 XX XX 20 20 20 XX 22 XX 24 24 24 XX XX 26 其中XX是一个空单元格

有办法得到这个吗

再见,
托马斯

你可以用ADO做很多事情

Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer

''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"

''Late binding, so no reference is needed

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

strSQL = "SELECT 1 As Col, F1 As Cont FROM [Sheet1$] " _
       & "UNION ALL SELECT 2 As Col, F2 As Cont FROM [Sheet1$] " _
       & "UNION ALL SELECT 3 As Col, F3 As Cont FROM [Sheet1$] " _
       & "ORDER BY Cont"

rs.Open strSQL, cn, 3, 3

''Pick a suitable empty worksheet for the results

With Worksheets("Sheet2")

    ''Working with the recordset ...
    Do While Not rs.EOF
        If rs("Cont") > j Then i = i + 1

        j = rs("Cont")

        .Cells(i, rs("Col")) = rs("Cont")

       rs.MoveNext
    Loop
End With

你可以用ADO做很多事情

Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer

''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"

''Late binding, so no reference is needed

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

strSQL = "SELECT 1 As Col, F1 As Cont FROM [Sheet1$] " _
       & "UNION ALL SELECT 2 As Col, F2 As Cont FROM [Sheet1$] " _
       & "UNION ALL SELECT 3 As Col, F3 As Cont FROM [Sheet1$] " _
       & "ORDER BY Cont"

rs.Open strSQL, cn, 3, 3

''Pick a suitable empty worksheet for the results

With Worksheets("Sheet2")

    ''Working with the recordset ...
    Do While Not rs.EOF
        If rs("Cont") > j Then i = i + 1

        j = rs("Cont")

        .Cells(i, rs("Col")) = rs("Cont")

       rs.MoveNext
    Loop
End With

此VBA脚本满足您的需要。您需要添加对脚本运行时的引用(工具->引用)。 只需将脚本指定给按钮或将其另存为宏即可。当您按下它时,它将使用您当前选择的单元格

Private Sub CommandButton2_Click()
Dim dict As New Scripting.Dictionary
ReDim isInColumn(1 To Selection.Columns.Count) As Integer
Dim max As Integer
Dim min As Integer
Dim row As Integer
min = Selection.Cells(1, 1).Value

For Each cell In Selection

If cell.Value < min Then min = cell.Value
If cell.Value > max Then max = cell.Value

If Not dict.Exists(cell.Value) Then
    dict.Add cell.Value, isInColumn
End If
    tempArray = dict(cell.Value)
    tempArray(cell.Column + 1 - Selection.Column) = 1
    dict(cell.Value) = tempArray
Next

For i = min To max
    If dict.Exists(i) Then
        tempArray = dict(i)
        For t = LBound(tempArray) To UBound(tempArray)
            If tempArray(t) = 1 Then
                Selection.Cells(1, 1).Offset(row, t - 1) = i
            Else
                Selection.Cells(1, 1).Offset(row, t - 1) = "xx"
            End If
        Next t

        row = row + 1

    End If
Next i

End Sub
Private子命令按钮2\u单击()
Dim dict作为新的脚本编写字典
ReDim isInColumn(1到Selection.Columns.Count)为整数
将最大值调整为整数
将最小值设置为整数
将行设置为整数
最小值=选择。单元格(1,1)。值
对于选择中的每个单元格
如果cell.Valuemax,则max=cell.Value
如果不存在dict.Exists(cell.Value),则
dict.Add cell.Value,isInColumn
如果结束
tempArray=dict(cell.Value)
tempArray(cell.Column+1-Selection.Column)=1
dict(单元格值)=临时数组
下一个
对于i=从最小到最大
如果dict.存在(i),则
tempArray=dict(i)
对于t=LBound(tempArray)到UBound(tempArray)
如果tempArray(t)=1,则
选择。单元格(1,1)。偏移量(行,t-1)=i
其他的
选择。单元格(1,1)。偏移量(行,t-1)=“xx”
如果结束
下一个t
行=行+1
如果结束
接下来我
端接头

此VBA脚本满足您的需要。您需要添加对脚本运行时的引用(工具->引用)。 只需将脚本指定给按钮或将其另存为宏即可。当您按下它时,它将使用您当前选择的单元格

Private Sub CommandButton2_Click()
Dim dict As New Scripting.Dictionary
ReDim isInColumn(1 To Selection.Columns.Count) As Integer
Dim max As Integer
Dim min As Integer
Dim row As Integer
min = Selection.Cells(1, 1).Value

For Each cell In Selection

If cell.Value < min Then min = cell.Value
If cell.Value > max Then max = cell.Value

If Not dict.Exists(cell.Value) Then
    dict.Add cell.Value, isInColumn
End If
    tempArray = dict(cell.Value)
    tempArray(cell.Column + 1 - Selection.Column) = 1
    dict(cell.Value) = tempArray
Next

For i = min To max
    If dict.Exists(i) Then
        tempArray = dict(i)
        For t = LBound(tempArray) To UBound(tempArray)
            If tempArray(t) = 1 Then
                Selection.Cells(1, 1).Offset(row, t - 1) = i
            Else
                Selection.Cells(1, 1).Offset(row, t - 1) = "xx"
            End If
        Next t

        row = row + 1

    End If
Next i

End Sub
Private子命令按钮2\u单击()
Dim dict作为新的脚本编写字典
ReDim isInColumn(1到Selection.Columns.Count)为整数
将最大值调整为整数
将最小值设置为整数
将行设置为整数
最小值=选择。单元格(1,1)。值
对于选择中的每个单元格
如果cell.Valuemax,则max=cell.Value
如果不存在dict.Exists(cell.Value),则
dict.Add cell.Value,isInColumn
如果结束
tempArray=dict(cell.Value)
tempArray(cell.Column+1-Selection.Column)=1
dict(单元格值)=临时数组
下一个
对于i=从最小到最大
如果dict.存在(i),则
tempArray=dict(i)
对于t=LBound(tempArray)到UBound(tempArray)
如果tempArray(t)=1,则
选择。单元格(1,1)。偏移量(行,t-1)=i
其他的
选择。单元格(1,1)。偏移量(行,t-1)=“xx”
如果结束
下一个t
行=行+1
如果结束
接下来我
端接头

为什么第二列是22,第三列是26?可以用Visual Basic或公式来完成。前者更容易,但您可能会遇到安全问题(例如,如果您随后将启用宏的Excel文件发送给关闭宏的人),后者将非常混乱,但可以移植并自动更新。您应该澄清您想要的是什么。为什么第二列是22,第三列是26?可以用Visual Basic或公式来完成。前者更容易,但您可能会遇到安全问题(例如,如果您随后将启用宏的Excel文件发送给关闭宏的人),后者将非常混乱,但可以移植并自动更新。你应该澄清你想要什么。这是真的,这是怜悯和怜悯。这是真的,这是怜悯和怜悯。这是真的。