繁体   English   中英

在Excel VBA中插入空白行

[英]Inserting Blank Rows in Excel VBA

嘿,我一直在编写一些代码,以将零件ID添加到Excel VBA中的用户表单之外的电子表格中。 我一直在阅读不同的文档,并且无法弄清楚为什么无论哪种类型的插入行的方法,我都会尝试使用重复值而不是空值来插入行。 如果有人知道如何指定空格,除了将整行写为空格,然后再写我想要的数字,那将不胜感激。

我已经尝试了以下两行来添加一行

像元(x + 1,列).EntireRow.Insert Shift:= xlDown

ws1.Rows(x + 1)。插入Shift:= xlDown

这是它用于的功能:

Public Sub Add(IDRange As Range)
          SearchCell = Cells(x, IDRange.Column)
          Cells(x, IDRange.Column).Select
          Do
               If SearchCell = PartID Then
                    MsgBox " this Company Already uses this part"
                    Exit Sub
               ElseIf x <> StopRow Then
                    x = x + 1
                    SearchCell = Cells(x, IDRange.Column)
               End If
          Loop While x <> StopRow And SearchCell <> PartID
          Cells(x + 1, IDRange.Column).EntireRow.Insert Shift:=xlDown
          Cells(x, IDRange.Column).Value = PartID
          MsgBox PartID & " has been added to Adress " & Cells(x, IDRange.Column).Address
          Cells(x, IDRange.Column).Select
End Sub

波纹管是调用添加函数的函数,我相信它可能是从

Private Sub AddPart_Click()
AddPartCounter = 0
Company = UserForm1.CompanyBox.Value
PartID = UserForm1.PartBox.Value

If Company = "" Then
     MsgBox " Please put in the company you would like the part to go under"
ElseIf PartID = "" Then
     MsgBox " Please put in the Part you would like entered"
ElseIf UserForm1.Studs.Value = False And UserForm1.Spreaders.Value = False And UserForm1.Blocks.Value = False And UserForm1.Imma.Value = False Then
     MsgBox "Please select the type of part you are trying to add"
Else
     Dim CurrentCell
     Set CurrentCell = Cells.Find(What:=Company, LookAt:=xlWhole)
     If CurrentCell Is Nothing Then
          MsgBox " Company Not Found "
          Exit Sub
     End If
     x = CurrentCell.Row
     Do
          Set CurrentCell = CurrentCell.Offset(1, 0)
     Loop While CurrentCell.Offset(1, 0) = "" And Not CurrentCell Is Nothing And CurrentCell.Offset(1, 0).Row <> thisvar.Row + 1
     StopRow = CurrentCell.Row

     'If they are trying to add a nut
     If UserForm1.Imma.Value = True Then
          Call Add(Nut_ID_Rng)

     'IF they are trying to add a stud
     ElseIf UserForm1.Studs.Value = True Then
          Call Add(Stud_ID_Rng)

     'If they are trying to add a block
     ElseIf UserForm1.Blocks.Value = True Then
          Call Add(Block_ID_Rng)
     'If they are trying to add a spreader
     ElseIf UserForm1.Spreaders.Value = True Then
          Call Add(Spreader_ID_Rng)
     End If
End If
AddPartCounter = 1
End Sub

我知道重复模式是通过调试从插入行产生的,但是我无法弄清楚为什么我尝试将变量更改为数字,但仍然执行相同的操作。 这就是重复值的样子。 在此处输入图片说明

问题是您在执行宏时很可能仍将值存储在剪贴板中。 要解决此问题,只需在运行插入行之前添加以下一行dode:

Applcation.CutCopyMode =假

这将清除剪贴板,并允许插入的行为空白。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM