VBA 将数据插入不同工作表中的下一个空白行

问题描述 投票:0回答:1

这里是新手。目标是能够从不同的用户输入工作表中列出状态工作表上用户的所有请求(顺便说一句,并非所有工作表都需要用户输入。)工作表的示例有:创建、更新、扩展、删除。我提出的程序从创建值中获取值,但是一旦更新值被填充,它就会覆盖来自创建值的值。有人可以帮忙吗?

这是我想出的代码

Sub commit()

Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim count As Integer

    Sheet1.Range("M1").Value = "=counta(F5:F8)+3"
    Sheet4.Range("A1").Value = "=counta(E3:E100)+2"
    Sheet13.Range("A1").Value = "=counta(E3:E100)+2"

count = WorksheetFunction.CountA(Sheet24.Range("A:A"))
For b = 3 To Sheet4.Range("A1").Value
Sheet24.Range("XFD1").Value = "=counta(a2:a100)+2"
a = Sheet24.Range("XFD1").Value
c = Sheet1.Range("M1").Value
d = Sheet13.Range("A1").Value
'create
    Sheet24.Range("A" & count).Value = Sheet4.Range("B" & b).Value
    Sheet24.Range("C" & count).Value = Sheet4.Range("D" & b).Value
    Sheet24.Range("D" & count).Value = Sheet4.Range("F" & b).Value
    Sheet24.Range("E" & count).Value = Sheet4.Range("J" & b).Value
    Sheet24.Range("G" & count).Value = Sheet1.Range("F" & c).Value
'update-description
    Sheet24.Range("A" & count + 1).Value = Sheet13.Range("B" & d).Value 'maintenance request code
    Sheet24.Range("C" & count + 1).Value = Sheet13.Range("D" & d).Value 'line number
    Sheet24.Range("D" & count + 1).Value = Sheet4.Range("F" & b).Value 'mattype code
    Sheet24.Range("E" & count + 1).Value = Sheet4.Range("G" & b).Value 
excel vba insert row
1个回答
0
投票

因此,有几种方法可以简化内容,我可以提供一个框架:

Dim ws as Worksheet:  For Each ws in Worksheets
    Select Case sheets(ws.name)
        Case "Create", "Update", "Extend", "Delete"
            With Sheets(ws.Name)
                'May want to put a loop for the last row on the ws
                Dim StatusLastRow as Long:  StatusLastRow = Sheets("Status").Cells(Sheets("Status").Rows.Count,1).End(XLUp).Row
                Sheets("Status").Cells(StatusLastRow+1,1).Value = .Cells(.Cells(.Rows.Count,1),1).Value
            End With
    End select
Next ws

我将其设置为仅从每张表中取出列(1)中的最后一行并附加到状态...您可以选择有一个循环,或者可能只是行(x).value = rows(y).value如果所有工作表上的格式都相同,则为每个。 这会产生影响,并可能使您免于引用单个单元格。

© www.soinside.com 2019 - 2024. All rights reserved.