willow1985
Well-known Member
- Joined
- Jul 24, 2019
- Messages
- 929
- Office Version
- 365
- Platform
- Windows
I am starting a new post for this question as I believe it is a separate question.
Someone on this forum was very generous to give me the following code but my problem with it is I do not know how to modify it to select specific columns and group them.
For example. How would I modify this to copy the data from the "Data" sheet for columns A1:C1 AND F and paste them into the form on the "Central Form" sheet
Note: The code also has a problem with clearing the data on rows when pasting it onto the forms (rows 18, 19 20, 36, 37 and 38 to be exact) that I do not know how to fix - I asked this on my previous post if others need to reference this.
Someone on this forum was very generous to give me the following code but my problem with it is I do not know how to modify it to select specific columns and group them.
For example. How would I modify this to copy the data from the "Data" sheet for columns A1:C1 AND F and paste them into the form on the "Central Form" sheet
Note: The code also has a problem with clearing the data on rows when pasting it onto the forms (rows 18, 19 20, 36, 37 and 38 to be exact) that I do not know how to fix - I asked this on my previous post if others need to reference this.
VBA Code:
Sub PasteToCentralForm3()
Dim tableRange As Range, destSheet As Worksheet
Dim tableArray() As Variant, Result() As Variant
Dim numRows As Long, numCols As Long
Dim i As Long, j As Long, loopCount As Long
Dim chunkSize As Long
CustCol = "A1:C1, F1:F1" 'adjust columns to grab **IT IS NOT GRABBING F!**
Set tableRange = ThisWorkbook.Sheets("Data").ListObjects("Order_Data").DataBodyRange 'Change table name as needed
Set destSheet = ThisWorkbook.Sheets("Central Form")
chunkSize = 15 'adjust chunk size as needed.
numRows = tableRange.Rows.Count
numCols = tableRange.Range(CustCol).Columns.Count
p = 1
' Read data into array
tableArray = tableRange.Value
ReDim Result(1 To numRows, 1 To numCols)
For m = 1 To numRows
If tableArray(m, 2) = "Central" Then
For n = 1 To numCols
Result(p, n) = tableArray(m, n)
Next n
p = p + 1
End If
Next m
Application.ScreenUpdating = False
loopCount = WorksheetFunction.RoundUp(UBound(Result, 1) / chunkSize, 0)
' Collect all data into one large array
Dim outputData() As Variant
ReDim outputData(1 To loopCount * (chunkSize + 2) - 2, 1 To numCols)
Dim outputIndex As Long
outputIndex = 1
For i = 1 To loopCount
Dim outputRowCount As Long
outputRowCount = WorksheetFunction.Min(chunkSize, numRows - (i - 1) * chunkSize)
For j = 1 To outputRowCount
For k = 1 To numCols
outputData(outputIndex, k) = Result((i - 1) * chunkSize + j, k)
Next k
outputIndex = outputIndex + 1
Next j
outputIndex = outputIndex + 3 ' Skip three rows for each chunk
Next i
' Output all data to destination sheet at once
destSheet.Cells(3, 1).Resize(UBound(outputData, 1), UBound(outputData, 2)).Value = outputData
Application.ScreenUpdating = True
End Sub
VBA repeat form loop code template2.xlsm | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | OrderDate | Region | Rep | Item | Units | Unit Cost | Total | ||
2 | 6-Jan-21 | East | Jones | Pencil | 95 | 1.99 | 189.05 | ||
3 | 23-Jan-21 | Central | Kivell | Binder | 50 | 19.99 | 999.5 | ||
4 | 9-Feb-21 | Central | Jardine | Pencil | 36 | 4.99 | 179.64 | ||
5 | 26-Feb-21 | Central | Gill | Pen | 27 | 19.99 | 539.73 | ||
6 | 15-Mar-21 | West | Sorvino | Pencil | 56 | 2.99 | 167.44 | ||
7 | 1-Apr-21 | East | Jones | Binder | 60 | 4.99 | 299.4 | ||
8 | 18-Apr-21 | Central | Andrews | Pencil | 75 | 1.99 | 149.25 | ||
9 | 5-May-21 | Central | Jardine | Pencil | 90 | 4.99 | 449.1 | ||
10 | 22-May-21 | West | Thompson | Pencil | 32 | 1.99 | 63.68 | ||
11 | 8-Jun-21 | East | Jones | Binder | 60 | 8.99 | 539.4 | ||
12 | 25-Jun-21 | Central | Morgan | Pencil | 90 | 4.99 | 449.1 | ||
13 | 12-Jul-21 | East | Howard | Binder | 29 | 1.99 | 57.71 | ||
14 | 29-Jul-21 | East | Parent | Binder | 81 | 19.99 | 1619.19 | ||
15 | 15-Aug-21 | East | Jones | Pencil | 35 | 4.99 | 174.65 | ||
16 | 1-Sep-21 | Central | Smith | Desk | 2 | 125 | 250 | ||
17 | 18-Sep-21 | East | Jones | Pen Set | 16 | 15.99 | 255.84 | ||
18 | 5-Oct-21 | Central | Morgan | Binder | 28 | 8.99 | 251.72 | ||
19 | 22-Oct-21 | East | Jones | Pen | 64 | 8.99 | 575.36 | ||
20 | 8-Nov-21 | East | Parent | Pen | 15 | 19.99 | 299.85 | ||
21 | 25-Nov-21 | Central | Kivell | Pen Set | 96 | 4.99 | 479.04 | ||
22 | 12-Dec-21 | Central | Smith | Pencil | 67 | 1.29 | 86.43 | ||
23 | 29-Dec-21 | East | Parent | Pen Set | 74 | 15.99 | 1183.26 | ||
24 | 15-Jan-22 | Central | Gill | Binder | 46 | 8.99 | 413.54 | ||
25 | 1-Feb-22 | Central | Smith | Binder | 87 | 15 | 1305 | ||
26 | 18-Feb-22 | East | Jones | Binder | 4 | 4.99 | 19.96 | ||
27 | 7-Mar-22 | West | Sorvino | Binder | 7 | 19.99 | 139.93 | ||
28 | 24-Mar-22 | Central | Jardine | Pen Set | 50 | 4.99 | 249.5 | ||
29 | 10-Apr-22 | Central | Andrews | Pencil | 66 | 1.99 | 131.34 | ||
30 | 27-Apr-22 | East | Howard | Pen | 96 | 4.99 | 479.04 | ||
31 | 14-May-22 | Central | Gill | Pencil | 53 | 1.29 | 68.37 | ||
32 | 31-May-22 | Central | Gill | Binder | 80 | 8.99 | 719.2 | ||
33 | 17-Jun-22 | Central | Kivell | Desk | 5 | 125 | 625 | ||
34 | 4-Jul-22 | East | Jones | Pen Set | 62 | 4.99 | 309.38 | ||
35 | 21-Jul-22 | Central | Morgan | Pen Set | 55 | 12.49 | 686.95 | ||
36 | 7-Aug-22 | Central | Kivell | Pen Set | 42 | 23.95 | 1005.9 | ||
37 | 24-Aug-22 | West | Sorvino | Desk | 3 | 275 | 825 | ||
38 | 10-Sep-22 | Central | Gill | Pencil | 7 | 1.29 | 9.03 | ||
39 | 27-Sep-22 | West | Sorvino | Pen | 76 | 1.99 | 151.24 | ||
40 | 14-Oct-22 | West | Thompson | Binder | 57 | 19.99 | 1139.43 | ||
41 | 31-Oct-22 | Central | Andrews | Pencil | 14 | 1.29 | 18.06 | ||
42 | 17-Nov-22 | Central | Jardine | Binder | 11 | 4.99 | 54.89 | ||
43 | 4-Dec-22 | Central | Jardine | Binder | 94 | 19.99 | 1879.06 | ||
44 | 21-Dec-22 | Central | Andrews | Binder | 28 | 4.99 | 139.72 | ||
Data |
VBA repeat form loop code template2.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | CENTRAL FORM | ||||||
2 | OrderDate | Region | Rep | Unit Cost | Notes | ||
3 | |||||||
4 | |||||||
5 | |||||||
6 | |||||||
7 | |||||||
8 | |||||||
9 | |||||||
10 | |||||||
11 | |||||||
12 | |||||||
13 | |||||||
14 | |||||||
15 | |||||||
16 | |||||||
17 | |||||||
18 | Company Name | ||||||
19 | CENTRAL FORM | ||||||
20 | OrderDate | Region | Rep | Unit Cost | Notes | ||
21 | |||||||
22 | |||||||
23 | |||||||
24 | |||||||
25 | |||||||
26 | |||||||
27 | |||||||
28 | |||||||
29 | |||||||
30 | |||||||
31 | |||||||
32 | |||||||
33 | |||||||
34 | |||||||
35 | |||||||
36 | Company Name | ||||||
37 | CENTRAL FORM | ||||||
38 | OrderDate | Region | Rep | Unit Cost | Notes | ||
39 | |||||||
40 | |||||||
41 | |||||||
42 | |||||||
43 | |||||||
44 | |||||||
45 | |||||||
46 | |||||||
47 | |||||||
48 | |||||||
49 | |||||||
50 | |||||||
51 | |||||||
52 | |||||||
53 | |||||||
54 | Company Name | ||||||
55 | CENTRAL FORM | ||||||
56 | OrderDate | Region | Rep | Item | Notes | ||
57 | |||||||
58 | |||||||
59 | |||||||
60 | |||||||
61 | |||||||
62 | |||||||
63 | |||||||
64 | |||||||
65 | |||||||
66 | |||||||
67 | |||||||
68 | |||||||
69 | |||||||
70 | |||||||
71 | |||||||
72 | Company Name | ||||||
Central Form |