Grid Construction With numeric values & Dates

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
71
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
13
3/11/2016

14
4/11/2016

15
5/11/2016

16
6/11/2016

17
7/11/2016

12
2/11/2016

3
24/10/2016

4
25/10/2016

5
26/10/2016

18
8/1111/2016

11
1/11/2016

2
23/10/2016

1
22/10/2016

6
27/10/2016

19
9/11/2016

10
31/10/2016

9
30/10/2016

8
29/10/2016

7
28/10/2016

20
10/11/2016

25
15/11/2016

24
14/11/2016

23
13/11/2016

22
12/11/2016

21
11/11/2016


<tbody>
</tbody>


Hi Friends,

Could you help to create grid like above
In middle cell of 5 by 5 Grid , i will place a random number (ex: 1) along with random date(ex:22/10/2016)
It should Construct the remaining values (in clockwise direction) for other cells automatically using excel formulae..

Appreciate for the help..
 
See if this is any use.

Rich (BB code):
Sub MakeGrid()
  Dim StartNum As Long, Levels As Long, GridSize As Long, Counter As Long, r As Long, c As Long
  Dim StartDate As Date
  Dim a As Variant
  Dim sMiddle As String
  
  Const CF1 As String = "=OR(ROW(A8)=ROW(#),COLUMN(A8)=COLUMN(#))"
  Const CF2 As String = "=ABS(ROW(A8)-ROW(#))=ABS(COLUMN(A8)-COLUMN(#))"
  
  StartNum = Range("B2").Value
  Levels = Range("B3").Value
  StartDate = Range("B4").Value
  
  With ActiveSheet.UsedRange.Offset(6)
    .ClearContents
    .FormatConditions.Delete
  End With
  GridSize = 2 * Levels + 1
  ReDim a(1 To GridSize, 1 To GridSize)
  r = GridSize / 2 + 0.5
  c = r
  
  Do
    a(r, c) = StartNum & Chr(10) & Format(StartDate, "dd/mm/yyyy")
    StartNum = StartNum + 1
    StartDate = StartDate + 1
    Counter = Counter + 1
    Select Case True
      Case a(r - 1, c) = "" And a(r, c + 1) <> ""
        r = r - 1
      Case a(r, c + 1) = "" And a(r + 1, c) <> ""
        c = c + 1
      Case a(r + 1, c) = "" And a(r, c - 1) <> ""
        r = r + 1
      Case Else
        c = c - 1
    End Select
  Loop Until Counter = (GridSize - 2) ^ 2
  
  With Range("A7").Resize(UBound(a, 1), UBound(a, 2))
    .Value = a
    .HorizontalAlignment = xlCenter
    .Columns(1).Delete Shift:=xlToLeft
  End With
  With Range("A8").CurrentRegion
    sMiddle = .Cells(.Rows.Count / 2 + 0.5, .Columns.Count / 2 + 0.5).Address
    .FormatConditions.Add Type:=xlExpression, Formula1:=Replace(CF1, "#", sMiddle)
    .FormatConditions(1).Interior.Color = vbRed
    .FormatConditions(1).StopIfTrue = True
    .FormatConditions.Add Type:=xlExpression, Formula1:=Replace(CF2, "#", sMiddle)
    .FormatConditions(2).Interior.Color = 15773696
  End With
End Sub

With construction data in A1:B4 as shown below, the code has produced what you see in Row 8 & below.

Excel Workbook
ABCDEFGHI
1Data
2Starting Number7
3Levels5
4Starting Date22/10/2016
5
6
7
86317/12/20166418/12/20166519/12/20166620/12/20166721/12/20166822/12/20166923/12/20167024/12/20167125/12/2016
96216/12/20163721/11/20163822/11/20163923/11/20164024/11/20164125/11/20164226/11/20164327/11/20167226/12/2016
106115/12/20163620/11/20161903/11/20162004/11/20162105/11/20162206/11/20162307/11/20164428/11/20167327/12/2016
116014/12/20163519/11/20161802/11/2016924/10/20161025/10/20161126/10/20162408/11/20164529/11/20167428/12/2016
125913/12/20163418/11/20161701/11/2016823/10/2016722/10/20161227/10/20162509/11/20164630/11/20167529/12/2016
135812/12/20163317/11/20161631/10/20161530/10/20161429/10/20161328/10/20162610/11/20164701/12/20167630/12/2016
145711/12/20163216/11/20163115/11/20163014/11/20162913/11/20162812/11/20162711/11/20164802/12/20167731/12/2016
155610/12/20165509/12/20165408/12/20165307/12/20165206/12/20165105/12/20165004/12/20164903/12/20167801/01/2017
168710/01/20178609/01/20178508/01/20178407/01/20178306/01/20178205/01/20178104/01/20178003/01/20177902/01/2017
Grid
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Peter,

Wow.. In India, today is festival of Deepawali (Diwali).

I treat this as a gift form your side.

Btw, It is perfect except coloring of Rows & Coloumns.
I saw red Color on 7th Column(ie G) (from 69 to 81) & etc..

Let me see if I can attach snapshot of It.

Thanks A Ton.


See if this is any use.

Rich (BB code):
Sub MakeGrid()
  Dim StartNum As Long, Levels As Long, GridSize As Long, Counter As Long, r As Long, c As Long
  Dim StartDate As Date
  Dim a As Variant
  Dim sMiddle As String
  
  Const CF1 As String = "=OR(ROW(A8)=ROW(#),COLUMN(A8)=COLUMN(#))"
  Const CF2 As String = "=ABS(ROW(A8)-ROW(#))=ABS(COLUMN(A8)-COLUMN(#))"
  
  StartNum = Range("B2").Value
  Levels = Range("B3").Value
  StartDate = Range("B4").Value
  
  With ActiveSheet.UsedRange.Offset(6)
    .ClearContents
    .FormatConditions.Delete
  End With
  GridSize = 2 * Levels + 1
  ReDim a(1 To GridSize, 1 To GridSize)
  r = GridSize / 2 + 0.5
  c = r
  
  Do
    a(r, c) = StartNum & Chr(10) & Format(StartDate, "dd/mm/yyyy")
    StartNum = StartNum + 1
    StartDate = StartDate + 1
    Counter = Counter + 1
    Select Case True
      Case a(r - 1, c) = "" And a(r, c + 1) <> ""
        r = r - 1
      Case a(r, c + 1) = "" And a(r + 1, c) <> ""
        c = c + 1
      Case a(r + 1, c) = "" And a(r, c - 1) <> ""
        r = r + 1
      Case Else
        c = c - 1
    End Select
  Loop Until Counter = (GridSize - 2) ^ 2
  
  With Range("A7").Resize(UBound(a, 1), UBound(a, 2))
    .Value = a
    .HorizontalAlignment = xlCenter
    .Columns(1).Delete Shift:=xlToLeft
  End With
  With Range("A8").CurrentRegion
    sMiddle = .Cells(.Rows.Count / 2 + 0.5, .Columns.Count / 2 + 0.5).Address
    .FormatConditions.Add Type:=xlExpression, Formula1:=Replace(CF1, "#", sMiddle)
    .FormatConditions(1).Interior.Color = vbRed
    .FormatConditions(1).StopIfTrue = True
    .FormatConditions.Add Type:=xlExpression, Formula1:=Replace(CF2, "#", sMiddle)
    .FormatConditions(2).Interior.Color = 15773696
  End With
End Sub

With construction data in A1:B4 as shown below, the code has produced what you see in Row 8 & below.

Grid

*ABCDEFGHI
1Data
2Starting Number7
3Levels5
4Starting Date22/10/2016
5
6
7
863
17/12/2016
64
18/12/2016
65
19/12/2016
66
20/12/2016
67
21/12/2016
68
22/12/2016
69
23/12/2016
70
24/12/2016
71
25/12/2016
962
16/12/2016
37
21/11/2016
38
22/11/2016
39
23/11/2016
40
24/11/2016
41
25/11/2016
42
26/11/2016
43
27/11/2016
72
26/12/2016
1061
15/12/2016
36
20/11/2016
19
03/11/2016
20
04/11/2016
21
05/11/2016
22
06/11/2016
23
07/11/2016
44
28/11/2016
73
27/12/2016
1160
14/12/2016
35
19/11/2016
18
02/11/2016
9
24/10/2016
10
25/10/2016
11
26/10/2016
24
08/11/2016
45
29/11/2016
74
28/12/2016
1259
13/12/2016
34
18/11/2016
17
01/11/2016
8
23/10/2016
7
22/10/2016
12
27/10/2016
25
09/11/2016
46
30/11/2016
75
29/12/2016
1358
12/12/2016
33
17/11/2016
16
31/10/2016
15
30/10/2016
14
29/10/2016
13
28/10/2016
26
10/11/2016
47
01/12/2016
76
30/12/2016
1457
11/12/2016
32
16/11/2016
31
15/11/2016
30
14/11/2016
29
13/11/2016
28
12/11/2016
27
11/11/2016
48
02/12/2016
77
31/12/2016
1556
10/12/2016
55
09/12/2016
54
08/12/2016
53
07/12/2016
52
06/12/2016
51
05/12/2016
50
04/12/2016
49
03/12/2016
78
01/01/2017
1687
10/01/2017
86
09/01/2017
85
08/01/2017
84
07/01/2017
83
06/01/2017
82
05/01/2017
81
04/01/2017
80
03/01/2017
79
02/01/2017

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:114px;"><col style="width:83px;"><col style="width:83px;"><col style="width:83px;"><col style="width:83px;"><col style="width:83px;"><col style="width:76px;"><col style="width:83px;"><col style="width:83px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
 
Upvote 0
Data
Starting Number7
Levels5
Starting Date30/10/2016
63
25/12/2016
64
26/12/2016
65
27/12/2016
66
28/12/2016
67
29/12/2016
68
30/12/2016

69
31/12/2016

70
01/01/2017

71
02/01/2017
62
24/12/2016
37
29/11/2016
38
30/11/2016
39
01/12/2016
40
02/12/2016

41
03/12/2016
42
04/12/2016

43
05/12/2016
72
03/01/2017

61
23/12/2016
36
28/11/2016
19
11/11/2016
20
12/11/2016

21
13/11/2016
22
14/11/2016
23
15/11/2016

44
06/12/2016
73
04/01/2017
60
22/12/2016
35
27/11/2016
18
10/11/2016

9
01/11/2016
10
02/11/2016
11
03/11/2016
24
16/11/2016

45
07/12/2016
74
05/01/2017
59
21/12/2016
34
26/11/2016
17
09/11/2016
8
31/10/2016
7
30/10/2016
12
04/11/2016
25
17/11/2016

46
08/12/2016
75
06/01/2017
58
20/12/2016
33
25/11/2016
16
08/11/2016
15
07/11/2016
14
06/11/2016
13
05/11/2016
26
18/11/2016

47
09/12/2016
76
07/01/2017
57
19/12/2016
32
24/11/2016
31
23/11/2016
30
22/11/2016
29
21/11/2016
28
20/11/2016
27
19/11/2016

48
10/12/2016
77
08/01/2017
56
18/12/2016
55
17/12/2016
54
16/12/2016
53
15/12/2016
52
14/12/2016
51
13/12/2016
50
12/12/2016

49
11/12/2016
78
09/01/2017
87
18/01/2017
86
17/01/2017
85
16/01/2017
84
15/01/2017
83
14/01/2017
82
13/01/2017
81
12/01/2017

80
11/01/2017
79
10/01/2017

<colgroup><col><col><col><col span="2"><col><col><col><col></colgroup><tbody>
</tbody>
 
Last edited:
Upvote 0
Hi Peter,

Could you add a search functionality for Number( ex: 5) & Date (Ex:DD/MM/YYYY) for the grid.

Regards & regards.
 
Upvote 0
Firstly, it is best not to quote whole (long) posts like you did in post #12. It makes your post and the thread harder to read/navigate. If it is necessary to quote, just quote small relevant parts, like I have here.

.. except coloring of Rows & Coloumns.
I saw red Color on 7th Column(ie G) (from 69 to 81) & etc..
What version of Excel are you using?



Could you add a search functionality for Number( ex: 5) & Date (Ex:DD/MM/YYYY) for the grid.
I don't understand how you envisage this working or what it would be used for.
Is this a separate function to creation of the grid?
How would we know what number & date to search for?
What if the number and date being searched for are in different cells?
What should happen once the number &/or date are found (or not found)?
.. etc

Please explain in more detail exactly what you are trying to achieve.
 
Upvote 0
What version of Excel are you using?
On looking more closely at the colour result that you obtained, I think that the answer to this question is probably Excel 2007, but please confirm the version anyway.

If it is Excel 2007, then add this blue line into the code where shown and try the code again.

Rich (BB code):
With Range("A8").CurrentRegion
  .Cells(1).Select
  sMiddle = .Cells(.Rows.Count / 2 + 0.5, .Columns.Count / 2 + 0.5).Address
 
Upvote 0
Hi Peter,


1) Yes, Excel version is 2007 & Now colouring of grid working perfectly.
2) It is a simple search functionality with Excel functions (without VBA).
Here, User enter either a number or date(dd mm yyyy) (in their respective search boxes) to search in the grid.
Once user enter either a number or date, Excel will show that cell with a color(Ex: Blue).
Most of the time, I search one serach functionality at a time. But, if both number & Date functionality works parrelly that is too good.
ie search results can be present in different cells.
3) We do not use search results further. Once we found/Not found results in cells, our work is done.

Thanks & regards
Vishy
 
Upvote 0
2) It is a simple search functionality with Excel functions (without VBA).
If it without vba then you would just select the grid and use the normal Find (Ctrl+F), enter your date or number, check 'Options' to ensure you do not have 'Match entire cell contents' checked, and click 'Find Next'
 
Upvote 0
Hi Peter,

I tested using normal find(Ctrl + F), it did not work.
Btw, Could you provide VBA code for search functionality as it will be easy if grid size is big.
Also, colorize the resulted cell with in grid (Ex: Blue)

Thanks & Regards
Vishy
 
Upvote 0
Since blue is already used for the diagonal line, I have used a different colour for the search.
The values to be searched for are placed in E2 and E3 as shown below.
Replace the grid construction code with this version.

Rich (BB code):
Sub MakeGrid_v2()
  Dim StartNum As Long, Levels As Long, GridSize As Long, Counter As Long, r As Long, c As Long
  Dim StartDate As Date
  Dim a As Variant
  Dim sMiddle As String
  
  Const CF1 As String = "=OR(AND($E$2<>"""",ISNUMBER(FIND($E$2&CHAR(10),A8))),ISNUMBER(FIND(CHAR(10)&TEXT($E$3,""dd/mm/yyyy""),A8)))"
  Const CF2 As String = "=OR(ROW(A8)=ROW(#),COLUMN(A8)=COLUMN(#))"
  Const CF3 As String = "=ABS(ROW(A8)-ROW(#))=ABS(COLUMN(A8)-COLUMN(#))"
  
  
  StartNum = Range("B2").Value
  Levels = Range("B3").Value
  StartDate = Range("B4").Value
  
  Application.ScreenUpdating = False
  With ActiveSheet.UsedRange.Offset(6)
    .ClearContents
    .FormatConditions.Delete
  End With
  GridSize = 2 * Levels + 1
  ReDim a(1 To GridSize, 1 To GridSize)
  r = GridSize / 2 + 0.5
  c = r
  
  Do
    a(r, c) = StartNum & Chr(10) & Format(StartDate, "dd/mm/yyyy")
    StartNum = StartNum + 1
    StartDate = StartDate + 1
    Counter = Counter + 1
    Select Case True
      Case a(r - 1, c) = "" And a(r, c + 1) <> ""
        r = r - 1
      Case a(r, c + 1) = "" And a(r + 1, c) <> ""
        c = c + 1
      Case a(r + 1, c) = "" And a(r, c - 1) <> ""
        r = r + 1
      Case Else
        c = c - 1
    End Select
  Loop Until Counter = (GridSize - 2) ^ 2
  
  With Range("A7").Resize(UBound(a, 1), UBound(a, 2))
    .Value = a
    .HorizontalAlignment = xlCenter
    .Columns(1).Delete Shift:=xlToLeft
  End With
With Range("A8").CurrentRegion
    .Cells(1).Select
    sMiddle = .Cells(.Rows.Count / 2 + 0.5, .Columns.Count / 2 + 0.5).Address
    .FormatConditions.Add Type:=xlExpression, Formula1:=CF1
    .FormatConditions(1).Interior.Color = 49407
    .FormatConditions(1).StopIfTrue = True
    .FormatConditions.Add Type:=xlExpression, Formula1:=Replace(CF2, "#", sMiddle)
    .FormatConditions(2).Interior.Color = vbRed
    .FormatConditions(2).StopIfTrue = True
    .FormatConditions.Add Type:=xlExpression, Formula1:=Replace(CF3, "#", sMiddle)
    .FormatConditions(3).Interior.Color = 15773696
  End With
  Application.ScreenUpdating = True
End Sub


Edit: Ignore the asterisks in row 7, Excel jeanie glitch.

Excel Workbook
ABCDEFGHI
1DataSearch
2Starting Number1Number28
3Levels5Date26/11/2016
4Starting Date22/10/2016
5
6
7*********
85717/12/20165818/12/20165919/12/20166020/12/20166121/12/20166222/12/20166323/12/20166424/12/20166525/12/2016
95616/12/20163121/11/20163222/11/20163323/11/20163424/11/20163525/11/20163626/11/20163727/11/20166626/12/2016
105515/12/20163020/11/20161303/11/20161404/11/20161505/11/20161606/11/20161707/11/20163828/11/20166727/12/2016
115414/12/20162919/11/20161202/11/2016324/10/2016425/10/2016526/10/20161808/11/20163929/11/20166828/12/2016
125313/12/20162818/11/20161101/11/2016223/10/2016122/10/2016627/10/20161909/11/20164030/11/20166929/12/2016
135212/12/20162717/11/20161031/10/2016930/10/2016829/10/2016728/10/20162010/11/20164101/12/20167030/12/2016
145111/12/20162616/11/20162515/11/20162414/11/20162313/11/20162212/11/20162111/11/20164202/12/20167131/12/2016
155010/12/20164909/12/20164808/12/20164707/12/20164606/12/20164505/12/20164404/12/20164303/12/20167201/01/2017
168110/01/20178009/01/20177908/01/20177807/01/20177706/01/20177605/01/20177504/01/20177403/01/20177302/01/2017
Grid
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,580
Members
449,174
Latest member
chandan4057

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