momo
momo

Reputation: 1

Insert data from different worksheets into next blank row

My objective is to list user requests from different worksheets on a Status worksheet. (Not all worksheets are required to be entered by users.)

Example worksheets: Create, Update, Extend, Delete.

The program I came up with gets the value from Create but once the Update value is filled it overwrites the one from Create.

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 

Upvotes: 0

Views: 44

Answers (1)

Cyril
Cyril

Reputation: 6829

So, there are a couple ways to go about simplifying the content, where I can provide a framework:

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

I set this up to just take the last row in columns(1) from each sheet and append to Status... you can choose to have a loop or maybe it's just rows(x).value = rows(y).value for each if you have the same format on all sheets. That has an impact and may save you from individual cell references.

Upvotes: 0

Related Questions