私は自分の仕事でスプレッドシート用にこのプログラムを作成しました。私のコードはほとんどの時間で動作しますが、
私のコードはほとんど常に動作しますが、何らかの理由でバグを起こすことがあります。 (エラーメッセージは表示されません。ソート時には他の行の情報がコピーされることがありますが、すべて空白にしてください)
私のプログラムは基本的に同じシート内の2つの積み重ねられたテーブルを自動的にソートする。
CODE:
Option Explicit
Sub Sorting()
' Keyboard Shortcut: Ctrl+m
'
'******************************* Define variables for the data that I want to store for later use
Dim MyDataFirstCell
Dim MyDataLastCell
Dim MySortCellStart
Dim MySortCellEnd
Dim MyDataFirstCell2
Dim MyDataLastCell2
Dim MySortCellStart2
Dim MySortCellEnd2
'************************** Establish the Data Area
ActiveSheet.Range("B1").Select
'Next Non Blank Cell down
ActiveCell.Offset(1, 0).Select
Do While IsEmpty(ActiveCell)
DoEvents
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(1, 0).Select
DoEvents
MyDataFirstCell = ActiveCell.Address 'Get the first cell address of Data Area
Selection.End(xlDown).Select 'Get to Bottom Row of the data
Selection.End(xlToRight).Select 'Get to the last Column and data cell by heading to the righthand end
Selection.End(xlToRight).Select
ActiveCell.Offset(-1, 0).Select ' Select the correct last cell
MyDataLastCell = ActiveCell.Address 'Get the Cell address of the last cell of my data area
'************************** Establish the Sort column first and last data points.
ActiveSheet.Range("B1").Select
'Next Non Blank Cell down
ActiveCell.Offset(1, 0).Select
Do While IsEmpty(ActiveCell)
DoEvents
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(1, 0).Select 'Get to first cell of data sort Column (Example Col 'R' Row 2 becuase Row 1 contains the header)
DoEvents
MySortCellStart = ActiveCell.Address 'Get the Cell address of the first cell of my data sort Column
Selection.End(xlDown).Select 'Get to the bottom Row of data
ActiveCell.Offset(-1, 0).Select
MySortCellEnd = ActiveCell.Address 'Get the Cell address of the last cell of my sort Column
'************************** Start the sort by specifying sort area and columns
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add _
Key:=Range(MySortCellStart & ":" & MySortCellEnd), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range(MyDataFirstCell & ":" & MyDataLastCell)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Second sorting
'************************** Establish the Data Area
ActiveSheet.Range("B1").Select
'Next Non Blank Cell down
ActiveCell.Offset(1, 0).Select
Do While IsEmpty(ActiveCell)
DoEvents
ActiveCell.Offset(1, 0).Select
Loop
'Next Blank Cell down
ActiveCell.Offset(1, 0).Select
Do While Not IsEmpty(ActiveCell)
DoEvents
ActiveCell.Offset(1, 0).Select
Loop
'Next Non Blank Cell down
ActiveCell.Offset(1, 0).Select
Do While IsEmpty(ActiveCell)
DoEvents
ActiveCell.Offset(1, 0).Select
Loop
DoEvents
ActiveCell.Offset(1, 0).Select
MyDataFirstCell2 = ActiveCell.Address 'Get the first cell address of Data Area
Selection.End(xlDown).Select 'Get to Bottom Row of the data
Selection.End(xlToRight).Select 'Get to the last Column and data cell by heading to the righthand end
Selection.End(xlToRight).Select
ActiveCell.Offset(-1, 0).Select ' Select the correct last cell
MyDataLastCell2 = ActiveCell.Address 'Get the Cell address of the last cell of my data area
'************************** Establish the Sort column first and last data points.
ActiveSheet.Range("B1").Select
'Next Non Blank Cell down
ActiveCell.Offset(1, 0).Select
Do While IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
'Next Blank Cell down
ActiveCell.Offset(1, 0).Select
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
'Next Non Blank Cell down
ActiveCell.Offset(1, 0).Select
Do While IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(1, 0).Select 'Get to first cell of data sort Column (Example Col 'R' Row 2 becuase Row 1 contains the header)
MySortCellStart2 = ActiveCell.Address 'Get the Cell address of the first cell of my data sort Column
Selection.End(xlDown).Select 'Get to the bottom Row of data
ActiveCell.Offset(-1, 0).Select
MySortCellEnd2 = ActiveCell.Address 'Get the Cell address of the last cell of my sort Column
'************************** Start the sort by specifying sort area and columns
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add _
Key:=Range(MySortCellStart2 & ":" & MySortCellEnd2), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range(MyDataFirstCell2 & ":" & MyDataLastCell2)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Select first element of first table
DoEvents
ActiveSheet.Range("F1").Select
Range(MyDataFirstCell).Select
End Sub
私はVBAでのコーディングで新しいです、私はCのようにしてLPCの言語を知っているが、私はVBAを学んだことがありません。だから、問題を解決する方法やコードを改善する方法の助けを借りて、私はそれについてすべてです。
ご理解とご協力をいただき、ありがとうございます。
あなたはhttp://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel([.Select' 'の使用を避ける]でトンあなたのコードを短縮することができます-vba-macros)。私は実際にあなたのコードを見ていないが、 '.Select'を使うと予期しない結果が生じることがある。エラーが発生したら、「Debug」をクリックします。エラーが発生した行は何ですか?エラーは何ですか? – BruceWayne
失敗したときに表示されるエラーメッセージは何ですか? –
@BruceWayneそれは問題です、それは私に何かエラーを表示されません、それはちょうどときに何をすることをしないでください –