2017-06-30 14 views
1

私はこの問題をしばらく取り組んできました。 CheckBox3とCheckBox6の両方がチェックされると、必要なときに " - "が複製されます。私はレンを使用しようとしましたが、配列を使って重複を削除しましたが、どちらも私のためのトリックはありませんでした。私はまだVBAの初心者なので、おそらくそれを間違っているだけです。とにかく、私のコードは次のとおりです。セル内の文字列から重複を取り除く

Private Sub btnOK_Click() 
Dim strText As String, strDelimiter As String 

strDelimiter = " " 

If cbxDD.Value = "DD" Or cbxMM.Value = "MM" Then 
    MsgBox "Please enter both a month and date.", , "Invalid Entry" 
    Exit Sub 
End If 

If CheckBox4.Value = True Then strText = strText & "DR" & strDelimiter 
If CheckBox5.Value = True Then strText = strText & "C" & strDelimiter 
If CheckBox2.Value = True Then strText = strText 
If CheckBox1.Value = True Then strText = strText & ChrW(8730) & strDelimiter 
If CheckBox3.Value = True Then strText = strText & "-" & strDelimiter 
If CheckBox6.Value = True Then strText = strText & "- CP" & strDelimiter 
If CheckBox7.Value = True Then strText = strText & "Cancelled" & strDelimiter 
If CheckBox9.Value = True Then strText = strText & "TS" & strDelimiter 
If CheckBox8.Value = True Then strText = strText & "Lost" & strDelimiter 

If Len(strText) > 0 = True Then 
    strText = Left(strText, Len(strText) - Len(strDelimiter)) 'remove trailing delimiter 
    ActiveCell.Value = cbxMM.Value & "/" & cbxDD.Value & " " & txtCode.Value & " " & strText 
    Unload Me 
Else 
    MsgBox "No Status selected.", , "Invalid Entry" 
End If 

If CheckBox2.Value Or CheckBox4.Value Or CheckBox5.Value = True Then 
    ActiveCell.Value = "x" & ActiveCell.Value 
Else 
    ActiveCell.Value = ActiveCell.Value 
End If  
End Sub 

助けがあれば助かります!ありがとう。

答えて

3

[置換]コマンドを使用して、必要な処理を実行できます。ただ、このコードを追加します。

strtext = Replace(strtext, "--", "-") 
+0

Ah!私は 'If'ステートメントであまりにも多くの時間を費やしましたが、その部分を書き直した後で同じことを実現した後に...素早い考え方:D – BruceWayne

+0

うわー。私はそれがとても簡単だろうとは思わなかった!ありがとう! – JerrySein

2

私はコードを正しく理解していれば、おそらくこの2つのラインIf Checkbox3.Value ...If CheckBox6.Value...を置き換える(これを試してみてください。

If CheckBox6.Value = True and Checkbox3.Value = False Then 
    strText = strText & "- CP" & strDelimiter 
ElseIf CheckBox3.Value = True and CheckBox6.Value = False Then 
    strText = strText & "-" & strDelimiter 
ElseIf CheckBox3.Value = True and CheckBox6.Value = True Then 
    strText = strText & "0 CP" & strDelimiter 

あるいは、実際に、その全体Ifブロックの後の最初の、

strText = Replace(strText,"--","-")

を試すか、に戻って2 -はない場合2番目の引数を微調整するだけで、おそらくstrText = Replace(strText,"- -","-")または...strText," - - ","-") ...

+0

ありがとうございます!私はあなたにupvoteを与えるだろうが、私はまだそれを行う特権を持っていない。 – JerrySein

関連する問題