我有一个excel电子表格,其中包含一个公式为=IF(L2=N2, IF(N2>0,TRUE, FALSE), FALSE)
的列,它给出了一个True或False值。如果我在excel中打开此电子表格并按升序对列进行排序,则会对其进行正确排序。录制返回的宏如下。
ActiveWorkbook.Worksheets("Incorrect Items").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Incorrect Items").AutoFilter.Sort.SortFields.Add _
Key:=Range("X1:X2188"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Incorrect Items").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
当我将其转移到我有访问权时。
With WorkSheet
.AutoFilter.Sort.SortFields.Clear
.AutoFilter.Sort.SortFields.add _
Key:=.range("X1:X2188"), SortOn:=0, Order:=1, DataOption:=0
With .AutoFilter.Sort
.Header = 1
.MatchCase = False
.Orientation = 1
.SortMethod = 1
.Apply
End With
End With
代码运行良好,没有返回任何错误,但似乎忽略了xlSortOnValues
常量并且没有对列进行排序。在排序之前复制和粘贴值,但我想知道为什么这在excel中起作用而不是从访问中起作用。
答案 0 :(得分:1)
xlSortOnValues
的处理方式不同,因此请确保您具有正确的库(VBA编辑器中的对象引用)。
答案 1 :(得分:1)
当从MS Access自动化MS Excel并处理公式,例如对具有从公式创建的值的列进行排序或从公式复制和的结果时,必须将计算设置为自动以观察正确的结果{{1 }}