简体   繁体   English

VBA 每次单击宏按钮时,将用户表单中的新行和数据插入到另一个工作表中

[英]VBA Insert new row and data from user form into another sheet table each time clicks macro button

I have a manual made form(text fields) that uses vlookup to return those fields correspondant values, based on unique ID typed.我有一个手动制作的表单(文本字段),它使用 vlookup 根据键入的唯一 ID 返回这些字段对应的值。 Example: ID: 0001 (static number) Type: vlookup value1 Price: vlookup value 2 Owner: vlookp value 3 And another table that just need that ID to be inserted each time in the first table row called "ID", Example: row1 "ID" the other columns rows are returned with a vlookup call formula.示例:ID:0001(静态数字) 类型:vlookup value1 价格:vlookup value 2 所有者:vlookp value 3 还有另一个表,每次只需要将该 ID 插入到名为“ID”的第一个表行中,示例:row1“ ID”,其他列行通过 vlookup 调用公式返回。

But what i need is that each time i type new value in the form Id field(from sheet 1 the form) and i hit the Insert macro button;但我需要的是,每次我在表单 Id 字段中键入新值(来自表 1 的表单),然后我点击插入宏按钮; VBA to compare that user typed "id" to the ids found in the table "ID" column rows(sheet2) and if that ID typed is not found; VBA 将用户键入的“id”与表“ID”列行(sheet2)中找到的 id 进行比较,如果未找到键入的 ID; to insert new line with same above format and insert the new ID into the first row in that new line;插入与上述格式相同的新行,并将新 ID 插入该新行的第一行; also to check another row value in the table(called: Type, placed in separate line alone) and compare to another user form field(called: Type) value (from sheet 1 just like the ID stuff..) and if it's different values then to copy the same line format and paste the new type field 's value, then execute the first step and insert ID in new line below the "new "type" line). Actually i started with that code but it's not checking or pasting new lines below the first one or giving an error if i use Paste:=xlPasteFormats, Operation:=xlDown for example.还要检查表中的另一行值(称为:Type,单独放在单独的行中)并与另一个用户表单字段(称为:Type)值(来自工作表 1,就像 ID 内容一样......)比较,如果它是不同的值然后复制相同的行格式并粘贴新类型字段的值,然后执行第一步并在“新”类型“行下方的新行中插入 ID。实际上我是从该代码开始的,但它没有检查或粘贴如果我使用Paste:=xlPasteFormats, Operation:=xlDown则第一行下方的新行或给出错误。

Sub add_item()
'
' add_item Macro
'

'
  Sheets("AJOUTER_PV").Select
  Range("K6").Select
    Selection.Copy
    Sheets("Inventaire").Select
    Range("A5502").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  If Sheets("AJOUTER_PV").Range("K6").value <> Sheets("Inventaire").Range("A5502").value Then
  
   Range("A5577:AF5577").Select
    Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A5502").Select
    ActiveWindow.SmallScroll ToRight:=3
    Range("A5502:AL5502").Select
    Selection.Copy
   
    Range("A5577:AL5577").PasteSpecial Paste:=xlPasteFormats, Operation:=xlDown

    Application.CutCopyMode = False
    End If
End Sub

*Where Range("K6") is the Id field from user form. *其中 Range("K6") 是用户表单中的 Id 字段。
Range("A5502") is the Id field from the table where the id is going to be inserted first time in "A5502" then "A5503" etc. Range("A5502") 是表中的 ID 字段,其中 ID 将首先插入到“A5502”中,然后是“A5503”等。
**before each line of the table, there is the "Type"line (in a separate merged to one cell line), and here where we need to check types from both user and table to know if we need to insert new type line with the new data from the user form, and then insert new line with the "ID" value from the user form same formatting (+ formulas) as the ID line with the pre-made vlookup auto completed rows using "ID" reference. **在表的每一行之前,有“类型”行(在一个单独的合并到一个单元格行中),在这里我们需要检查用户和表的类型以了解我们是否需要插入新的类型行使用来自用户表单的新数据,然后使用来自用户表单的“ID”值插入新行与使用“ID”引用的预制 vlookup 自动完成行的 ID 行相同的格式(+ 公式)。 表格

Perhaps some answer is better than no answer.也许有些答案总比没有答案好。 If it isn't I apologize.如果不是我道歉。

Option Explicit

Sub Add_Item()

    ' declare all variables
    Dim WsPV        As Worksheet                ' Sheets("AJOUTER_PV")
    Dim WsInv       As Worksheet                ' Sheets("Inventaire")
    Dim Id          As Variant                  ' referenced ID
    
    ' assign values / objects to the declared variables
    Set WsPV = Worksheets("AJOUTER_PV")
    Set WsInv = Worksheets("Inventaire")
    
    ' use the Cells collection to address cells
    ' syntax is Cells([Row number], [Column number or name])
    Id = WsPV.Cells(6, "K").Value
    WsInv.Cells(5502, "A").Value = Id
    ' you could also use:- WsInv.Cells(5502, "A").Value = WsPV.Cells(6, "K").Value
    
    '  What 's this? You just made A5502 = K6!
    ' If Sheets("AJOUTER_PV").Range("K6").Value <> Sheets("Inventaire").Range("A5502").Value Then
  
    ' not clear what you intend, either from your code or you text
    ' so, here is some syntax you might adapt
    WsPV.Rows(5577).EntireRow.Insert CopyOrigin:=xlFormatFromLeftOrAbove
    WsPV.Range("A5502:AL5502").Copy Destination:=WsInv.Cells(5502, "B")
    Application.CutCopyMode = False
End Sub

The outstanding message of the above is that you don't need to Select anything.上面的突出信息是你不需要Select任何东西。 Instead, you should address the objects (worksheets, ranges, cells) and modify their properties (only their Value property is touched here).相反,您应该处理对象(工作表、范围、单元格)并修改它们的属性(此处仅涉及它们的Value属性)。 In order to address them you should declare variables and assign values and objects to them.为了解决这些问题,您应该声明变量并为它们分配值和对象。 In order not to leave anyone out you should specify Option Explicit at the top of the code sheet.为了不遗漏任何人,您应该在代码表的顶部指定Option Explicit

Updated version of code(rebuilded table "Inventaire" so actual cells are like in the updated code... but the problem with this code that actually i don't need that a cell keep equal to another but need a sequence of copy paste each time in a new row underneath "CBR" column when the code is executed.更新版本的代码(重建表“Inventaire”所以实际单元格就像更新代码中一样......但是这个代码的问题实际上我不需要一个单元格与另一个单元格保持相等但需要一系列复制粘贴每个执行代码时在“CBR”列下方的新行中的时间。

Option Explicit

Sub Add_Item()

    ' declare all variables
    Dim WsPV        As Worksheet                ' Sheets("AJOUTER_PV")
    Dim WsInv       As Worksheet                ' Sheets("Inventaire")
    Dim Id          As Variant                  ' referenced CBR
    
    ' assign values / objects to the declared variables
    Set WsPV = Worksheets("AJOUTER_PV")
    Set WsInv = Worksheets("Inventaire")
    
    ' use the Cells collection to address cells
    ' syntax is Cells([Row number], [Column number or name])
    Id = WsPV.Cells(6, "K").Value
    WsInv.Cells(4, "A").Value = Id
    ' you could also use:- WsInv.Cells(5502, "A").Value = WsPV.Cells(6, "K").Value
    
    '  What 's this? You just made A5502 = K6!
     If Sheets("AJOUTER_PV").Range("K6").Value <> Sheets("Inventaire").Range("A4").Value Then
   WsPV.Rows(5).EntireRow.Insert Shift:=xlDown
    WsPV.Rows(5).EntireRow.Insert CopyOrigin:=xlFormatFromLeftOrAbove
    ElseIf Sheets("AJOUTER_PV").Range("K6").Value = Sheets("Inventaire").Range("A4").Value Then
    ' not clear what you intend, either from your code or you text
    ' so, here is some syntax you might adapt
   
    
    WsInv.Range("A4:H4").Copy Destination:=WsInv.Cells(5, "A")
    Application.CutCopyMode = False
    End If
End Sub

So before adding the new line, this code is always copying the same value to first row then add the new value to the second line which gives always only 2 lines and duplicate data..所以在添加新行之前,这段代码总是将相同的值复制到第一行,然后将新值添加到第二行,这总是只给出 2 行和重复数据。

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

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