VBA to copy data from a template in a sheet to Database Sheet

dandelion

New Member
Joined
Jul 16, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a "Template" as in the MiniSheet below where I want to copy data to DataBase Sheet. (I present 02 sheets in 01 layout here for easier reference). What I want to do is I want to select and copy the non-blank cells in each Month column and paste in Database Sheet vertically with the name of the Month in each row. Is there any way to do it in VBA instead of copy manually?

Book3
CDEFGHIJKLMNOP
2Template
3NameCategoryJan 2020Feb 2020Mar 2020Apr 2020May 2020Jun 2020Jul 2020Aug 2020Sep 2020Oct 2020Nov 2020Dec 2020
4Apple JuiceSour$ 3.00$ 4.00$ 5.00$ 4.00$ 5.00$ 6.00$ 3.00
5Lemon JuiceSour$ 3.00$ 2.00$ 1.00$ 4.00$ 3.00$ 3.00$ 3.00
6Oragne JuiceSweet$ 2.00$ 1.00$ 3.00$ 5.00
7Watermelon JuiceSweet$ 4.00$ 1.00$ 4.00$ 5.00$ 5.00
8
9
10Database
11Name CategoryPeriodValue
12Apple JuiceSourJan 2020$ 3.00
13Lemon JuiceSourJan 2020$ 3.00
14Oragne JuiceSweetFeb 2020$ 2.00
15Apple JuiceSourMar 2020$ 4.00
16Watermelon JuiceSweetMar 2020$ 4.00
Sheet1
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Are you creating the Database from scratch, or just adding the current month? What the rows and columns are things located in the Database sheet?
To create the Database from scratch, assuming the sheet is already created and the Name field is in Cell A2, try this:
VBA Code:
Sub CreateDatabase()

Dim Templ As Worksheet
Dim DB As Worksheet
Dim LastCol As Long
Dim i As Long, j As Long, k As Long

Set Templ = Sheets("Template")
Set DB = Sheets("Database")

'start populating with row 3 of DB
k = 3
'find the last used column of Template
LastCol = Templ.Cells(3, Templ.Columns.Count).End(xlToLeft).Column

With DB
    'for columns 5 thru last column
    For j = 5 To LastCol
        'check rows 3 thru 7
        For i = 3 To 7
            'if the cost is not empty
            If Templ.Cells(i, j) > "" Then
                'copy the price to the next row in DB from that cell
                .Cells(k, 4) = Templ.Cells(i, j) 'price
                'copy the Name and category
                Templ.Range(Templ.Cells(i, 3), Templ.Cells(i, 4)).Copy
                'paste it into columns A & B on DB
                .Range("A" & k).PasteSpecial 'name and category
                'copy the month to DB
                .Cells(k, 3) = Templ.Cells(2, j) 'month
                'increment k to set up for the next row on DB
                k = k + 1
            End If
        Next i
    Next j
End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top