这是我在这里发表的第一篇文章...你能帮我解决一下这个代码吗?现在代码是我在Excel电子表格上创建的两个按钮用于工作......你可以看到它创建一个新的电子表格并将结果移动到两列(这很好)我只需要信息(在结果表上)为升序或降序(假设它是相同的东西,但只有单词不同)。提前感谢一百万;欢呼。
Private Sub CommandButton1_Click()
Dim DueDate, CurrentDate As Date
Dim ReadDays, j, k, LastRow, DaysDiff As Integer
Dim ResultSheet, CurrentSheet As Worksheet
j = 4
k = 1
LastRow = 4
CurrentDate = Date
ReadDays = InputBox(Prompt:="Set alarm for how many days?", Title:="Alarm Input Box",
Default:="Number of days")
While ActiveSheet.Cells(LastRow, 1).Value <> ""
LastRow = LastRow + 1
Wend
Set CurrentSheet = ActiveSheet()
Set ResultSheet = ThisWorkbook.Sheets.Add(, ActiveSheet())
While j < LastRow
If (IsDate(CurrentSheet.Cells(j, 16))) Then
DueDate = CurrentSheet.Cells(j, 16)
If (DaysDiff <= ReadDays) Then
ResultSheet.Cells(k, 1) = CurrentSheet.Cells(j, 1)
ResultSheet.Cells(k, 2) = CurrentSheet.Cells(j, 16)
k = k + 1
End If
End If
j = j + 1
Wend
End Sub
Private Sub CommandButton2_Click()
Dim DueDate, CurrentDate As Date
Dim ReadDays, i, k, LastRow, DaysDiff As Integer
Dim ResultSheet, CurrentSheet As Worksheet
i = 4
k = 1
LastRow = 4
CurrentDate = Date
ReadDays = InputBox(Prompt:="Set alarm for how many days?", Title:="Alarm Input Box",
Default:="Number of days")
While ActiveSheet.Cells(LastRow, 1).Value <> ""
LastRow = LastRow + 1
Wend
Set CurrentSheet = ActiveSheet()
Set ResultSheet = ThisWorkbook.Sheets.Add(, ActiveSheet())
While i < LastRow
If (IsDate(CurrentSheet.Cells(i, 14))) Then
DueDate = CurrentSheet.Cells(i, 14)
DaysDiff = DateDiff("d", CurrentDate, DueDate)
If (DaysDiff <= ReadDays) Then
ResultSheet.Cells(k, 1) = CurrentSheet.Cells(i, 1)
ResultSheet.Cells(k, 2) = CurrentSheet.Cells(i, 14)
k = k + 1
End If
End If
i = i + 1
Wend
End Sub
答案 0 :(得分:0)
以下代码对A1:A9:
范围内的数据进行排序Range("A1:A9").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:A9")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
代码是使用宏录制器生成的。
答案 1 :(得分:0)
只需改编“VBA Proggrammer”的代码,就像这样:
'after your code
dim LastRow as long
Dim Rg as Range
with resultsheet
LastRow = .cells(.rows.count,1).end(xlup).row 'might be same as k in your code
Set Rg= .Range("A1:B" & trim (str(LastRow)))
end With
with Rg.Sort
with .SortFields
.Clear
.Add Key:=Range("A1"), SortOn:=xlSortOnValues, _
order:=xlAscending, DataOption:= xlSortTextAsNumbers
End With
.SetRange Range("A1:A9")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
除此之外,您声明变量的方式:
Dim ReadDays, j, k, LastRow, DaysDiff As Integer
是一个很好的例子: 首先使用Long,因为在VBA中工作得更快。 第二,如果你把dim a,b写为Integer,变量a被声明为variant,而不是整数。
正确的方法:
Dim ReadDays as Long, j as Long, k as Long, LastRow as Long, DaysDiff As Long
答案 2 :(得分:0)
有一些帮助..这是最终的代码,它的工作正常,感谢您的帮助:
Sub CommandButton1_Click()
Dim DueDate, CurrentDate As Date
Dim ReadDays, j, k, LastRow, DaysDiff As Integer
Dim ResultSheet, CurrentSheet As Worksheet
Dim SortRange As Range
Dim SortColumn As Range
j = 4
k = 1
LastRow = 4
CurrentDate = Date
ReadDays = InputBox(Prompt:="Set alarm for how many days?", Title:="Alarm Input Box", Default:="Number of days")
While ActiveSheet.Cells(LastRow, 1).Value <> ""
LastRow = LastRow + 1
Wend
Set CurrentSheet = ActiveSheet()
Set ResultSheet = ThisWorkbook.Sheets.Add(, ActiveSheet())
While j < LastRow
If (IsDate(CurrentSheet.Cells(j, 16))) Then
DueDate = CurrentSheet.Cells(j, 16)
DaysDiff = DateDiff("d", CurrentDate, DueDate)
If (DaysDiff <= ReadDays) Then
ResultSheet.Cells(k, 1) = CurrentSheet.Cells(j, 1)
ResultSheet.Cells(k, 2) = CurrentSheet.Cells(j, 16)
k = k + 1
End If
End If
j = j + 1
Wend
Set SortRange = Range("A1:B" & k)
Set SortColumn = Range("B1")
Columns(1).ColumnWidth = 24
Columns(2).ColumnWidth = 12
SortRange.NumberFormat = "m/d/yyyy"
SortRange.Sort SortColumn, xlAscending
End Sub
这只是一个按钮的示例,您也可以对另一个按钮进行更改。
归功于戴夫:) ..感谢我的朋友。