将数据从列表框移动到工作表

时间:2019-01-14 10:16:23

标签: excel vba populate

我需要您的帮助,将数据从列表框移至工作表。 此列表框包含14列。如何将列表框中的行复制到工作表?

public class UnitInstruction {
    [Key] public int id { get; set; }
    public string instructionID { get; set; }
    public int? unitID { get; set; }
    public string propertyOne { get; set; }
    public string propertyTwo { get; set; }
}


public class UnitInstructionDbContext : DbContext
{
    public DbSet<UnitInstruction> UnitInstructions { get; set; }
    public UnitInstructionDbContext() : base(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString)
    {
        Database.SetInitializer<UnitInstructionDbContext>(null);
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("RealEstate");
        modelBuilder.Configurations.Add(new UnitInstructionMap());
        base.OnModelCreating(modelBuilder);
    }
}

public class UnitInstructionMap : EntityTypeConfiguration<UnitInstruction>
{
    public UnitInstructionMap()
    {
        ToTable("UnitInstructions");
        HasKey(i => i.ID);
        Property(i => i.InstructionID).HasColumnName("InstructionID");
        Property(i => i.UnitID).HasColumnName("UnitID");
        Property(i => i.PropertyOne).HasColumnName("PropertyOne");
        Property(i => i.PropertyTwo).HasColumnName("PropertyTwo");
    }
}


CREATE TABLE [RealEstate].[UnitInstructions](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [InstructionID] [varchar](20) NULL,
    [UnitID] [int] NULL,
    [PropertyOne] [varchar](10) NULL,
    [PropertyTwo] [varchar](250) NULL,
 CONSTRAINT [PK_CommercialRealEstate.RetailUnitInstructions] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

*****************更新15-01-2019 17:30 ********************* * 我找到了这段代码,但将其向下移动了1行,如下图

所示
Sub Post ()

    Dim arr
    Dim cnt As Integer
    cnt = ListBox1.ListCount
    arr = ListBox1.List

    With Sheets("DATABASE").ListObjects(1)
      .ListRows.Add
      .DataBodyRange.Cells(.ListRows.Count, 1).Resize(cnt, 14) = arr
    End With
    ListBox1.clear 

End Sub

Pic

2 个答案:

答案 0 :(得分:0)

不清楚如何设置目标单元格,无论如何都可以尝试(遵循示例逻辑):

Dim Trg as Range

With Sheets("DATABASE").ListObjects(1)
     Set Trg = .DataBodyRange.Cells(.ListRows.Count, 1)
End With
Trg.Resize(UBound(arr, 1), UBound(arr, 2)) = arr

如果可以预先指定目标范围的左上角单元格和右下角单元格,则将数组的内容粘贴到某个范围很容易,因此可以说

Range("B8:E16") = arr

如果您只知道左上角的单元格并想动态设置目标范围的大小,请像这样使用.Resize

Range("B8").Resize(UBound(arr, 1), UBound(arr, 2)) = arr

NB:如果目标范围小于数组,则仅复制该数量的数据,其余部分被忽略。

答案 1 :(得分:0)

最后我找到了解决我的问题的代码

Private Sub CommandButton1_Click()
Dim lngItem As Long

For lngItem = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(lngItem) Then
        With Sheets(1) '< qualify sheet here
            .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Value = 
  ListBox1.List(lngItem, 1)
            .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Value = 
 ListBox1.List(lngItem, 2)
        End With
    End If
  Next lngItem

  'Unload Me
  End Sub