microsoft visual basic运行时错1004
microsoft visual basic运行时错1004 排序引用无效 请确保它在所要排序的数据内,并且第一个“排序依据”框不相同且不为空我的写的代码为:
Sub 住宅类整体_价格区间分布()
Set objSheet1 = Worksheets("Sheet1")
Set objSheet2 = Worksheets("Sheet2")
objSheet1.Select
Selection.Sort Key1:=Range("O2"), Order1:=xlAscending, Key2:=Range("K2") _
, Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _
xlSortNormal, DataOption2:=xlSortNormal
For x = 1 To 60000
If Trim(objSheet1.Cells(x, 1).Value) = "" Then
ImaxRow = x - 1
Exit For
End If
Next
objSheet2.Select
Cells.Select
Selection.ClearContents
Cells.Select
Selection.NumberFormatLocal = "G/通用格式"
objSheet2.Cells(1, 1).Value = "0"
objSheet2.Cells(2, 1).Value = "3500"
objSheet2.Cells(3, 1).Value = "4000"
objSheet2.Cells(4, 1).Value = "4500"
objSheet2.Cells(5, 1).Value = "5000"
objSheet2.Cells(6, 1).Value = "5250"
objSheet2.Cells(7, 1).Value = "5500"
objSheet2.Cells(8, 1).Value = "5750"
objSheet2.Cells(9, 1).Value = "6000"
objSheet2.Cells(10, 1).Value = "6250"
objSheet2.Cells(11, 1).Value = "6500"
objSheet2.Cells(12, 1).Value = "6750"
objSheet2.Cells(13, 1).Value = "7000"
objSheet2.Cells(14, 1).Value = "7500"
objSheet2.Cells(15, 1).Value = "8000"
objSheet2.Cells(16, 1).Value = "9000"
objSheet2.Cells(17, 1).Value = "10000"
objSheet2.Cells(18, 1).Value = "450000"
objSheet2.Cells(19, 1).Value = "合计"
objSheet2.Cells(1, 2).Value = "套数"
objSheet2.Cells(1, 3).Value = "占比"
objSheet2.Cells(1, 4).Value = "变化"
objSheet2.Cells(1, 5).Value = "面积"
objSheet2.Cells(1, 6).Value = "金额"
objSheet2.Cells(1, 7).Value = "均价"
For i = 2 To ImaxRow
For j = 2 To 18
If objSheet1.Cells(i, 15).Value > objSheet2.Cells(j - 1, 1).Value And objSheet1.Cells(i, 15).Value <= objSheet2.Cells(j, 1).Value Then
objSheet2.Cells(j, 2).Value = objSheet2.Cells(j, 2).Value + 1
objSheet2.Cells(j, 5).Value = objSheet2.Cells(j, 5).Value + objSheet1.Cells(i, 14).Value
objSheet2.Cells(j, 6).Value = objSheet2.Cells(j, 6).Value + objSheet1.Cells(i, 16).Value
objSheet2.Cells(19, 2).Value = objSheet2.Cells(19, 2).Value + 1
objSheet2.Cells(19, 5).Value = objSheet2.Cells(19, 5).Value + objSheet1.Cells(i, 14).Value
objSheet2.Cells(19, 6).Value = objSheet2.Cells(19, 6).Value + objSheet1.Cells(i, 16).Value
End If
Next
Next
For j = 2 To 18
If objSheet2.Cells(j, 2).Value <> "" Then
objSheet2.Cells(j, 3).Value = objSheet2.Cells(j, 2).Value / objSheet2.Cells(19, 2).Value
objSheet2.Cells(j, 7).Value = objSheet2.Cells(j, 6).Value / objSheet2.Cells(j, 5).Value
End If
Next
Cells.Select
With Selection.Font
.Name = "宋体"
.Size = 9
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Columns("B:G").Select
Selection.NumberFormatLocal = "0_ "
Columns("C:C").Select
Selection.NumberFormatLocal = "0.00%"
objSheet2.Cells(1, 1).Value = "价格/月份"
objSheet2.Cells(2, 1).Value = "3500以下"
objSheet2.Cells(3, 1).Value = "3500~4000"
objSheet2.Cells(4, 1).Value = "4000~4500"
objSheet2.Cells(5, 1).Value = "4500~5000"
objSheet2.Cells(6, 1).Value = "5000~5250"
objSheet2.Cells(7, 1).Value = "5250~5500"
objSheet2.Cells(8, 1).Value = "5500~5750"
objSheet2.Cells(9, 1).Value = "5750~6000"
objSheet2.Cells(10, 1).Value = "6000~6250"
objSheet2.Cells(11, 1).Value = "6250~6500"
objSheet2.Cells(12, 1).Value = "6500~6750"
objSheet2.Cells(13, 1).Value = "6750~7000"
objSheet2.Cells(14, 1).Value = "7000~7500"
objSheet2.Cells(15, 1).Value = "7500~8000"
objSheet2.Cells(16, 1).Value = "8000~9000"
objSheet2.Cells(17, 1).Value = "9000~10000"
objSheet2.Cells(18, 1).Value = "10000以上"
End Sub
--------------------编程问答-------------------- microsoft visual basic运行时错1004 排序引用无效 请确保它在所要排序的数据内,并且第一个“排序依据”框不相同且不为空
各位高手多多指点 --------------------编程问答-------------------- 没有选择排序范围
Sheet1.Cells.Select'用这个
'objSheet1.Select '删除这个
Selection.Sort Key1:=Range("O2"), Order1:=xlAscending, Key2:=Range("K2") _
, Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _
xlSortNormal, DataOption2:=xlSortNormal --------------------编程问答-------------------- 用这个:
objSheet1.Cells.Select --------------------编程问答-------------------- 还是不可以的啊 --------------------编程问答-------------------- 复制以下语句到excel VBA里去试:
Sheet1.Cells.Select'我试的没问题。
Selection.Sort Key1:=Range("O2"), Order1:=xlAscending, Key2:=Range("K2") _
, Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _
xlSortNormal, DataOption2:=xlSortNormal
然后把sheet1改成你VB里的sheet1的对象。
vb的sheet1对像.Cells.Select --------------------编程问答-------------------- 另外你工程里引用了excel 11.0对象了吗,如果没有的话xlAscending这样的常量要改成数值。
补充:VB , 基础类