简体   繁体   English

在Excel VBA中插入空白行

[英]Inserting Blank Rows in Excel VBA

Hey I have been writing some code to add a part ID to a spreadsheet off of a user form in Excel VBA. 嘿,我一直在编写一些代码,以将零件ID添加到Excel VBA中的用户表单之外的电子表格中。 I have been reading through different documentation and can not figure out why no matter what type of method of inserting a row I try it inserts a row with a repeating value instead of a blank one. 我一直在阅读不同的文档,并且无法弄清楚为什么无论哪种类型的插入行的方法,我都会尝试使用重复值而不是空值来插入行。 If anyone knows how to specify blank, other than writing the whole row to blank and then writing my numbers I want after, that would be appreciated. 如果有人知道如何指定空格,除了将整行写为空格,然后再写我想要的数字,那将不胜感激。

I have tried both the following lines to add a row 我已经尝试了以下两行来添加一行

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

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

This is the function it is used in: 这是它用于的功能:

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

Bellow is the function that calls the Add Function and where I belive it may be getting the company name from 波纹管是调用添加函数的函数,我相信它可能是从

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

I know that the repeating pattern is coming from the insert line through debugging but I can not figure out why I have tried changing variables to numbers and it still did the same thing. 我知道重复模式是通过调试从插入行产生的,但是我无法弄清楚为什么我尝试将变量更改为数字,但仍然执行相同的操作。 This what it looks like with the repeating values. 这就是重复值的样子。 enter image description here 在此处输入图片说明

The problem is that you most likely have a value still stored in your clipboard when you execute the Macro. 问题是您在执行宏时很可能仍将值存储在剪贴板中。 To fix that, simply add this line of dode before running the insert line: 要解决此问题,只需在运行插入行之前添加以下一行dode:

Applcation.CutCopyMode = False Applcation.CutCopyMode =假

That will clear your clipboard and allow the inserted rows to be blank. 这将清除剪贴板,并允许插入的行为空白。

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

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