VBA Excel宏创建一个小的数据库表使用多个表(新的宏,有兴趣学习)



我已经开始使用宏几个星期以来,我正在努力解决一个问题。

我试图创建一个数据输入掩码作为一个项目的一部分,我的目标是存储从表1到表9中的掩码按顺序的数据,我设法实现了这一点,并清除初始掩码,以便可以输入一个新的数据。

实现上述功能的代码如下:

子Macro1

将数据从表1传输到表9

Sheets("Tabelle9").Select
Range("A2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R1C2"
Selection.AutoFill Destination:=Range("A2:A28"), Type:=xlFillDefault
Range("A2:A28").Select
Range("B2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C2"
Selection.AutoFill Destination:=Range("B2:B28"), Type:=xlFillDefault
Range("B2:B28").Select
Range("C2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C3"
Selection.AutoFill Destination:=Range("C2:C28"), Type:=xlFillDefault
Range("C2:C28").Select
Range("D2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C6"
Selection.AutoFill Destination:=Range("D2:D28"), Type:=xlFillDefault
Range("D2:D28").Select
Range("E2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C9"
Selection.AutoFill Destination:=Range("E2:E28"), Type:=xlFillDefault
Range("E2:E28").Select
Range("F2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R1C3"
Selection.AutoFill Destination:=Range("F2:F28"), Type:=xlFillDefault
Range("F2:F28").Select
Range("G2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C14"
Selection.AutoFill Destination:=Range("G2:G28"), Type:=xlFillDefault
Range("G2:G28").Select
Range("H2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C11"
Selection.AutoFill Destination:=Range("H2:H28"), Type:=xlFillDefault
Range("H2:H28").Select
Range("I2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C5"
Selection.AutoFill Destination:=Range("I2:I28"), Type:=xlFillDefault
Range("I2:I28").Select
Range("J2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C13"
Selection.AutoFill Destination:=Range("J2:J28"), Type:=xlFillDefault
Range("J2:J28").Select
Range("K2").Select      
ActiveCell.FormulaR1C1 = "=Tabelle1!R[2]C12"
Selection.AutoFill Destination:=Range("K2:K28"), Type:=xlFillDefault
Range("K2:K28").Select

Sheets("Tabelle9").Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

清除表1中的所有字段

Sheets("Tabelle1").Select
Range("A4:A30").Select
Selection.ClearContents
Range("C4:C30").Select
Selection.ClearContents
Range("E4:F30").Select
Selection.ClearContents
Range("H4:H30").Select
Selection.ClearContents
Range("J4:N30").Select
Selection.ClearContents

结束子

我面临的问题是,每次我使用命令按钮运行宏时,表9中的数据都会被替换。通过大量的互联网搜索,我尝试编辑代码如下:

子小道

Sheets("Tabelle1").Select
Range("B1").Select      
Selection.Copy
Sheets("Tabelle9").Select
Range("A2").Select
Selection.End(xlDown).Select
Count = ActiveCell.Cells.Row + 1
Range("A" & Count).Select
Selection.PasteSpecial Paste:=xlPasteAll, 
Operation:=xlNone,SkipBlanks:= _True, Transpose:=True 

结束子

棘手的部分来了,作为一个新手我不明白,如果有人能指导我,我将非常感激。

  1. 如何通过使用参考单元格而不是单元格的绝对地址将数据粘贴到新表中?(Ex-我想将数据从表1粘贴到表9的顺序,以便从宏代码的每次运行中保存数据的28个单元格,以便当我第二次运行宏时,数据从单元格29保存到56)
  2. 是否可以从表1中复制粘贴一个字段并粘贴到表9中的28个连续单元格中(表1中的A1复制到表9中的所有A1- a28中),如果可以,请告诉我方法。

感谢任何建议。谢谢!

请尝试将工作表名称更改为original:

Sub Trail()
   Static Count As Integer
   Count = Count + 1
   Sheets("Sheet1").Select
   Range("B1").Select
   Selection.Copy
   Sheets("Sheet2").Select
   Sheet2.Range("A" & Count & ":A28").Select
   Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=True, Transpose:=True
   Selection.End(xlDown).Select
   Count = ActiveCell.Cells.Row
End Sub

最新更新