2017-06-06 64 views
-1

嗨我是VBA的新手,我有一個問題,我需要將所有項目SKU添加到由一個列分隔的單個列中,每個主產品可能有多個子SKU,具體取決於產品的顏色和尺寸。我已經爲100個單元寫了一個手動公式,假設最大5種尺寸和20種顏色可用。在Excel VBA中簡化嵌套字符串串聯

式是

=IF(A11="","Error",A11)&IF(A12="","",","&A12)&IF(A13="","",","&A13)&IF(A14="","",","&A14)&IF(A15="","",","&A15)&IF(A16="","",","&A16)&IF(A17="","",","&A17)&IF(A18="","",","&A18)&IF(A19="","",","&A19)&IF(A20="","",","&A20)&IF(A21="","",","&A21)&IF(A22="","",","&A22)&IF(A23="","",","&A23)&IF(A24="","",","&A24)&IF(A25="","",","&A25)&IF(A26="","",","&A26)&IF(A27="","",","&A27)&IF(A28="","",","&A28)&IF(A29="","",","&A29)&IF(A30="","",","&A30)&IF(A31="","",","&A31)&IF(A32="","",","&A32)&IF(A33="","",","&A33)&IF(A34="","",","&A34)&IF(A35="","",","&A35)&IF(A36="","",","&A36)&IF(A37="","",","&A37)&IF(A37="","",","&A37)&IF(A38="","",","&A38)&IF(A39="","",","&A39)&IF(A40="","",","&A40)&IF(A41="","",","&A41)&IF(A42="","",","&A42)&IF(A43="","",","&A43)&IF(A44="","",","&A44)&IF(A45="","",","&A45)&IF(A46="","",","&A46)&IF(A47="","",","&A47)&IF(A48="","",","&A48)&IF(A49="","",","&A49)&IF(A50="","",","&A50)&IF(A51="","",","&A51)&IF(A52="","",","&A52)&IF(A53="","",","&A53)&IF(A54="","",","&A54)&IF(A55="","",","&A55)&IF(A56="","",","&A56)&IF(A57="","",","&A57)&IF(A58="","",","&A58)&IF(A59="","",","&A59)&IF(A60="","",","&A60)&IF(A61="","",","&A61)&IF(A62="","",","&A62)&IF(A63="","",","&A63)&IF(A64="","",","&A64)&IF(A65="","",","&A65)&IF(A66="","",","&A66)&IF(A67="","",","&A67)&IF(A68="","",","&A68)&IF(A69="","",","&A69)&IF(A70="","",","&A70)&IF(A71="","",","&A71)&IF(A72="","",","&A72)&IF(A73="","",","&A73)&IF(A74="","",","&A74)&IF(A75="","",","&A75)&IF(A76="","",","&A76)&IF(A77="","",","&A77)&IF(A78="","",","&A78)&IF(A79="","",","&A79)&IF(A80="","",","&A80)&IF(A81="","",","&A81)&IF(A82="","",","&A82)&IF(A83="","",","&A83)&IF(A84="","",","&A84)&IF(A85="","",","&A85)&IF(A86="","",","&A86)&IF(A87="","",","&A87)&IF(A88="","",","&A88)&IF(A89="","",","&A89)&IF(A90="","",","&A90)&IF(A91="","",","&A91)&IF(A92="","",","&A92)&IF(A93="","",","&A93)&IF(A94="","",","&A94)&IF(A95="","",","&A95)&IF(A96="","",","&A96)&IF(A97="","",","&A97)&IF(A98="","",","&A98)&IF(A99="","",","&A99)&IF(A100="","",","&A100) 

是有辦法來自動化式,使得它保持加在列A中的所有值在列B中的細胞,直到它到達與空值的小區。

基本上主產物

SKU 
MS-19954141 

具有子產品

MS-19954141_gray-blue_66cm/3-6 months 
MS-19954141_gray-blue_73cm/6-9 months 
MS-19954141_gray-blue_80cm/9-12 months 
MS-19954141_gray-blue_90cm/12-24 months 
MS-19954141_white_66cm/3-6 months 
MS-19954141_white_73cm/6-9 months 
MS-19954141_white_80cm/9-12 months 
MS-19954141_white_90cm/12-24 months 

這些子產品必須被加在一起,並作爲

MS-19954141_gray-blue_66cm/3-6 months, MS-19954141_gray-blue_73cm/6-9 months, MS-19954141_gray-blue_80cm/9-12 months, MS-19954141_gray-blue_90cm/12-24 months, MS-19954141_white_66cm/3-6 months, MS-19954141_white_73cm/6-9 months, MS-19954141_white_80cm/9-12 months, MS-19954141_white_90cm/12-24 months 

因此,我們採取第一列B中提及子產品添加一個「,」,然後第二個子產品添加一個「,」如果列A是空白等那麼將不會添加數據。我所做的是一個非常粗糙的方法,我們不知道有多少個子產品會在那裏。我已經編寫了添加到第100行的代碼,但在某些情況下可能會超過200個子產品。 20種顏色X 10種尺寸。所以尋找替代

+0

哇 - 什麼是你想怎麼辦?你正在尋找一個非常大的'TRUE'語句......這很不清楚,你能發佈一些樣本數據並澄清你想要做什麼/輸出嗎? – BruceWayne

+0

我已編輯帖子 – iSrini

回答

0

試試這個代碼:

Dim sht As Worksheet 
Dim Start_Cell As Range 
Dim Output_Cell As Range 
Dim i As Long 
Dim Result As String 
Dim SKU As String 

Set sht = ThisWorkbook.Worksheets("MySheet") 'Replace MySheet with the name of the sheet with data 
Set Start_Cell = sht.Range("A11") 
Set Output_Cell = sht.Range("B1") 

i = 0 
Result = "" 
SKU = Start_Cell 

Do Until SKU = "" 
    Result = Result & SKU & "," 
    i = i + 1 
    SKU = Start_Cell.Offset(i) 
Loop 

If Result <> "" Then Result = Left(Result, Len(Result) - 1) 'Removes last comma 
Output_Cell = Result