rename sheet based on number format in last row for each sheet

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
hello,
I would rename sheet based on amount is existed in last row for column F when the sheet is active.
I try with this
VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim lr As Long
lr = Range("F" & Rows.Count).End(xlUp).Row
ActiveSheet.Name = Format("INVOICE" & " " & lr, "#,##0.00")
End Sub
data before
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
35TOTAL39.0028,485.00
SH1
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=E22*D22


OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR1.00500.00500.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR1.00460.00460.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00450.001,800.00
287GC 385/65R22.5 AT131 CHI12.001,640.0019,680.00
298KM 265/70R16 KOR4.00880.003,520.00
309KM 235/65R17 HP71 KOR1.00875.00875.00
3110KM 235/55R19 PS71 KOR2.00670.001,340.00
32TOTAL33.0032,425.00
IN1
Cell Formulas
RangeFormula
D32,F32D32=SUM(D22:D31)
F22:F31F22=E22*D22


what I want
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
35TOTAL39.0028,485.00
INVOICE 28,485.00
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=E22*D22


OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR1.00500.00500.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR1.00460.00460.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00450.001,800.00
287GC 385/65R22.5 AT131 CHI12.001,640.0019,680.00
298KM 265/70R16 KOR4.00880.003,520.00
309KM 235/65R17 HP71 KOR1.00875.00875.00
3110KM 235/55R19 PS71 KOR2.00670.001,340.00
32TOTAL33.0032,425.00
INVOICE 32,425.00
Cell Formulas
RangeFormula
D32,F32D32=SUM(D22:D31)
F22:F31F22=E22*D22
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this instead (in ThisWorkbook module)

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  On Error Resume Next
  Sh.Name = "INVOICE " & Sh.Range("F" & Rows.Count).End(xlUp).Text
End Sub
 
Upvote 0
Solution
perfect!
Can I implement for specific sheets instead of each sheet ?
I try to do that for first and second sheets by using loop but doesn't work.
VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  On Error Resume Next
  Dim i as long 
  For i = 1 To 2
  Sh(i).Name = "INVOICE " & Sh.Range("F" & Rows.Count).End(xlUp).Text
  Next
End Sub
 
Upvote 0
Can I implement for specific sheets instead of each sheet ?
Yes, but if the following is not what you want or you can't adapt it then we would need details of how to determine which sheets to do this for or which sheet to not do it for.

This will act on the the left-most two sheets in the workbook only

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  If Sh.Index <= 2 Then
    On Error Resume Next
    Sh.Name = "INVOICE " & Sh.Range("F" & Rows.Count).End(xlUp).Text
  End If
End Sub
 
Upvote 0
great !
just curiosity if I put specific sheets in array supposes rename
like this
VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  If Sh.Index = Array("IN1", "SH1") Then
    On Error Resume Next
    Sh.Name = "INVOICE " & Sh.Range("F" & Rows.Count).End(xlUp).Text
  End If
End Sub
how can I do that correctly.
 
Upvote 0
I'm not really sure about how that would work. We could certainly do it by sheet name but that would only work once. If the last value in column F might change, the worksheet name would not update. On the other hand if this is to be a one-off worksheet name change then perhaps the Workbook_SheetActivate is not the best 'event' to use.

Perhaps you could explain in more detail about how the sheet names should change and if the name should change again if the last value in column F changes.
Why were you trying to use for the worksheet activate event?
 
Upvote 0
if the name should change again if the last value in column F changes.
Yes
Why were you trying to use for the worksheet activate event?
ok if you see there is another way to do that I don't mind
Why were you trying to use for the worksheet activate event?
it's not important , I don't have to use worksheet activate event.
Perhaps you could explain in more detail about how the sheet names should change and if the name should change again if the last value in column F changes.
when change the amount in lastrow for column F for any sheet or sheets together then should change at once for sheets
 
Upvote 0
when change the amount in lastrow for column F for any sheet or sheets together then should change at once for sheets
Then post #2 remains the solution?
Yet in post #3 you seemed to be asking about specific sheets, not for any sheet.
I am confused.
 
Upvote 0
Yet in post #3 you seemed to be asking about specific sheets, not for any sheet.
I am confused.
I meant when I have sheets(SH1,IN1)
one of them change in last row then will change for sheets sh1,in1 again
and if change in last row for both sheets then will change for sheets sh1,in1 again.
example:
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
232KM 13R22.5 MA11 KOR12.002,000.0024,000.00
24TOTAL22.0049,550.00
IN1
Cell Formulas
RangeFormula
D24,F24D24=SUM(D22:D23)
F22:F23F22=D22*E22


OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR5.00500.002,500.00
232VEGA 55A R KOR4.00425.001,700.00
243LARGEST 60A L HIGH JAP4.00440.001,760.00
254KM 215/65R16 TA31 KOR5.00460.002,300.00
265265/70R16 ALGERIA8.00630.005,040.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR8.00880.007,040.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
3515KM 205/65R16 HS63 KOR4.00450.001,800.00
3616GC 385/65R22.5 AT131 CHI12.001,640.0019,680.00
3718KM 235/65R17 HP71 KOR1.00875.00875.00
3819KM 235/55R19 PS71 KOR2.00670.001,340.00
3920BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
4021KM 13R22.5 MA11 KOR12.002,000.0024,000.00
41TOTAL94.00110,460.00
SH1
Cell Formulas
RangeFormula
D41,F41D41=SUM(D22:D40)
F22:F38F22=E22*D22
F39:F40F39=D39*E39



result
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
232KM 13R22.5 MA11 KOR12.002,000.0024,000.00
24TOTAL22.0049,550.00
INVOICE 49,550.00
Cell Formulas
RangeFormula
D24,F24D24=SUM(D22:D23)
F22:F23F22=D22*E22



OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR5.00500.002,500.00
232VEGA 55A R KOR4.00425.001,700.00
243LARGEST 60A L HIGH JAP4.00440.001,760.00
254KM 215/65R16 TA31 KOR5.00460.002,300.00
265265/70R16 ALGERIA8.00630.005,040.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR8.00880.007,040.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
3515KM 205/65R16 HS63 KOR4.00450.001,800.00
3616GC 385/65R22.5 AT131 CHI12.001,640.0019,680.00
3718KM 235/65R17 HP71 KOR1.00875.00875.00
3819KM 235/55R19 PS71 KOR2.00670.001,340.00
3920BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
4021KM 13R22.5 MA11 KOR12.002,000.0024,000.00
41TOTAL94.00110,460.00
INVOICE NO 110,460.00
Cell Formulas
RangeFormula
D41,F41D41=SUM(D22:D40)
F22:F38F22=E22*D22
F39:F40F39=D39*E39





change in IN1 sheet.
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
232KM 13R22.5 MA11 KOR12.002,000.0024,000.00
243KM 14R22.5 MA11 KOR12.002,000.0024,000.00
25TOTAL22.0073,550.00
INVOICE 49,550.00
Cell Formulas
RangeFormula
D25D25=SUM(D22:D23)
F22:F24F22=D22*E22
F25F25=SUM(F22:F24)


result for both sheets
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
232KM 13R22.5 MA11 KOR12.002,000.0024,000.00
243KM 14R22.5 MA11 KOR12.002,000.0024,000.00
25TOTAL22.0073,550.00
INVOICE 73,550.00
Cell Formulas
RangeFormula
D25D25=SUM(D22:D23)
F22:F24F22=D22*E22
F25F25=SUM(F22:F24)


keep second sheet without change.
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR5.00500.002,500.00
232VEGA 55A R KOR4.00425.001,700.00
243LARGEST 60A L HIGH JAP4.00440.001,760.00
254KM 215/65R16 TA31 KOR5.00460.002,300.00
265265/70R16 ALGERIA8.00630.005,040.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR8.00880.007,040.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
3515KM 205/65R16 HS63 KOR4.00450.001,800.00
3616GC 385/65R22.5 AT131 CHI12.001,640.0019,680.00
3718KM 235/65R17 HP71 KOR1.00875.00875.00
3819KM 235/55R19 PS71 KOR2.00670.001,340.00
3920BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
4021KM 13R22.5 MA11 KOR12.002,000.0024,000.00
41TOTAL94.00110,460.00
INVOICE NO 110,460.00
Cell Formulas
RangeFormula
D41,F41D41=SUM(D22:D40)
F22:F38F22=E22*D22
F39:F40F39=D39*E39
 
Last edited:
Upvote 0
I still do not understand.

You have marked post #2 as the solution.
Does it solve your problem?

If not, why is it marked as the solution?
and
In what way does it not work for you?
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

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