Add a sheet counter in a cell - excel

i have a excel file with a lot of sheets. i want to make a sheet counter to put in a specific cell of each one of those sheets "sheet x of Y"
for example, if you are on the sheet 3 of 5, in the cell "X5" must to say "sheet 2" and in the cell "X6" say "of 5"
each sheet have a button to add other sheet, so the sheet counter must be update every time you add a sheet
i tried this code but doesn't work, it only puts "0" in the first sheet
Public Sub CountWorkSheets()
If Application.Sheets.Count = 2 Then
Sheets(1).Range("X5").Value = 1 And Sheets(2).Range("X5").Value = 2
End If
End Sub

I suggest go to ThisWorkbook and hook into the NewSheet event
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Call AddSheetCounters
End Sub
Public Sub AddSheetCounters()
Dim i As Long, n As Long
n = Me.Sheets.Count
For i = 1 To n
Me.Sheets(i).Range("X5").Value = "Sheet " & i
Me.Sheets(i).Range("X6").Value = "of " & n
Next i
End Sub
Edit the text for ranges X5 and X6 as you need. Every time a new sheet is added the macro is run. Unfortunately when a sheet is deleted, it won't and things will get out of sync.

Related

Get Data from Excel Sheets to mastersheet and edit with doublecklick

i have an Workbook with 5 Sheets where you can add Data while Using a Userform. The Data begins in each Sheet in row 10, since i nead the rows above 10 for my interface. I have also an Mastersheet, where the Data from these specific Sheets should be copied and should be edited if needed by DoubleClick and an Unserform. I Managed to write the Code for Copying the Data from these Sheets, but I also need the Sheetname next to the Data in Column A, so it's clear where the Data is from. The Data begins also in Row 10 of the Mastersheet.
1.Problem: Can't get the Sheet names in Column A and the Data to start in Column A.
2. If one Sheet is empty it Copies the Headers into the Mastersheet
3. By Double Click in the Row my Userform doesn't appear.
When i write The Sheet Name in Column B of the Mastersheet, the Data gets edited. But The Mastersheet should refresh itself when the editing is done
I would really appreciate if someone could help me, since i just startet with vba and don't know What to do.
`
Private Sub Worksheet_Activate()
Dim Ws As Worksheet
Me.Rows("6:" & Rows.count).Clear
Me.Range("A9:Z9").Value = Array("Markt".... (And So on)
For Each Ws In ThisWorkbook.Worksheets
If Ws.Name <> "Summary" And Ws.Name <> "Startseite" And Ws.Name <> "Agenda" (And so on) Then
Ws.Rows("10:" & Ws.Cells.Find("*", searchdirection:=xlPrevious).Row).Copy Me.Range("A" & Me.Cells.Find("*", searchdirection:=xlPrevious).Row + 1)
End If
Next Ws
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
Dim Sheet As String
Dim POS As Integer
Cancel = True
If Range(Target.Address).Row >= 10 Then
Sheet = Cells(Target.Row, 2).Value 'Get The Sheet Name from Column B I could change that bu I need the Sheet name in a Column to do that
If IsNumeric(Cells(Target.Row, 1).Value) Then
POS = CInt(Cells(Target.Row, 1).Value)
Else
POS = 0
End If
Else
Exit Sub
End If
If POS = 0 Then
Exit Sub
Else
Sheets("Code").Range("G2") = Sheet
Sheets("Code").Range("G3") = POS
End If
Edit.Show
End Sub
`
I tried changing the code but it didn't work

Find all rows in another sheet matching key

I have two sheets in my Excel workbook, one called Customer and the other called Product:
I wish to make a Lookup table in the Customer sheet, where in for example cell G2, above the lookup table, I put in the customer_id I wish to find all rows for in the sheet called Product. So, for example if I in G2 put in "1", I will get the two matching rows in sheet "Product". I have tried using VLOOKUP, but I always just match the first row, and none of the others.
EDIT:
in the lookup table you will only get the information in the sheet named "Product": if you put in "1" you will get rows 2 and 3 from the Product sheet. Customer_id only has one row per customer_id in the sheet called "customer", whereas in the sheet called "Product" you can have many rows per customer_id
Your source data for Pivot Table would be your Product Sheet:
Create a Pivot Table, and take Customer Field to filter section in the setup:
Just changing manually the number in Cell B2 will return the Product related To that customer id.
NOTE: If you input a Customer_id that is not in the Product Sheet, an alert Msgbox will pop up warning you about that. So if you see at any moment that alert, it means that customer_id has 0 records in PRoduct Sheet :)
Hope this helps
You could try Workbook_Change:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wsCus As Worksheet, wsPro As Worksheet
Dim LastRow As Long, ID As Long, i As Long
Dim arr As Variant
Dim FullRecord
With ThisWorkbook
Set wsCus = .Worksheets("Customer")
Set wsPro = .Worksheets("Product")
End With
If Not Intersect(Target, wsCus.Range("G2")) Is Nothing And Target.Count = 1 Then
ID = Target.Value
With wsPro
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
arr = .Range("A2:A" & LastRow)
For i = LBound(arr) To UBound(arr)
If arr(i, 1) = ID Then
If FullRecord = "" Then
FullRecord = i + 1
Else
FullRecord = FullRecord & ", " & i + 1
End If
End If
Next i
Application.EnableEvents = False
If FullRecord = "" Then
wsCus.Range("I2").Value = "No match found"
Else
wsCus.Range("I2").Value = "Matched lines for ID (" & ID & "): " & FullRecord
End If
Application.EnableEvents = True
End With
End If
End Sub
Instructions:
Once you have your workbook open press ALT & F11 to open VBA Editor.
Double click on the Customer sheet on the left upper part.
Select Worksheet.
Change event.
Paste the code as you see in the picture.
Then save the workbook as Excel Macro - Enabled Workbook, close VBA Editor and change G2 value.
Steps:
Results:
Find Match
No Match

Order sheets based on input in Excel spreadsheet

In the Excel spreadsheet the user can define the order of the sheets within Column A:
A B
1 Sheet3
2 Sheet4
3 Sheet1
4 Sheet2
5
Once the user enterred the order in Column A he/she can click a button which is linked to this VBA code:
Sub Move()
Sheets(Sheet1.Range("A2").Value).Move After:=Sheets(Sheet1.Range("A1").Value)
Sheets(Sheet1.Range("A3").Value).Move After:=Sheets(Sheet1.Range("A2").Value)
Sheets(Sheet1.Range("A4").Value).Move After:=Sheets(Sheet1.Range("A3").Value)
End Sub
This VBA puts the sheets in the order based on the inputs from the user in Column A. All this works fine so far
Now I have the issue that the number of sheets varies so it can happen that instead of only 4 sheets there will be 8 or 10 or 15 and so on. In this case it would be necessary to add all those sheets manually to the VBA code.
Is it possible to make the VBA code more dynamically. Something like an array for the values in Column A and a VBA like this:
Sub Move()
MoveSheets based on Array {Sheet1.Range("A1:A5")}
End Sub
I think this will work if your sheet names are on "Sheet1" in B4 going down.
Sub x()
Dim n As Long, i As Long, r As Range
Application.ScreenUpdating = False
With Sheets("Sheet1")
Set r = .Range("B4", .Range("B" & Rows.Count).End(xlUp))
For n = r.Count To 1 Step -1
Sheets(r.Cells(n).Value).Move after:=Sheets(Sheets.Count - i)
i = i + 1
Next n
Application.Goto .Range("A1")
End With
Application.ScreenUpdating = True
End Sub

Copy Check Box to Every 5th Cell with Command Button

I have an issue which i can't solve.I wrote this code:
Private Sub CommandButton2_Click()
Sheets("sheet2").OLEObjects("CheckBox1").Copy
Sheets("sheet3").Range("V7").PasteSpecial
End Sub
This code copy a checkbox from (sheet 2) to (sheet 3) starting from V7 cell. Now I want the next time I press the command button to paste the data to cell V12,next time to V17 etc. My vba knowledge is not very good as you can see.
This code will see how many checkboxes are already in the sheet you are pasting to and add 5 rows for each check box, then paste five rows under the last one.
Private Sub CommandButton2_Click()
' copy checkbox
Sheets("sheet2").OLEObjects("CheckBox1").Copy
Dim wks As Worksheet
Set wks = Sheets("Sheet3")
Dim cb As OLEObject, i As Integer
'determine how many boxes are already there and get count of cell to paste to
i = 7
For Each cb In wks.OLEObjects
If InStr(1, cb.Name, "CheckBox") Then i = i + 5
Next
'paste new checkbox
Sheets("sheet3").Range("V" & i).PasteSpecial
End Sub
Use a global variable. These must be at the top of your sheet, module, or form code above all subs and functions.
Then use that as the row number in your range. Range("V" & lRow)
Private lRow As Long
Private Sub CommandButton2_Click()
'Let's check if this is the first time the button has been used.
If lRow = 0 then
lRow = 7
Else
'Increment the row from the one we wrote to last time.
lRow = lRow + 5
End If
'Do the copy
Sheets("sheet2").OLEObjects("CheckBox1").Copy
Sheets("sheet3").Range("V" & lRow).PasteSpecial
End Sub
I dont know what data you got between in Sheet(3).Range("V7") and Sheet(3).Range("V12")
but juste before you're PasteSpecial, you shoud keep track where was the last time you paste data in Sheets("sheets3") in a specific cell in Sheet("sheets3"), in exemple : Sheets("Sheet3").Range("A1")
Then you'll be able to pastespecial to this cell 5 row under like this :
Sheets("sheet3").Range(Sheets("Sheets3").Range("A1").Offset(5,0)).PasteSpecial
right after that you update the Sheets("Sheets3").Range("A1") = Sheets("sheet3").Range(Sheets("Sheets3").Range("A1").Offset(5,0)).Address
So this should do the work :
Private Sub CommandButton2_Click()
Dim oWsSource as Worksheet
Dim oWsDestination as Worksheet
Set oWsDestination = ThisWorkbook.Worksheet("Sheets3")
Set oWsSource = ThisWorkbook.Worksheet("Sheets2")
'Do the copy
oWsSource.OLEObjects("CheckBox1").Copy
oWsDestination.Range(oWsDestination.Range("A1").Value).Offset(5,0)).PasteSpecial
oWsDestination.Range("A1").Value = oWsDestination.Range(oWsDestination.Range("A1").Value).Offset(5, 0)).Address
End Sub
All the answers put the first checkbox but the next one put it again to the same cell as before.I don't know if its matter but I use excel 2010.

Adding from next sheet to the end sheet

Assume I have n sheets. My second sheet is called "Calc", which is where I do my summation/calculation.
I'd like to add everything A1, A2... A1000, to Z1, Z2...Z1000 from sheet 3 (sheet after Calc) to sheet n.
These are imported sheets. I do not know the name of these sheets and I am not allowed to change them.
Any of the sheets between and including sheet 3 to sheet n can be removed or added at any time.
First I was thinking of trying =SUM(''!A20), but it automatically changes the '' to the first and last sheet.
When I remove the last sheet, it gives me error and the calculation fails. I was thinking of doing indirect, but it would be very tedious, as I cannot drag to change the cells in sheet 3 to sheet n.
for example: =SUM(INDIRECT("'"&F2&"'!C4"),INDIRECT("'"&F3&"'!C4")), the C4 does not change as I drag them across the board.
Any other idea?
Let me guess:
You have many sheets;
You can delete or add new sheet;
There are data in Range("A1:Z1000") in every sheet;
You want sum every sheet's Range("A1:Z1000") in sheet("Calc").Range("A1:Z1000");
Every sheet has data in A1:Z1000.
Sum in "Calc" sheet.
If so try this:
Sub SumEverySheet()
Dim Sh As Worksheet
Dim i, j As Integer
Dim cellValue
For Each Sh In Worksheets
If Sh.Name <> "Sheet1" And Sh.Name <> "Calc" Then
' row: 1 to 1000
For i = 1 To 1000
' A - Z (1 - 26)
For j = 1 To 26
cellValue = Sheets("Calc").Cells(i, j)
sheetValue = Sh.Cells(i, j)
Sheets("Calc").Cells(i, j) = sheetValue + cellValue
Next j
Next i
End If
Next Sh
End Sub
Put this macro in Workbook_AfterSave can make it run automatically after save.
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
MsgBox "after save"
Call SumEverySheet
End Sub
Hope this will help you.

Resources