2016-04-06 25 views
0

Merhaba Guys VBA Excel'e yeni katılmışım. Bu şu ana kadar sahip olduğum kod. Üç girişli bir combobox var 1.Part # 2. Ay (Icol) 3. Tutar. Kombobox'ımın A sütununa bakmasını ve bu parçayı satırın altına eklemek istemediğimde parçanın bulunup bulunmadığını görmek istiyorum. Her şey, A sütununa yeni bir parça eklenmediği sürece iyi sonuç verir. Parça, satırın altına eklenir, ancak değer bölümü bunu yapmaz ve bu, kullanıcının eski seçtiği aya karşılık gelmelidir. Yeni parça "Current ay + 1 = N, 200. Bu nedenle çizgi değeri içincombobox ile girilen veriler nasıl eklenir ve excel dosyasına eklenir

Private Sub cmdAdd_Click()

Dim iRow As Long 
Dim lastRow As Long 
Dim iCol As String 
Dim c As Range 
Dim ws As Worksheet 
Dim value As Long 
Dim NewPart As Boolean 
Set ws = Worksheets("sheet2") 

Set c = ws.Range("A7:A1048576").Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _ 
     SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole) 
If c Is Nothing Then 
'find first empty row in database 
    lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count 
    iRow = lastRow + 1 
    NewPart = True 
Else 
'find row where the part is 
    iRow = ws.Cells.Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _ 
     SearchDirection:=xlPrevious, LookIn:=xlValues).Row 
    NewPart = False 
End If 
'check for a part number 
If Trim(Me.PartTextBox.value) = "" Then 
    Me.PartTextBox.SetFocus 
    MsgBox "Please Enter A Part Number" 
    Exit Sub 
End If 

If Trim(Me.MonthComboBox.value) = "" Then 
    Me.MonthComboBox.SetFocus 
    MsgBox "Please Enter A Month" 
    Exit Sub 
End If 

If Trim(Me.AddTextBox.value) = "" Then 
    Me.AddTextBox.SetFocus 
    MsgBox "Please Enter A Value To Add Or Substract" 
    Exit Sub 
End If 

'copy the data to the database 
'use protect and unprotect lines, 
'  with your password 
'  if worksheet is protected 

Select Case MonthComboBox.value 

    Case "Current Month" 

     iCol = "C" 

    Case "Current Month +1" 

     iCol = "N" 

    Case "Current Month +2" 

     iCol = "O" 

    Case "Current Month +3" 

     iCol = "P" 

    Case "Current Month +4" 

     iCol = "Q" 



End Select 
value = Cells(iRow, iCol).value 

If NewPart = True Then 
    ws.Cells(iRow, "A").value = Me.PartTextBox.value 
    ws.Cells(iRow, iCol).value = value + CLng(Me.AddTextBox.value) 


End If 



With ws 
' .Unprotect Password:="password" 

    .Cells(iRow, iCol).value = value + CLng(Me.AddTextBox.value) 

' .Protect Password:="password" 
End With 




'clear the data 
Me.PartTextBox.value = "" 
Me.MonthComboBox.value = "" 
Me.AddTextBox.value = "" 
Me.PartTextBox.SetFocus 

End Sub 

Private Sub cmdClose_Click() 
Unload Me 
End Sub 


Private Sub UserForm_Initialize() 

'Empty NameTextBox 
PartTextBox.value = "" 

'Empty PhoneTextBox 
AddTextBox.value = "" 

'Empty DinnerComboBox 

'Fill DinnerComboBox 
With MonthComboBox 
    .AddItem "Current Month" 
    .AddItem "Current Month +1" 
    .AddItem "Current Month +2" 
    .AddItem "Current Month +3" 
    .AddItem "Current Month +4" 

End With 

End Sub 

cevap

0

yeni parça eklemek ve sütun N'ye ileri 200 = Hücreleri (IRow, iCol) referans değil harfler olarak tam sayılar olması gerekir .value

Yani seçme açıklamada numaralarını kullanmak gerekir.

Select Case MonthComboBox.value 

Case "Current Month" 

    iCol = 3 

Case "Current Month +1" 

    iCol = 14 

Case "Current Month +2" 

    iCol = 15 

Case "Current Month +3" 

    iCol = 16 

Case "Current Month +4" 

    iCol = 17 


End Select 
+0

Maalesef bu sorun olmadığından emin değilse numaralara iCol geçerek. Statemde hatalar alıyorum ents ve ayrıca sanırım bu durum böyle değil çünkü bölüm A sütununda bulunduğunda Select Case çalışıyor. Seçme durumu, satırın altına yeni bir parça eklendiğinde çalışmaz. – Luis

+0

@INOPIAE dize değeri, 'Cells' nesnesi tarafından kabul edilir. Sadece 'yazın? Hücrelerini (1, "C") derhal penceresinde. Address' – user3598756

+0

Select Case MonthComboBox.value Vaka "Geçerli Ay" iCol = Hücreler (3, "C"). Kılıfı Adres "Mevcut Ay + 1" iCol = Hücreler (14, "K"), geçerli ay + 2. Örneği Adres "" iCol = hücreler (15, "O") "Mevcut ay +3. Örneği Adres" iCol = Hücreler (16, "P") Adres Durum "Geçerli Ay +4" iCol = Hücreler (17, "Q") Adres – Luis

İlgili konular