Excel 2010 VBA / MACRO查找字段值为自动创建语句

时间:2013-10-05 09:16:26

标签: excel excel-vba excel-2010 vba

我需要一个宏/ vba,它可以根据excel中的值自动创建一个选择雄蕊。示例如下。查询可以硬编码为'SELECT * FROM PRDTBL WHERE'其余部分应该由宏处理,它将基于A01,A02和A03中的值。如果其中一个值为空,则忽略。任何人都可以给我一个关于如何实现它的想法/链接?感谢

ID      QUERY                                                                   A01     A02     A03     Remark
1001    SELECT * FROM PRDTBL WHERE A01='ASIA' AND A02='MACHINE' AND A03='G1234' ASIA    MACHINE G1234   
1001    SELECT * FROM PRDTBL WHERE A01='EURO' AND A02='MACHINE' AND A03='F101'  EURO    MACHINE F101    
1004    SELECT * FROM PRDTBL WHERE A01='EURO' AND A03='ASS1'                    EURO    ASS1    
1003    SELECT * FROM PRDTBL WHERE A01='LATIN' AND A02='FURNITURE'              LATIN   FURNITURE       
1005    SELECT * FROM PRDTBL WHERE A01='AUS'                                    AUS         

1 个答案:

答案 0 :(得分:0)

对于像这样快的东西,我只想创建一个公式并使用"&"用于将字符串连接在一起的符号:

="SELECT * FROM PRDTBL WHERE "&IF(B1="","","A01 = '"&B1&"'")&IF(C1="","",IF(B1<>""," AND ","")&"A02 = '"&C1&"'")&IF(D1="","",IF(OR(B1<>"",C1<>"")," AND ","")& "A03 = '"&D1&"'")