Excel 中的自定义排序和边框
Custom sorting and borders in Excel
我有一个 Excel table 看起来像这样(出于与工作相关的明显原因编辑了值,但格式仍然适用):
(请向下滚动查看我需要的东西,以及我制作的 VBA 脚本,但由于某些原因无法正常运行 - 是的,我是脚本编写的初学者)
A B C D E F G H I J
XNumber Typ SBin Bpos SSU Material qty No. C
8421017 ASD 111-A-07 1 6532378604 31814605 150 ST 154A148
8421033 ASD 111-A-07 2 6532378605 32150251 30 ST 154A132
8421207 ASD 111-A-07 3 6532414771 32153499 25 ST 154A839
8421108 ASD 111-A-07 3 6532414771 32153495 25 ST 154A741
8421123 ASD 111-A-07 3 6532414771 32153495 25 ST 154A756
8421112 ASD 111-A-07 3 6532414771 32153495 25 ST 154A743
8421210 ASD 111-A-07 3 6532414771 32153499 25 ST 154A842
8421116 ASD 111-A-07 3 6532414771 32153495 25 ST 154A747
8421120 ASD 111-A-07 3 6532414771 32153495 25 ST 154A752
8421128 ASD 111-A-07 3 6532414771 32153495 25 ST 154A758
8421019 ASD 111-B-01 3 6532375593 32114883 30 ST 154A143
8421023 ASD 111-B-04 3 6532413406 32153495 25 ST 154A134
8421014 ASD 111-B-06 2 6532356348 32114885 30 ST 154A142
8421009 ASD 111-B-07 1 6532391172 32114884 30 ST 154A151
8421030 ASD 111-B-07 1 6532391172 32150249 30 ST 154A147
8421185 ASD 111-B-07 2 6532416499 32153494 30 ST 154A819
8421202 ASD 111-B-07 2 6532416499 32153495 25 ST 154A835
8421161 ASD 111-B-07 2 6532416499 32153494 30 ST 154A797
8421157 ASD 111-B-07 2 6532416499 32153494 30 ST 154A790
8421173 ASD 111-B-07 2 6532416499 32153494 30 ST 154A809
8421177 ASD 111-B-07 2 6532416499 32153494 30 ST 154A812
8421165 ASD 111-B-07 2 6532416499 32153494 30 ST 154A800
8421122 ASD 111-B-07 2 6532416499 32150246 30 ST 154A757
8421169 ASD 111-B-07 2 6532416499 32153494 30 ST 154A803
8421181 ASD 111-B-07 2 6532416499 32153494 30 ST 154A817
8421011 ASD 111-C-01 1 6532398471 32081811 300 ST 154A145
8421114 ASD 111-C-02 2 6532414779 32150246 30 ST 154A749
8421136 ASD 111-C-02 2 6532414779 32153495 25 ST 154A766
8421132 ASD 111-C-02 2 6532414779 32153495 25 ST 154A764
8421228 ASD 111-C-02 2 6532414779 32153499 25 ST 154A861
8421118 ASD 111-C-02 2 6532414779 32150246 30 ST 154A750
8421215 ASD 111-C-02 2 6532414779 32153499 25 ST 154A847
8421140 ASD 111-C-02 2 6532414779 32153495 25 ST 154A772
8421220 ASD 111-C-02 2 6532414779 32153499 25 ST 154A852
8421224 ASD 111-C-02 2 6532414779 32153499 25 ST 154A855
8421012 ASD 111-C-03 3 6532375624 32185577 30 ST 154A149
8421076 ASD 111-C-04 1 6532414781 32114882 30 ST 154A710
8421080 ASD 111-C-04 1 6532414781 32150246 30 ST 154A714
8421086 ASD 111-C-04 1 6532414781 32150246 30 ST 154A720
8421074 ASD 111-C-04 1 6532414781 32113181 150 ST 154A708
8421077 ASD 111-C-04 1 6532414781 32150248 30 ST 154A711
8421231 ASD 111-C-04 1 6532414781 32153499 25 ST 154A863
8421233 ASD 111-C-04 1 6532414781 32153499 25 ST 154A865
8421093 ASD 111-C-04 1 6532414781 32150246 30 ST 154A726
8421084 ASD 111-C-04 1 6532414781 32114882 30 ST 154A717
8421232 ASD 111-D-01 1 6532417810 32150246 30 ST 154A867
8421222 ASD 111-D-01 1 6532417810 32150246 30 ST 154A857
8421229 ASD 111-D-01 1 6532417810 32150246 30 ST 154A862
8421238 ASD 111-D-01 1 6532417810 32150246 30 ST 154A871
8421235 ASD 111-D-01 1 6532417810 32150246 30 ST 154A868
8421040 ASD 111-D-01 1 6532417810 32150743 30 ST 154A671
8421218 ASD 111-D-01 1 6532417810 32150246 30 ST 154A850
8421226 ASD 111-D-01 1 6532417810 32150246 30 ST 154A859
8421241 ASD 111-D-01 1 6532417810 32150246 30 ST 154A876
8421005 ASD 111-D-01 3 6532303506 32150254 30 ST 154A663
8421216 ASD 111-D-03 3 6532417804 32150246 30 ST 154A848
8421205 ASD 111-D-03 3 6532417804 32150246 30 ST 154A838
8421211 ASD 111-D-03 3 6532417804 32150246 30 ST 154A844
8421043 ASD 111-D-03 3 6532417804 32150255 30 ST 154A679
8421090 ASD 111-D-03 3 6532417804 32114882 30 ST 154A725
8421004 ASD 111-D-04 2 6532398452 32150254 30 ST 154A662
8421237 ASD 111-D-05 3 6532414792 32153499 25 ST 154A870
8421144 ASD 111-D-05 3 6532414792 32153495 25 ST 154A777
8421190 ASD 111-D-06 3 6532416543 32150246 30 ST 154A825
8421178 ASD 111-D-06 3 6532416543 32150246 30 ST 154A813
8421194 ASD 111-D-06 3 6532416543 32150246 30 ST 154A826
8421201 ASD 111-D-06 3 6532416543 32150246 30 ST 154A836
8421162 ASD 111-D-06 3 6532416543 32150246 30 ST 154A795
8421158 ASD 111-D-06 3 6532416543 32150246 30 ST 154A793
8421197 ASD 111-D-06 3 6532416543 32150246 30 ST 154A830
8421186 ASD 111-D-06 3 6532416543 32150246 30 ST 154A821
8421166 ASD 111-D-06 3 6532416543 32150246 30 ST 154A798
8421170 ASD 111-D-06 3 6532416543 32150246 30 ST 154A805
8421154 ASD 111-D-06 3 6532416543 32150246 30 ST 154A789
8421174 ASD 111-D-06 3 6532416543 32150246 30 ST 154A808
8421182 ASD 111-D-06 3 6532416543 32150246 30 ST 154A814
8421239 ASD 111-E-05 3 6532414797 32153499 25 ST 154A873
8421242 ASD 111-E-05 3 6532414797 32153499 25 ST 154A875
8421036 ASD 111-F-01 2 6532411757 32114878 30 ST 154A672
8421013 ASD 111-F-03 3 6532408584 32012238 30 ST 154A140
8421008 ASD 111-F-03 3 6532408584 32074861 30 ST 154A150
8421003 ASD 111-F-03 3 6532408584 32150254 30 ST 154A126
8421047 ASD 111-F-05 2 6532422980 32012238 30 ST 154A681
8421183 ASD 111-F-06 1 6532414832 32153495 25 ST 154A816
8421187 ASD 111-F-06 1 6532414832 32153495 25 ST 154A820
8421191 ASD 111-F-06 1 6532414832 32153495 25 ST 154A823
8421028 ASD 111-F-06 2 6532403825 32150255 30 ST 154A130
8421221 ASD 111-F-06 3 6532417790 32153494 30 ST 154A854
8421204 ASD 111-F-06 3 6532417790 32153494 30 ST 154A837
8421217 ASD 111-F-06 3 6532417790 32153494 30 ST 154A853
8421208 ASD 111-F-06 3 6532417790 32153494 30 ST 154A841
8421225 ASD 111-F-06 3 6532417790 32153494 30 ST 154A858
8421212 ASD 111-F-06 3 6532417790 32153494 30 ST 154A845
8421213 ASD 111-F-06 3 6532417790 32153494 30 ST 154A849
8421107 ASD 111-G-04 1 6532411911 32153499 25 ST 154A738
8421111 ASD 111-G-04 1 6532411911 32153499 25 ST 154A745
8421119 ASD 111-G-04 2 6532411913 32153499 25 ST 154A753
8421135 ASD 111-G-04 2 6532411913 32153499 25 ST 154A769
8421124 ASD 111-G-04 2 6532411913 32153499 25 ST 154A755
8421127 ASD 111-G-04 2 6532411913 32153499 25 ST 154A761
8421131 ASD 111-G-04 2 6532411913 32153499 25 ST 154A763
8421115 ASD 111-G-04 2 6532411913 32153499 25 ST 154A748
8421139 ASD 111-G-04 2 6532411913 32153499 25 ST 154A771
8421091 ASD 111-G-06 1 6532411761 32153494 30 ST 154A723
8421095 ASD 111-G-06 1 6532411761 32153494 30 ST 154A729
8421097 ASD 111-G-06 1 6532411761 32153494 30 ST 154A733
8421075 ASD 111-G-06 1 6532411761 32153494 30 ST 154A707
8421083 ASD 111-G-06 1 6532411761 32153494 30 ST 154A716
8421082 ASD 111-G-06 3 6532416505 32113181 150 ST 154A715
8421219 ASD 111-G-06 3 6532416505 32153495 25 ST 154A851
8421223 ASD 111-G-06 3 6532416505 32153495 25 ST 154A856
8421227 ASD 111-G-06 3 6532416505 32153495 25 ST 154A860
8421230 ASD 111-G-06 3 6532416505 32153495 25 ST 154A864
8421089 ASD 111-G-06 3 6532416505 32113181 150 ST 154A722
8421206 ASD 111-G-06 3 6532416505 32153495 25 ST 154A840
8421209 ASD 111-G-06 3 6532416505 32153495 25 ST 154A843
8421214 ASD 111-G-06 3 6532416505 32153495 25 ST 154A846
8421020 ASD 111-G-07 1 6532414831 31983630 20 ST 154A139
8421035 ASD 111-G-07 1 6532414831 31983630 20 ST 154A666
8421179 ASD 111-G-07 1 6532414831 32153495 25 ST 154A810
8421018 ASD 111-G-07 1 6532414831 31850999 30 ST 154A137
8421026 ASD 111-H-04 1 6532411763 32150250 30 ST 154A129
8421031 ASD 111-H-05 1 6532392763 32150252 30 ST 154A146
8421038 ASD 111-H-05 1 6532392763 32150256 30 ST 154A674
8421029 ASD 111-H-05 1 6532392763 31814606 250 ST 154A152
8421015 ASD 111-H-05 1 6532392763 32150256 30 ST 154A141
8421041 ASD 111-H-06 2 6532411917 32150250 30 ST 154A678
8421134 ASD 111-H-07 2 6532416534 32150246 30 ST 154A768
8421200 ASD 111-H-07 2 6532416534 32153494 30 ST 154A833
8421130 ASD 111-H-07 2 6532416534 32150246 30 ST 154A765
8421189 ASD 111-H-07 2 6532416534 32153494 30 ST 154A822
8421138 ASD 111-H-07 2 6532416534 32150246 30 ST 154A770
8421142 ASD 111-H-07 2 6532416534 32150246 30 ST 154A775
8421193 ASD 111-H-07 2 6532416534 32153494 30 ST 154A829
8421126 ASD 111-H-07 2 6532416534 32150246 30 ST 154A759
8421146 ASD 111-H-07 2 6532416534 32150246 30 ST 154A780
8421150 ASD 111-H-07 2 6532416534 32150246 30 ST 154A785
8421247 ASD 111-H-07 3 6532416523 32153495 25 ST 154A880
8421248 ASD 111-H-07 3 6532416523 32153495 25 ST 154A881
8421245 ASD 111-H-07 3 6532416523 32153495 25 ST 154A877
8421243 ASD 111-H-07 3 6532416523 32153495 25 ST 154A874
8421249 ASD 111-H-07 3 6532416523 32153495 25 ST 154A882
8421234 ASD 111-H-07 3 6532416523 32153495 25 ST 154A866
8421236 ASD 111-H-07 3 6532416523 32153495 25 ST 154A869
8421057 ASD 111-H-07 3 6532416523 31983630 20 ST 154A695
8421065 ASD 111-H-07 3 6532416523 31983630 20 ST 154A699
8421240 ASD 111-H-07 3 6532416523 32153495 25 ST 154A872
8421053 ASD 111-I-03 1 6532411919 32150250 30 ST 154A687
8421025 ASD 111-I-04 1 6532411924 32153499 25 ST 154A133
8421034 ASD 111-I-07 1 6532411888 32012238 30 ST 154A668
8421098 ASD 111-I-07 2 6532414829 32150246 30 ST 154A732
8421106 ASD 111-I-07 2 6532414829 32150246 30 ST 154A739
8421068 ASD 111-I-07 2 6532414829 32114882 30 ST 154A701
8421073 ASD 111-I-07 2 6532414829 32150246 30 ST 154A704
8421102 ASD 111-I-07 2 6532414829 32150246 30 ST 154A734
8421110 ASD 111-I-07 2 6532414829 32150246 30 ST 154A742
8421195 ASD 111-I-07 3 6532414834 32153495 25 ST 154A827
8421198 ASD 111-I-07 3 6532414834 32153495 25 ST 154A831
8421016 ASD 111-J-04 1 6532411934 32114878 30 ST 154A138
8421064 ASD 111-J-04 1 6532411934 32150250 30 ST 154A697
8421069 ASD 111-J-04 1 6532411934 32150250 30 ST 154A705
8421085 ASD 111-J-04 2 6532411935 32150250 30 ST 154A718
8421092 ASD 111-J-04 2 6532411935 32150250 30 ST 154A724
8421022 ASD 111-J-04 2 6532411935 32150743 30 ST 154A131
8421078 ASD 111-J-04 2 6532411935 32150250 30 ST 154A713
8421067 ASD 111-J-04 3 6532411936 32153499 25 ST 154A154A
8421079 ASD 111-J-04 3 6532411936 32153499 25 ST 154A709
8421087 ASD 111-J-04 3 6532411936 32153499 25 ST 154A719
8421039 ASD 111-J-04 3 6532411936 32153499 25 ST 154A669
8421054 ASD 111-J-04 3 6532411936 32153499 25 ST 154A686
8421070 ASD 111-J-05 2 6532414827 32113181 150 ST 154A706
8421246 ASD 111-J-05 2 6532414827 32153499 25 ST 154A879
8421147 ASD 111-J-05 2 6532414827 32153495 25 ST 154A781
8421171 ASD 111-J-05 2 6532414827 32153495 25 ST 154A802
8421155 ASD 111-J-05 2 6532414827 32153495 25 ST 154A786
8421167 ASD 111-J-05 2 6532414827 32153495 25 ST 154A799
8421175 ASD 111-J-05 2 6532414827 32153495 25 ST 154A806
8421052 ASD 111-J-05 2 6532414827 31983630 20 ST 154A684
8421151 ASD 111-J-05 2 6532414827 32153495 25 ST 154A782
8421244 ASD 111-J-05 2 6532414827 32153499 25 ST 154A878
8421160 ASD 111-J-05 2 6532414827 32153495 25 ST 154A792
8421164 ASD 111-J-05 2 6532414827 32153495 25 ST 154A796
8421062 ASD 111-J-05 2 6532414827 32113181 150 ST 154A696
8421055 ASD 111-J-07 3 6532419615 32150743 30 ST 154A688
8421042 ASD 111-K-02 3 6532411892 32153494 30 ST 154A675
8421024 ASD 111-K-02 3 6532411892 32153494 30 ST 154A135
8421051 ASD 111-L-01 1 6532411938 32153494 30 ST 154A685
8421103 ASD 111-L-01 1 6532411938 32153499 25 ST 154A735
8421099 ASD 111-L-01 1 6532411938 32153499 25 ST 154A731
8421094 ASD 111-L-01 1 6532411938 32153499 25 ST 154A727
8421059 ASD 111-L-01 1 6532411938 32153494 30 ST 154A694
8421066 ASD 111-L-01 1 6532411938 32153494 30 ST 154A700
8421203 ASD 111-L-03 1 6532413444 32153499 25 ST 154A834
8421021 ASD 111-L-03 1 6532413444 32113181 150 ST 154A136
8421101 ASD 111-L-03 2 6532413446 32153494 30 ST 154A737
8421109 ASD 111-L-03 2 6532413446 32153494 30 ST 154A744
8421129 ASD 111-L-03 2 6532413446 32153494 30 ST 154A762
8421153 ASD 111-L-03 2 6532413446 32153494 30 ST 154A788
8421141 ASD 111-L-03 2 6532413446 32153494 30 ST 154A774
8421117 ASD 111-L-03 2 6532413446 32153494 30 ST 154A751
8421104 ASD 111-L-03 2 6532413446 32153495 25 ST 154A736
8421125 ASD 111-L-03 2 6532413446 32153494 30 ST 154A760
8421137 ASD 111-L-03 2 6532413446 32153494 30 ST 154A773
8421113 ASD 111-L-03 2 6532413446 32153494 30 ST 154A746
8421149 ASD 111-L-03 2 6532413446 32153494 30 ST 154A784
8421105 ASD 111-L-03 2 6532413446 32153494 30 ST 154A740
8421100 ASD 111-L-03 2 6532413446 32153495 25 ST 154A730
8421121 ASD 111-L-03 2 6532413446 32153494 30 ST 154A754
8421133 ASD 111-L-03 2 6532413446 32153494 30 ST 154A767
8421145 ASD 111-L-03 2 6532413446 32153494 30 ST 154A778
8421180 ASD 111-L-03 3 6532413410 32153499 25 ST 154A811
8421176 ASD 111-L-03 3 6532413410 32153499 25 ST 154A807
8421184 ASD 111-L-04 2 6532413418 32153499 25 ST 154A815
8421037 ASD 111-L-04 2 6532413418 32113181 150 ST 154A670
8421199 ASD 111-L-04 2 6532413418 32153499 25 ST 154A832
8421050 ASD 111-L-04 2 6532413418 32113181 150 ST 154A683
8421192 ASD 111-L-04 2 6532413418 32153499 25 ST 154A824
8421188 ASD 111-L-04 2 6532413418 32153499 25 ST 154A818
8421196 ASD 111-L-04 2 6532413418 32153499 25 ST 154A828
8421027 ASD 111-L-05 1 6532413420 32150248 30 ST 154A127
8421007 ASD 111-L-05 1 6532413420 32150254 30 ST 154A665
8421010 ASD 111-L-05 1 6532413420 32114882 30 ST 154A144
8421045 ASD 111-L-05 1 6532413420 32150248 30 ST 154A677
8421006 ASD 111-L-05 2 6532413393 32150254 30 ST 154A664
8421096 ASD 111-L-06 1 6532413380 32153495 25 ST 154A728
8421148 ASD 111-L-06 1 6532413380 32153499 25 ST 154A779
8421152 ASD 111-L-06 1 6532413380 32153499 25 ST 154A783
8421046 ASD 111-L-06 1 6532413380 32153495 25 ST 154A676
8421143 ASD 111-L-06 1 6532413380 32153499 25 ST 154A776
8421081 ASD 111-L-06 1 6532413380 32153495 25 ST 154A712
8421060 ASD 111-L-06 1 6532413380 32153495 25 ST 154A689
8421072 ASD 111-L-06 1 6532413380 32153495 25 ST 154A702
8421088 ASD 111-L-06 1 6532413380 32153495 25 ST 154A721
8421168 ASD 111-L-06 1 6532413380 32153499 25 ST 154A801
8421172 ASD 111-L-06 1 6532413380 32153499 25 ST 154A804
8421156 ASD 111-L-06 1 6532413380 32153499 25 ST 154A787
8421159 ASD 111-L-06 1 6532413380 32153499 25 ST 154A791
8421163 ASD 111-L-06 1 6532413380 32153499 25 ST 154A794
8421049 ASD 111-L-06 3 6532413424 32150246 30 ST 154A680
8421058 ASD 111-L-06 3 6532413424 32114878 30 ST 154A691
8421063 ASD 111-L-06 3 6532413424 32150246 30 ST 154A693
8421044 ASD 111-L-06 3 6532413424 32114882 30 ST 154A673
8421048 ASD 111-L-06 3 6532413424 32114878 30 ST 154A682
8421032 ASD 111-L-06 3 6532413424 32150246 30 ST 154A128
8421056 ASD 111-L-06 3 6532413424 32114882 30 ST 154A690
8421061 ASD 111-L-06 3 6532413424 32150248 30 ST 154A692
8421071 ASD 111-L-06 3 6532413424 32150248 30 ST 154A703
我想寻求有关 VBA 脚本的帮助,该脚本将执行以下操作:
对所有 table 排序(select 所有有值的单元格 - 包括工作人员使用但为空的 J 列),并且仅升序排序通过 "Material"(F 列),根据需要重新排列行,确保不会对数量值求和 - 排序后我需要相同数量的行。
排序完成后,我需要一个脚本来开始分析table,并在每个相同的material周围制作一个边框。或者,如果它更简单,从第 1 行开始分析,并且,如果下一行的 J 列上的以下值有任何不同(或为空),则执行 Borders(xlEdgeBottom).Weight = xlThick
我想出的 VBA 脚本如下(排序不起作用,它汇总了数量,这不好,而且还以某种方式按 SBin 排序)。
边框脚本省略了在第一行下方放置边框(在我将整个 table 按 Material 排序之后)(因为第二行具有不同的值),并且还省略了在最后一行下方放置边框,下一行的单元格将为空。
请帮助我,我们将不胜感激。
VBA代码如下:
Sub AddBorders()
' define stuff
Dim bottomN As Integer
bottomN = Range("F" & Rows.Count).End(xlUp).Row
' border all
With ActiveSheet.UsedRange.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With ActiveSheet.UsedRange.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
' convert to numbers
Range("A:A").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("D:D").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("E:E").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("F:F").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("I:I").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
' sorting --- not working as intented yet
'ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
'ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("F1:F" & bottomN) _
' , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
' xlSortTextAsNumbers
'With ActiveWorkbook.Worksheets("Sheet1").Sort
' .SetRange Range("F1:F" & bottomN)
' .Header = xlYes
' .MatchCase = False
' .Orientation = xlTopToBottom
' .SortMethod = xlPinYin
' .Apply
'End With
' start thick borders
Dim startRow As Integer
Dim iRow As Integer
startRow = 1
For iRow = 2 To ActiveCell.CurrentRegion.Rows.Count
If WorksheetFunction.IsNumber(Cells(iRow - 1, 6)) Then
If Cells(iRow, 6) <> Cells(iRow - 1, 6) Then
AddBorder startRow, iRow - 1
startRow = iRow
End If
Else
AddBorder startRow, iRow
End If
Next iRow
End Sub
Sub AddBorder(startRow As Integer, endRow As Integer)
Dim borderRange As Range
Dim randomColor As Integer
randomColor = Int((56 * Rnd) + 1)
' tweaked for the same row borders
Set borderRange = Range("A" & endRow & ":J" & endRow)
'borderRange.BorderAround ColorIndex:=0, Weight:=xlThick
borderRange.Borders(xlEdgeBottom).Weight = xlThick
End Sub
提前谢谢你。如果我能在你的帮助下解决这个问题,这对我来说意义重大。
- 大卫.
排序脚本
Sub Macro1()
Range("A1:J" & Range("A" & Rows.Count).End(xlUp).Row).Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range_
("F1:F" & Range("A" & Rows.Count).End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
我会这样做的:
' define stuff
Dim ws As Worksheet
Set ws = ActiveSheet 'never like to use "Active" option. it is much much better to precisely define your workbook and worksheets
Dim bottomN As Integer
bottomN = Range("F" & Rows.Count).End(xlUp).Row
' border all
ws.UsedRange.Borders.LineStyle = xlContinuous
With ws.Sort
With .SortFields
.Clear
.Add Key:=Range("F2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
.SetRange Range("A2:I248")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' start thick borders
Dim startRow As Integer
Dim iRow As Integer
startRow = 1
For iRow = 2 To bottomN
If WorksheetFunction.IsNumber(Cells(iRow - 1, 6).Value) = True Then
If StrComp(CStr(Cells(iRow, 6)), CStr(Cells(iRow - 1, 6))) <> 0 Then
AddBorder startRow, iRow - 1
startRow = iRow
End If
Else
AddBorder startRow, iRow
End If
Next iRow
谢谢大家!
我采用了 izzymo 制作的排序脚本和 PolaEla 制作的一些脚本,并设法完成了脚本并使其执行我需要的操作。
非常感谢您的帮助!
我有一个 Excel table 看起来像这样(出于与工作相关的明显原因编辑了值,但格式仍然适用):
(请向下滚动查看我需要的东西,以及我制作的 VBA 脚本,但由于某些原因无法正常运行 - 是的,我是脚本编写的初学者)
A B C D E F G H I J
XNumber Typ SBin Bpos SSU Material qty No. C
8421017 ASD 111-A-07 1 6532378604 31814605 150 ST 154A148
8421033 ASD 111-A-07 2 6532378605 32150251 30 ST 154A132
8421207 ASD 111-A-07 3 6532414771 32153499 25 ST 154A839
8421108 ASD 111-A-07 3 6532414771 32153495 25 ST 154A741
8421123 ASD 111-A-07 3 6532414771 32153495 25 ST 154A756
8421112 ASD 111-A-07 3 6532414771 32153495 25 ST 154A743
8421210 ASD 111-A-07 3 6532414771 32153499 25 ST 154A842
8421116 ASD 111-A-07 3 6532414771 32153495 25 ST 154A747
8421120 ASD 111-A-07 3 6532414771 32153495 25 ST 154A752
8421128 ASD 111-A-07 3 6532414771 32153495 25 ST 154A758
8421019 ASD 111-B-01 3 6532375593 32114883 30 ST 154A143
8421023 ASD 111-B-04 3 6532413406 32153495 25 ST 154A134
8421014 ASD 111-B-06 2 6532356348 32114885 30 ST 154A142
8421009 ASD 111-B-07 1 6532391172 32114884 30 ST 154A151
8421030 ASD 111-B-07 1 6532391172 32150249 30 ST 154A147
8421185 ASD 111-B-07 2 6532416499 32153494 30 ST 154A819
8421202 ASD 111-B-07 2 6532416499 32153495 25 ST 154A835
8421161 ASD 111-B-07 2 6532416499 32153494 30 ST 154A797
8421157 ASD 111-B-07 2 6532416499 32153494 30 ST 154A790
8421173 ASD 111-B-07 2 6532416499 32153494 30 ST 154A809
8421177 ASD 111-B-07 2 6532416499 32153494 30 ST 154A812
8421165 ASD 111-B-07 2 6532416499 32153494 30 ST 154A800
8421122 ASD 111-B-07 2 6532416499 32150246 30 ST 154A757
8421169 ASD 111-B-07 2 6532416499 32153494 30 ST 154A803
8421181 ASD 111-B-07 2 6532416499 32153494 30 ST 154A817
8421011 ASD 111-C-01 1 6532398471 32081811 300 ST 154A145
8421114 ASD 111-C-02 2 6532414779 32150246 30 ST 154A749
8421136 ASD 111-C-02 2 6532414779 32153495 25 ST 154A766
8421132 ASD 111-C-02 2 6532414779 32153495 25 ST 154A764
8421228 ASD 111-C-02 2 6532414779 32153499 25 ST 154A861
8421118 ASD 111-C-02 2 6532414779 32150246 30 ST 154A750
8421215 ASD 111-C-02 2 6532414779 32153499 25 ST 154A847
8421140 ASD 111-C-02 2 6532414779 32153495 25 ST 154A772
8421220 ASD 111-C-02 2 6532414779 32153499 25 ST 154A852
8421224 ASD 111-C-02 2 6532414779 32153499 25 ST 154A855
8421012 ASD 111-C-03 3 6532375624 32185577 30 ST 154A149
8421076 ASD 111-C-04 1 6532414781 32114882 30 ST 154A710
8421080 ASD 111-C-04 1 6532414781 32150246 30 ST 154A714
8421086 ASD 111-C-04 1 6532414781 32150246 30 ST 154A720
8421074 ASD 111-C-04 1 6532414781 32113181 150 ST 154A708
8421077 ASD 111-C-04 1 6532414781 32150248 30 ST 154A711
8421231 ASD 111-C-04 1 6532414781 32153499 25 ST 154A863
8421233 ASD 111-C-04 1 6532414781 32153499 25 ST 154A865
8421093 ASD 111-C-04 1 6532414781 32150246 30 ST 154A726
8421084 ASD 111-C-04 1 6532414781 32114882 30 ST 154A717
8421232 ASD 111-D-01 1 6532417810 32150246 30 ST 154A867
8421222 ASD 111-D-01 1 6532417810 32150246 30 ST 154A857
8421229 ASD 111-D-01 1 6532417810 32150246 30 ST 154A862
8421238 ASD 111-D-01 1 6532417810 32150246 30 ST 154A871
8421235 ASD 111-D-01 1 6532417810 32150246 30 ST 154A868
8421040 ASD 111-D-01 1 6532417810 32150743 30 ST 154A671
8421218 ASD 111-D-01 1 6532417810 32150246 30 ST 154A850
8421226 ASD 111-D-01 1 6532417810 32150246 30 ST 154A859
8421241 ASD 111-D-01 1 6532417810 32150246 30 ST 154A876
8421005 ASD 111-D-01 3 6532303506 32150254 30 ST 154A663
8421216 ASD 111-D-03 3 6532417804 32150246 30 ST 154A848
8421205 ASD 111-D-03 3 6532417804 32150246 30 ST 154A838
8421211 ASD 111-D-03 3 6532417804 32150246 30 ST 154A844
8421043 ASD 111-D-03 3 6532417804 32150255 30 ST 154A679
8421090 ASD 111-D-03 3 6532417804 32114882 30 ST 154A725
8421004 ASD 111-D-04 2 6532398452 32150254 30 ST 154A662
8421237 ASD 111-D-05 3 6532414792 32153499 25 ST 154A870
8421144 ASD 111-D-05 3 6532414792 32153495 25 ST 154A777
8421190 ASD 111-D-06 3 6532416543 32150246 30 ST 154A825
8421178 ASD 111-D-06 3 6532416543 32150246 30 ST 154A813
8421194 ASD 111-D-06 3 6532416543 32150246 30 ST 154A826
8421201 ASD 111-D-06 3 6532416543 32150246 30 ST 154A836
8421162 ASD 111-D-06 3 6532416543 32150246 30 ST 154A795
8421158 ASD 111-D-06 3 6532416543 32150246 30 ST 154A793
8421197 ASD 111-D-06 3 6532416543 32150246 30 ST 154A830
8421186 ASD 111-D-06 3 6532416543 32150246 30 ST 154A821
8421166 ASD 111-D-06 3 6532416543 32150246 30 ST 154A798
8421170 ASD 111-D-06 3 6532416543 32150246 30 ST 154A805
8421154 ASD 111-D-06 3 6532416543 32150246 30 ST 154A789
8421174 ASD 111-D-06 3 6532416543 32150246 30 ST 154A808
8421182 ASD 111-D-06 3 6532416543 32150246 30 ST 154A814
8421239 ASD 111-E-05 3 6532414797 32153499 25 ST 154A873
8421242 ASD 111-E-05 3 6532414797 32153499 25 ST 154A875
8421036 ASD 111-F-01 2 6532411757 32114878 30 ST 154A672
8421013 ASD 111-F-03 3 6532408584 32012238 30 ST 154A140
8421008 ASD 111-F-03 3 6532408584 32074861 30 ST 154A150
8421003 ASD 111-F-03 3 6532408584 32150254 30 ST 154A126
8421047 ASD 111-F-05 2 6532422980 32012238 30 ST 154A681
8421183 ASD 111-F-06 1 6532414832 32153495 25 ST 154A816
8421187 ASD 111-F-06 1 6532414832 32153495 25 ST 154A820
8421191 ASD 111-F-06 1 6532414832 32153495 25 ST 154A823
8421028 ASD 111-F-06 2 6532403825 32150255 30 ST 154A130
8421221 ASD 111-F-06 3 6532417790 32153494 30 ST 154A854
8421204 ASD 111-F-06 3 6532417790 32153494 30 ST 154A837
8421217 ASD 111-F-06 3 6532417790 32153494 30 ST 154A853
8421208 ASD 111-F-06 3 6532417790 32153494 30 ST 154A841
8421225 ASD 111-F-06 3 6532417790 32153494 30 ST 154A858
8421212 ASD 111-F-06 3 6532417790 32153494 30 ST 154A845
8421213 ASD 111-F-06 3 6532417790 32153494 30 ST 154A849
8421107 ASD 111-G-04 1 6532411911 32153499 25 ST 154A738
8421111 ASD 111-G-04 1 6532411911 32153499 25 ST 154A745
8421119 ASD 111-G-04 2 6532411913 32153499 25 ST 154A753
8421135 ASD 111-G-04 2 6532411913 32153499 25 ST 154A769
8421124 ASD 111-G-04 2 6532411913 32153499 25 ST 154A755
8421127 ASD 111-G-04 2 6532411913 32153499 25 ST 154A761
8421131 ASD 111-G-04 2 6532411913 32153499 25 ST 154A763
8421115 ASD 111-G-04 2 6532411913 32153499 25 ST 154A748
8421139 ASD 111-G-04 2 6532411913 32153499 25 ST 154A771
8421091 ASD 111-G-06 1 6532411761 32153494 30 ST 154A723
8421095 ASD 111-G-06 1 6532411761 32153494 30 ST 154A729
8421097 ASD 111-G-06 1 6532411761 32153494 30 ST 154A733
8421075 ASD 111-G-06 1 6532411761 32153494 30 ST 154A707
8421083 ASD 111-G-06 1 6532411761 32153494 30 ST 154A716
8421082 ASD 111-G-06 3 6532416505 32113181 150 ST 154A715
8421219 ASD 111-G-06 3 6532416505 32153495 25 ST 154A851
8421223 ASD 111-G-06 3 6532416505 32153495 25 ST 154A856
8421227 ASD 111-G-06 3 6532416505 32153495 25 ST 154A860
8421230 ASD 111-G-06 3 6532416505 32153495 25 ST 154A864
8421089 ASD 111-G-06 3 6532416505 32113181 150 ST 154A722
8421206 ASD 111-G-06 3 6532416505 32153495 25 ST 154A840
8421209 ASD 111-G-06 3 6532416505 32153495 25 ST 154A843
8421214 ASD 111-G-06 3 6532416505 32153495 25 ST 154A846
8421020 ASD 111-G-07 1 6532414831 31983630 20 ST 154A139
8421035 ASD 111-G-07 1 6532414831 31983630 20 ST 154A666
8421179 ASD 111-G-07 1 6532414831 32153495 25 ST 154A810
8421018 ASD 111-G-07 1 6532414831 31850999 30 ST 154A137
8421026 ASD 111-H-04 1 6532411763 32150250 30 ST 154A129
8421031 ASD 111-H-05 1 6532392763 32150252 30 ST 154A146
8421038 ASD 111-H-05 1 6532392763 32150256 30 ST 154A674
8421029 ASD 111-H-05 1 6532392763 31814606 250 ST 154A152
8421015 ASD 111-H-05 1 6532392763 32150256 30 ST 154A141
8421041 ASD 111-H-06 2 6532411917 32150250 30 ST 154A678
8421134 ASD 111-H-07 2 6532416534 32150246 30 ST 154A768
8421200 ASD 111-H-07 2 6532416534 32153494 30 ST 154A833
8421130 ASD 111-H-07 2 6532416534 32150246 30 ST 154A765
8421189 ASD 111-H-07 2 6532416534 32153494 30 ST 154A822
8421138 ASD 111-H-07 2 6532416534 32150246 30 ST 154A770
8421142 ASD 111-H-07 2 6532416534 32150246 30 ST 154A775
8421193 ASD 111-H-07 2 6532416534 32153494 30 ST 154A829
8421126 ASD 111-H-07 2 6532416534 32150246 30 ST 154A759
8421146 ASD 111-H-07 2 6532416534 32150246 30 ST 154A780
8421150 ASD 111-H-07 2 6532416534 32150246 30 ST 154A785
8421247 ASD 111-H-07 3 6532416523 32153495 25 ST 154A880
8421248 ASD 111-H-07 3 6532416523 32153495 25 ST 154A881
8421245 ASD 111-H-07 3 6532416523 32153495 25 ST 154A877
8421243 ASD 111-H-07 3 6532416523 32153495 25 ST 154A874
8421249 ASD 111-H-07 3 6532416523 32153495 25 ST 154A882
8421234 ASD 111-H-07 3 6532416523 32153495 25 ST 154A866
8421236 ASD 111-H-07 3 6532416523 32153495 25 ST 154A869
8421057 ASD 111-H-07 3 6532416523 31983630 20 ST 154A695
8421065 ASD 111-H-07 3 6532416523 31983630 20 ST 154A699
8421240 ASD 111-H-07 3 6532416523 32153495 25 ST 154A872
8421053 ASD 111-I-03 1 6532411919 32150250 30 ST 154A687
8421025 ASD 111-I-04 1 6532411924 32153499 25 ST 154A133
8421034 ASD 111-I-07 1 6532411888 32012238 30 ST 154A668
8421098 ASD 111-I-07 2 6532414829 32150246 30 ST 154A732
8421106 ASD 111-I-07 2 6532414829 32150246 30 ST 154A739
8421068 ASD 111-I-07 2 6532414829 32114882 30 ST 154A701
8421073 ASD 111-I-07 2 6532414829 32150246 30 ST 154A704
8421102 ASD 111-I-07 2 6532414829 32150246 30 ST 154A734
8421110 ASD 111-I-07 2 6532414829 32150246 30 ST 154A742
8421195 ASD 111-I-07 3 6532414834 32153495 25 ST 154A827
8421198 ASD 111-I-07 3 6532414834 32153495 25 ST 154A831
8421016 ASD 111-J-04 1 6532411934 32114878 30 ST 154A138
8421064 ASD 111-J-04 1 6532411934 32150250 30 ST 154A697
8421069 ASD 111-J-04 1 6532411934 32150250 30 ST 154A705
8421085 ASD 111-J-04 2 6532411935 32150250 30 ST 154A718
8421092 ASD 111-J-04 2 6532411935 32150250 30 ST 154A724
8421022 ASD 111-J-04 2 6532411935 32150743 30 ST 154A131
8421078 ASD 111-J-04 2 6532411935 32150250 30 ST 154A713
8421067 ASD 111-J-04 3 6532411936 32153499 25 ST 154A154A
8421079 ASD 111-J-04 3 6532411936 32153499 25 ST 154A709
8421087 ASD 111-J-04 3 6532411936 32153499 25 ST 154A719
8421039 ASD 111-J-04 3 6532411936 32153499 25 ST 154A669
8421054 ASD 111-J-04 3 6532411936 32153499 25 ST 154A686
8421070 ASD 111-J-05 2 6532414827 32113181 150 ST 154A706
8421246 ASD 111-J-05 2 6532414827 32153499 25 ST 154A879
8421147 ASD 111-J-05 2 6532414827 32153495 25 ST 154A781
8421171 ASD 111-J-05 2 6532414827 32153495 25 ST 154A802
8421155 ASD 111-J-05 2 6532414827 32153495 25 ST 154A786
8421167 ASD 111-J-05 2 6532414827 32153495 25 ST 154A799
8421175 ASD 111-J-05 2 6532414827 32153495 25 ST 154A806
8421052 ASD 111-J-05 2 6532414827 31983630 20 ST 154A684
8421151 ASD 111-J-05 2 6532414827 32153495 25 ST 154A782
8421244 ASD 111-J-05 2 6532414827 32153499 25 ST 154A878
8421160 ASD 111-J-05 2 6532414827 32153495 25 ST 154A792
8421164 ASD 111-J-05 2 6532414827 32153495 25 ST 154A796
8421062 ASD 111-J-05 2 6532414827 32113181 150 ST 154A696
8421055 ASD 111-J-07 3 6532419615 32150743 30 ST 154A688
8421042 ASD 111-K-02 3 6532411892 32153494 30 ST 154A675
8421024 ASD 111-K-02 3 6532411892 32153494 30 ST 154A135
8421051 ASD 111-L-01 1 6532411938 32153494 30 ST 154A685
8421103 ASD 111-L-01 1 6532411938 32153499 25 ST 154A735
8421099 ASD 111-L-01 1 6532411938 32153499 25 ST 154A731
8421094 ASD 111-L-01 1 6532411938 32153499 25 ST 154A727
8421059 ASD 111-L-01 1 6532411938 32153494 30 ST 154A694
8421066 ASD 111-L-01 1 6532411938 32153494 30 ST 154A700
8421203 ASD 111-L-03 1 6532413444 32153499 25 ST 154A834
8421021 ASD 111-L-03 1 6532413444 32113181 150 ST 154A136
8421101 ASD 111-L-03 2 6532413446 32153494 30 ST 154A737
8421109 ASD 111-L-03 2 6532413446 32153494 30 ST 154A744
8421129 ASD 111-L-03 2 6532413446 32153494 30 ST 154A762
8421153 ASD 111-L-03 2 6532413446 32153494 30 ST 154A788
8421141 ASD 111-L-03 2 6532413446 32153494 30 ST 154A774
8421117 ASD 111-L-03 2 6532413446 32153494 30 ST 154A751
8421104 ASD 111-L-03 2 6532413446 32153495 25 ST 154A736
8421125 ASD 111-L-03 2 6532413446 32153494 30 ST 154A760
8421137 ASD 111-L-03 2 6532413446 32153494 30 ST 154A773
8421113 ASD 111-L-03 2 6532413446 32153494 30 ST 154A746
8421149 ASD 111-L-03 2 6532413446 32153494 30 ST 154A784
8421105 ASD 111-L-03 2 6532413446 32153494 30 ST 154A740
8421100 ASD 111-L-03 2 6532413446 32153495 25 ST 154A730
8421121 ASD 111-L-03 2 6532413446 32153494 30 ST 154A754
8421133 ASD 111-L-03 2 6532413446 32153494 30 ST 154A767
8421145 ASD 111-L-03 2 6532413446 32153494 30 ST 154A778
8421180 ASD 111-L-03 3 6532413410 32153499 25 ST 154A811
8421176 ASD 111-L-03 3 6532413410 32153499 25 ST 154A807
8421184 ASD 111-L-04 2 6532413418 32153499 25 ST 154A815
8421037 ASD 111-L-04 2 6532413418 32113181 150 ST 154A670
8421199 ASD 111-L-04 2 6532413418 32153499 25 ST 154A832
8421050 ASD 111-L-04 2 6532413418 32113181 150 ST 154A683
8421192 ASD 111-L-04 2 6532413418 32153499 25 ST 154A824
8421188 ASD 111-L-04 2 6532413418 32153499 25 ST 154A818
8421196 ASD 111-L-04 2 6532413418 32153499 25 ST 154A828
8421027 ASD 111-L-05 1 6532413420 32150248 30 ST 154A127
8421007 ASD 111-L-05 1 6532413420 32150254 30 ST 154A665
8421010 ASD 111-L-05 1 6532413420 32114882 30 ST 154A144
8421045 ASD 111-L-05 1 6532413420 32150248 30 ST 154A677
8421006 ASD 111-L-05 2 6532413393 32150254 30 ST 154A664
8421096 ASD 111-L-06 1 6532413380 32153495 25 ST 154A728
8421148 ASD 111-L-06 1 6532413380 32153499 25 ST 154A779
8421152 ASD 111-L-06 1 6532413380 32153499 25 ST 154A783
8421046 ASD 111-L-06 1 6532413380 32153495 25 ST 154A676
8421143 ASD 111-L-06 1 6532413380 32153499 25 ST 154A776
8421081 ASD 111-L-06 1 6532413380 32153495 25 ST 154A712
8421060 ASD 111-L-06 1 6532413380 32153495 25 ST 154A689
8421072 ASD 111-L-06 1 6532413380 32153495 25 ST 154A702
8421088 ASD 111-L-06 1 6532413380 32153495 25 ST 154A721
8421168 ASD 111-L-06 1 6532413380 32153499 25 ST 154A801
8421172 ASD 111-L-06 1 6532413380 32153499 25 ST 154A804
8421156 ASD 111-L-06 1 6532413380 32153499 25 ST 154A787
8421159 ASD 111-L-06 1 6532413380 32153499 25 ST 154A791
8421163 ASD 111-L-06 1 6532413380 32153499 25 ST 154A794
8421049 ASD 111-L-06 3 6532413424 32150246 30 ST 154A680
8421058 ASD 111-L-06 3 6532413424 32114878 30 ST 154A691
8421063 ASD 111-L-06 3 6532413424 32150246 30 ST 154A693
8421044 ASD 111-L-06 3 6532413424 32114882 30 ST 154A673
8421048 ASD 111-L-06 3 6532413424 32114878 30 ST 154A682
8421032 ASD 111-L-06 3 6532413424 32150246 30 ST 154A128
8421056 ASD 111-L-06 3 6532413424 32114882 30 ST 154A690
8421061 ASD 111-L-06 3 6532413424 32150248 30 ST 154A692
8421071 ASD 111-L-06 3 6532413424 32150248 30 ST 154A703
我想寻求有关 VBA 脚本的帮助,该脚本将执行以下操作:
对所有 table 排序(select 所有有值的单元格 - 包括工作人员使用但为空的 J 列),并且仅升序排序通过 "Material"(F 列),根据需要重新排列行,确保不会对数量值求和 - 排序后我需要相同数量的行。
排序完成后,我需要一个脚本来开始分析table,并在每个相同的material周围制作一个边框。或者,如果它更简单,从第 1 行开始分析,并且,如果下一行的 J 列上的以下值有任何不同(或为空),则执行 Borders(xlEdgeBottom).Weight = xlThick
我想出的 VBA 脚本如下(排序不起作用,它汇总了数量,这不好,而且还以某种方式按 SBin 排序)。 边框脚本省略了在第一行下方放置边框(在我将整个 table 按 Material 排序之后)(因为第二行具有不同的值),并且还省略了在最后一行下方放置边框,下一行的单元格将为空。
请帮助我,我们将不胜感激。
VBA代码如下:
Sub AddBorders()
' define stuff
Dim bottomN As Integer
bottomN = Range("F" & Rows.Count).End(xlUp).Row
' border all
With ActiveSheet.UsedRange.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With ActiveSheet.UsedRange.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
' convert to numbers
Range("A:A").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("D:D").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("E:E").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("F:F").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Range("I:I").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
' sorting --- not working as intented yet
'ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
'ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("F1:F" & bottomN) _
' , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
' xlSortTextAsNumbers
'With ActiveWorkbook.Worksheets("Sheet1").Sort
' .SetRange Range("F1:F" & bottomN)
' .Header = xlYes
' .MatchCase = False
' .Orientation = xlTopToBottom
' .SortMethod = xlPinYin
' .Apply
'End With
' start thick borders
Dim startRow As Integer
Dim iRow As Integer
startRow = 1
For iRow = 2 To ActiveCell.CurrentRegion.Rows.Count
If WorksheetFunction.IsNumber(Cells(iRow - 1, 6)) Then
If Cells(iRow, 6) <> Cells(iRow - 1, 6) Then
AddBorder startRow, iRow - 1
startRow = iRow
End If
Else
AddBorder startRow, iRow
End If
Next iRow
End Sub
Sub AddBorder(startRow As Integer, endRow As Integer)
Dim borderRange As Range
Dim randomColor As Integer
randomColor = Int((56 * Rnd) + 1)
' tweaked for the same row borders
Set borderRange = Range("A" & endRow & ":J" & endRow)
'borderRange.BorderAround ColorIndex:=0, Weight:=xlThick
borderRange.Borders(xlEdgeBottom).Weight = xlThick
End Sub
提前谢谢你。如果我能在你的帮助下解决这个问题,这对我来说意义重大。
- 大卫.
排序脚本
Sub Macro1()
Range("A1:J" & Range("A" & Rows.Count).End(xlUp).Row).Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range_
("F1:F" & Range("A" & Rows.Count).End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
我会这样做的:
' define stuff
Dim ws As Worksheet
Set ws = ActiveSheet 'never like to use "Active" option. it is much much better to precisely define your workbook and worksheets
Dim bottomN As Integer
bottomN = Range("F" & Rows.Count).End(xlUp).Row
' border all
ws.UsedRange.Borders.LineStyle = xlContinuous
With ws.Sort
With .SortFields
.Clear
.Add Key:=Range("F2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
.SetRange Range("A2:I248")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' start thick borders
Dim startRow As Integer
Dim iRow As Integer
startRow = 1
For iRow = 2 To bottomN
If WorksheetFunction.IsNumber(Cells(iRow - 1, 6).Value) = True Then
If StrComp(CStr(Cells(iRow, 6)), CStr(Cells(iRow - 1, 6))) <> 0 Then
AddBorder startRow, iRow - 1
startRow = iRow
End If
Else
AddBorder startRow, iRow
End If
Next iRow
谢谢大家!
我采用了 izzymo 制作的排序脚本和 PolaEla 制作的一些脚本,并设法完成了脚本并使其执行我需要的操作。
非常感谢您的帮助!