Match sheet name for cell with sheet name and repeat filling blank cells

Mussala

Board Regular
Joined
Sep 28, 2022
Messages
74
Office Version
  1. 2019
Platform
  1. Windows
Hi experts,
I have multiple sheets in my file , but I want loop throughout theses sheets(mst,secv,thr) based on matching cell H6 with MATCH sheet .
so should search for matched sheet name with cell H6 if it's matched , then should fill blank cells & repeat them for columns B,C,D based on cells J9,N8,S7 where are existed in MATCH sheet .
if there columns B,C,D are already filled, then should ignore columns B,C,D are filled , just search for next blank cells in column B,C, and fill them.
I have this code for @Peter_Ss but this work for just one sheet.
VBA Code:
Sub Fill_Values_v2()
  Dim ws2 As Worksheet
  Dim lrC As Long, lrD As Long
 
  Set ws2 = Sheets("Sheet2")
  With Sheets("Sheet1")
    lrC = .Range("A" & Rows.Count).End(xlUp).Row
    lrD = .Range("B" & Rows.Count).End(xlUp).Row
    If lrC > lrD Then
      With .Range("B" & lrD + 1 & ":D" & lrC)
        .Value = Array(ws2.Range("J9").Value, ws2.Range("S7").Value, ws2.Range("N8").Value)
      End With
    End If
  End With
End Sub
original data
Microsoft Excel .xlsx
ABCDE
1ITEMNAMEDATEREF NOBRAND
21BFGH-001
32BFGH-002
43BFGH-003
54BFGH-004
65BFGH-005
76BFGH-006
87BFGH-007
98BFGH-008
109BFGH-009
1110BFGH-010
mst


Microsoft Excel.xlsx
ABCDE
1ITEMNAMEDATEREF NOBRAND
21BFGH-0110
32BFGH-0111
43BFGH-0112
54BFGH-0113
65BFGH-0114
76BFGH-0115
87BFGH-0116
secv



Microsoft Excel.xlsx
ABCDE
1ITEMNAMEDATEREF NOBRAND
21BFGH-0011
32BFGH-0012
43BFGH-0013
54BFGH-0014
65BFGH-0015
thr


Microsoft Excel .xlsx
HIJKLMNOPQRS
5SHEET NAME
6DATE
7REF NO
8NAME
9
MATCH


example and result for sheet mst
Microsoft Excel .xlsx
HIJKLMNOPQRS
4
5SHEET NAME
6mstDATE
7REF NO10/04/2023
8NAMERE-001
9MUSSA
MATCH
Cell Formulas
RangeFormula
S7S7=TODAY()


should be
Microsoft Excel .xlsx
ABCDE
1ITEMNAMEDATEREF NOBRAND
21MUSSA10/04/2023RE-001BFGH-001
32MUSSA10/04/2023RE-001BFGH-002
43MUSSA10/04/2023RE-001BFGH-003
54MUSSA10/04/2023RE-001BFGH-004
65MUSSA10/04/2023RE-001BFGH-005
76MUSSA10/04/2023RE-001BFGH-006
87MUSSA10/04/2023RE-001BFGH-007
98MUSSA10/04/2023RE-001BFGH-008
109MUSSA10/04/2023RE-001BFGH-009
1110MUSSA10/04/2023RE-001BFGH-010
mst


another example for sheet thr
Microsoft Excel .xlsx
HIJKLMNOPQRS
5SHEET NAME
6thrDATE
7REF NO10/04/2023
8NAMETH-001
9MUSTAFA
MATCH
Cell Formulas
RangeFormula
S7S7=TODAY()

should be
Microsoft Excel .xlsx
ABCDE
1ITEMNAMEDATEREF NOBRAND
21MUSTAFA10/04/2023TH-001BFGH-0011
32MUSTAFA10/04/2023TH-001BFGH-0012
43MUSTAFA10/04/2023TH-001BFGH-0013
54MUSTAFA10/04/2023TH-001BFGH-0014
65MUSTAFA10/04/2023TH-001BFGH-0015
thr
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello. This is how I would do it:
Test_1.xlsm
\_______________________/
(there is a hidden sheet)​

Test_1.xlsm
ABCDEFGHIJKL
1
2SHEET NAME
3thrDATE
4REF NO10/4/2023
5NAMETH-001
6MUSTAFA
MATCH
Cell Formulas
RangeFormula
L4L4=TODAY()
Cells with Data Validation
CellAllowCriteria
A3List='Master data'!$A$2:$A$4
 
Upvote 0
@Mario_R please post your solution to the board, rather than just posting a link to the file. Thanks
 
Upvote 0
No problem.

VBA Code:
Sub Fill_Values_v3()
Dim ws As Worksheet, mData, mBrands
Application.ScreenUpdating = False
'------------------>
With Sheets("Match")
  If IsError(Evaluate("Cell(""Row"", " & .Range("A3") & "!A1)")) Then
    Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
    ws.Name = .Range("A3")
    .Range("A2").Copy ws.Range("A1:E1")
    ws.Range("A1:E1") = Array("ITEM", "NAME", "DATE", "REF NO", "BRAND")
    ws.Columns(1).HorizontalAlignment = xlCenter
    ws.Rows.RowHeight = 22
    ws.Cells.VerticalAlignment = xlCenter
  End If
'------------------>
  Set ws = Sheets(CStr(.Range("A3"))): ws.Activate
  mData = Array(.[c6].Value, .[L4].Value, .[g5].Value)
End With
'------------------>
With Sheets("Master data")
  Set mBrands = .Columns("A").Find(ws.Name, LookAt:=xlWhole)
  mBrands = Application.Transpose(.Range(mBrands(1, 2), mBrands.End(xlToRight)))
End With
'------------------>
With ws.Cells(Rows.Count, 1).End(xlUp)
  .Cells(2, 2).Resize(UBound(mBrands), 3) = mData
  .Cells(2, 5).Resize(UBound(mBrands)) = mBrands
  With ws.[a1].CurrentRegion.Columns(1)
    .Cells(2) = 1
    .Cells(2).Resize(.Rows.Count - 1).DataSeries
  End With
  ws.Columns.AutoFit
End With
'------------------>
End Sub
 
Upvote 0
@Mario_R

thanks , but there is problem! . as I said and the original code fills blank cells .

your code will skip blank cells for column B,C,D if the column A,E are filled and copy to the bottom .

every time I fill data in columns A,E and leave columns B,C,D are blank, so should filled empty cells when columns A,E are filled as the original code does it.
I hope you can fix it .

 
Upvote 0
Did you try to enter data two or three times to a specific sheet?...

Because leaving cells blank does not represent anything in the present context.
 
Upvote 0
first should not copy to the bottom repeatedly as your code does it when run more time .
the idea depends on search for blank cells and filled based on cells in MATCH sheet .
this is what I got
after first run I add new data from row 7 like this
Test_1 (1).xlsm
ABCDE
1ITEMNAMEDATEREF NOBRAND
21MUSTAFA10/04/2023TH-001BFGH-0011
32MUSTAFA10/04/2023TH-001BFGH-0012
43MUSTAFA10/04/2023TH-001BFGH-0013
54MUSTAFA10/04/2023TH-001BFGH-0014
65MUSTAFA10/04/2023TH-001BFGH-0015
76BFGH-0016
87BFGH-0017
98BFGH-0018
109BFGH-0019
1110BFGH-0020
thr

but if I run again this is will give me
Test_1 (1).xlsm
ABCDE
1ITEMNAMEDATEREF NOBRAND
21MUSTAFA10/04/2023TH-001BFGH-0011
32MUSTAFA10/04/2023TH-001BFGH-0012
43MUSTAFA10/04/2023TH-001BFGH-0013
54MUSTAFA10/04/2023TH-001BFGH-0014
65MUSTAFA10/04/2023TH-001BFGH-0015
76BFGH-0016
87BFGH-0017
98BFGH-0018
109BFGH-0019
1110BFGH-0020
1211MUSTAFA110/04/2023TH-0011BFGH-0011
1312MUSTAFA110/04/2023TH-0011BFGH-0012
1413MUSTAFA110/04/2023TH-0011BFGH-0013
1514MUSTAFA110/04/2023TH-0011BFGH-0014
1615MUSTAFA110/04/2023TH-0011BFGH-0015
thr



the right
Test_1 (1).xlsm
ABCDE
1ITEMNAMEDATEREF NOBRAND
21MUSTAFA10/04/2023TH-001BFGH-0011
32MUSTAFA10/04/2023TH-001BFGH-0012
43MUSTAFA10/04/2023TH-001BFGH-0013
54MUSTAFA10/04/2023TH-001BFGH-0014
65MUSTAFA10/04/2023TH-001BFGH-0015
76MUSTAFA110/04/2023TH-0011BFGH-0016
87MUSTAFA110/04/2023TH-0011BFGH-0017
98MUSTAFA110/04/2023TH-0011BFGH-0018
109MUSTAFA110/04/2023TH-0011BFGH-0019
1110MUSTAFA110/04/2023TH-0011BFGH-0020
thr



and if I run more one time then will gives this
Test_1 (1).xlsm
ABCDE
1ITEMNAMEDATEREF NOBRAND
21MUSTAFA10/04/2023TH-001BFGH-0011
32MUSTAFA10/04/2023TH-001BFGH-0012
43MUSTAFA10/04/2023TH-001BFGH-0013
54MUSTAFA10/04/2023TH-001BFGH-0014
65MUSTAFA10/04/2023TH-001BFGH-0015
76MUSTAFA110/04/2023TH-0011BFGH-0016
87MUSTAFA110/04/2023TH-0011BFGH-0017
98MUSTAFA110/04/2023TH-0011BFGH-0018
109MUSTAFA110/04/2023TH-0011BFGH-0019
1110MUSTAFA110/04/2023TH-0011BFGH-0020
1211MUSTAFA110/04/2023TH-0011BFGH-0016
1312MUSTAFA110/04/2023TH-0011BFGH-0017
1413MUSTAFA110/04/2023TH-0011BFGH-0018
1514MUSTAFA110/04/2023TH-0011BFGH-0019
1615MUSTAFA110/04/2023TH-0011BFGH-0020
1716MUSTAFA110/04/2023TH-0011BFGH-0016
1817MUSTAFA110/04/2023TH-0011BFGH-0017
1918MUSTAFA110/04/2023TH-0011BFGH-0018
2019MUSTAFA110/04/2023TH-0011BFGH-0019
2120MUSTAFA110/04/2023TH-0011BFGH-0020
thr


this is wrong if there are no blank cells for columns B,C,D then should not copy at all then the right will keep this
Test_1 (1).xlsm
ABCDE
1ITEMNAMEDATEREF NOBRAND
21MUSTAFA10/04/2023TH-001BFGH-0011
32MUSTAFA10/04/2023TH-001BFGH-0012
43MUSTAFA10/04/2023TH-001BFGH-0013
54MUSTAFA10/04/2023TH-001BFGH-0014
65MUSTAFA10/04/2023TH-001BFGH-0015
76MUSTAFA110/04/2023TH-0011BFGH-0016
87MUSTAFA110/04/2023TH-0011BFGH-0017
98MUSTAFA110/04/2023TH-0011BFGH-0018
109MUSTAFA110/04/2023TH-0011BFGH-0019
1110MUSTAFA110/04/2023TH-0011BFGH-0020
thr
 
Upvote 0
Let's see if I understand you: do you provide the data in columns A and E manually?

Actually: if so, you should only write the data in column E, since consecutive numbering of column A is very easy to implement.
 
Upvote 0
in my real project will copy from cells for other sheet ( not enter manually inside cells by me )
 
Upvote 0
Now I understand it and clearly the auxiliary sheet that I had suggested is not necessary.

In fact: you don't even need the code that checks if the sheet exists and creates it if it doesn't.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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