Excel VBA中的问题;。套用;

Excel VBA中的问题;。套用;,excel,vba,apply,Excel,Vba,Apply,尝试创建一个动态排序宏,在第一列上工作,然后在尝试在新列上运行时出现错误(尝试在不更改其他列顺序的情况下对每个列进行a-Z排序) 您正在尝试使用录制使用的排序代码版本。这非常冗长,并且“记住”以前操作中的参数值。有一种更简洁的语法也同样有效 dim c as long with activesheet for c=1 to .cells(1, .columns.count).end(xltoleft).column if .cells(.rows.count, c).e

尝试创建一个动态排序宏,在第一列上工作,然后在尝试在新列上运行时出现错误(尝试在不更改其他列顺序的情况下对每个列进行a-Z排序)


您正在尝试使用录制使用的排序代码版本。这非常冗长,并且“记住”以前操作中的参数值。有一种更简洁的语法也同样有效

dim c as long

with activesheet
    for c=1 to .cells(1, .columns.count).end(xltoleft).column
        if .cells(.rows.count, c).end(xlup).row >1 then
            with .columns(c).Cells
                .Sort Key1:=.cells(1), Order1:=xlAscending, _
                      Orientation:=xlTopToBottom, Header:=xlNo
            end with
        end if
    next c
end with

你能描述一下你得到的错误吗?“VBA.apply中的问题”没有那么明确unfortunately@UncleBenBen你说得对,但我的钱在一条无用的“1004:应用程序定义错误”消息上;-)您说您希望一次对单个列进行排序,而不影响其他列,但要定义
LastColumn=sht.Cells(StartCell.Row,sht.columns.Count).End(xlToLeft.Column
然后使用
.SetRange ActiveCell.range(StartCell,sht.Cells(LastRow,LastColumn))
未在单个列上运行。这是1004错误,抱歉!!!所以,它在我的工作表的前几列中有效,有时比其他列更有效,我还没有找到原因。我如何确保它适用于整个工作表?这是我第一次尝试为VBA编写代码。很抱歉,您留下了太多问题没有回答,我无法提供明确的答案。也许可以在你的问题中找到注释部分,并编辑你的问题,以包括错误号以及它实际发生在代码的哪一行。
dim c as long

with activesheet
    for c=1 to .cells(1, .columns.count).end(xltoleft).column
        if .cells(.rows.count, c).end(xlup).row >1 then
            with .columns(c).Cells
                .Sort Key1:=.cells(1), Order1:=xlAscending, _
                      Orientation:=xlTopToBottom, Header:=xlNo
            end with
        end if
    next c
end with