You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA Userform无法向指定工作表写入数据仅新增空行求助

VBA Userform写入指定工作表仅添加空行问题求助

问题描述

我是VBA初学者,开发Userform时遇到故障:制作的用于向工作表指定区域填充数据的Userform,在数天前刚开发完成时可正常运行,目前运行时仅会向目标表格中添加空行,花费数小时排查仍未定位错误原因。

异常特征

  • 修改Userform的写入目标工作表后,功能可正常运行
  • 仅写入预期的指定工作表时,会出现仅添加空行的问题

参考截图

Userform界面截图

完整实现代码

Private Sub CommandButton2_Click()
Dim dl As Integer
Dim list_num As Integer
Dim ligne As Integer

list_num = Me.liste_com3.ListCount - 1

If Me.liste_com3.ListCount > 0 Then 'contrôl si la liste n'est pas vide
    
    If MsgBox("Voulez-vous enregistrer cette transaction ?", vbYesNo) = vbYes Then
    
        For ligne = 0 To list_num
        
            'ajouter nouvelle ligne dans le tableau
            Sheets(8).ListObjects(1).ListRows.Add
            'chercher numéro prochaine ligne du tableau
            dl = Sheets(8).Range("b9999").End(xlUp).Row
            
            'ajouter les infos dans la bdd
            Sheets(8).Range("Z" & dl) = Me.info1
            Sheets(8).Range("C" & dl) = Format(Me.txt_fac3, """FAC-""00000")
            Sheets(8).Range("D" & dl) = Format(CDate(Now()), "dd/mm/yyyy hh:mm:ss")
            Sheets(8).Range("E" & dl) = Me.cbx_com
            
            'controler si c'est un fournisseur ou un client
            If Me.label_type = "Fournisseur :" Then
                Sheets(8).Range("F" & dl) = Me.cbx_type3
            Else
                Sheets(8).Range("G" & dl) = Me.cbx_type3
            End If
            
            'ajouter les données de la zone de liste
            Sheets(8).Range("H" & dl) = Me.liste_com3.List(ligne, 0)
            Sheets(8).Range("J" & dl) = Int(Me.liste_com3.List(ligne, 1))
        Next ligne
            
            MsgBox "Enregistrement réussi !"
            Unload Me
            ThisWorkbook.Save
    End If
         
End If

Sheets(8).Range("K6:N9999").NumberFormat = "#,##0 [$XOF]"

End Sub

Private Sub liste_com3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If Me.liste_com3.ListIndex >= 0 Then
    If MsgBox("Voulez-vous supprimer cette entrée ?", vbYesNo) = vbYes Then
        Me.liste_com3.RemoveItem Me.liste_com3.ListIndex
        memo = memo - 1
    End If
End If
End Sub

Private Sub opt_in_Click()
Me.label_type = "Fournisseur :"
Me.cbx_type3.RowSource = "col_fourni"
Me.cbx_com.RowSource = "col_comm"
Me.info1 = "Entrée"
End Sub

Private Sub opt_out_Click()
Me.label_type = "Client :"
Me.cbx_type3.RowSource = "col_clients"
Me.cbx_com.RowSource = ""
Me.info1 = "Sortie"
End Sub

Private Sub txt_fac3_Change()
If Not IsNumeric(txt_fac3) And txt_fac3 <> "" Then
    MsgBox "Veuillez entrer un nombre..."
    Me.txt_fac3 = ""
End If
End Sub

Private Sub txt_num3_Change()
'controle si numérique
If Not IsNumeric(txt_num3) And txt_num3 <> "" Then
    MsgBox "Veuillez entrer un nombre..."
    Me.txt_num3 = ""
End If
End Sub

Private Sub UserForm_Initialize()
Me.label_info_3.Caption = "Mouvements"
End Sub

内容的提问来源于stack exchange,提问作者Billy_Zeke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.31 20:48:46