Adding from next sheet to the end sheet - excel

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.

Related

Work on data from different worksheets in a single worksheet

I have an Excel file that consists of worksheets named 1, 2, 3, and so on. These worksheets involve data that I would like to analyze in the same ranges. I want to analyze these data in a single worksheet (Let's called it Master Worksheet).
So, to give an example, for the B2 cell of the Master worksheet, I would like to take average of the range B2:B11 of worksheet 1. For C2 cell, I would like to take the minimum of C2:C11 of worksheet 1, and so on.
Similarly, for B3 cell of the Master worksheet, I would like to take average of the range B2:B11 of worksheet 2. For C3 cell, I would like to take the minimum of C2:C11 of worksheet 2, and so on.
For this, I could not find any shortcuts in Excel, so I thought writing an VBA code would work. However I am not very familiar with it, so I am a bit stuck.
Sub MasterWorksheet()
Dim i as integer
Dim TotalNumberofWorksheets As Integer
TotalNumberofWorksheets = 99
For i to TotalNumberofWorksheets:
ActiveWorkbook.Worksheets("Master Worksheet").Cell(2, i+1).Value = ActiveWorkbook.Worksheets(i).Range("B2:B11").Average()
ActiveWorkbook.Worksheets("Master Worksheet").Cell(2, i+1).Value = ActiveWorkbook.Worksheets(i).Range("C2:C11").Min()
...
End Sub
I tried this, however it did not work. Any idea on how to write it?
The correct syntax for a For loop is
For i = 1 To TotalNumberofWorksheets 'you can make it start at i=2 too if need be
'or go backwards from TotalNumber and then use TotalNumberofWorksheets Step = -1
'your code here
Next i
So no need for : after the end-step and a needed i = startValue
I doubt you have 99 worksheets in your workbook, hence why I would change your code to
Sub MasterWorksheet()
Dim i As Integer
Dim TotalNumberofWorksheets As Integer
TotalNumberofWorksheets = ActiveWorkbook.Sheets.Count
For i = 1 To TotalNumberofWorksheets
If ActiveWorkbook.Worksheets(i).Name <> "Master Worksheet" Then
ActiveWorkbook.Worksheets("Master Worksheet").cell(2, i + 1).Value = ActiveWorkbook.Worksheets(i).Range("B2:B11").Average()
ActiveWorkbook.Worksheets("Master Worksheet").cell(2, i + 1).Value = ActiveWorkbook.Worksheets(i).Range("C2:C11").Min()
'etc
End If
Next i
End Sub
This way you also have a security to not include the master sheet.

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

VBA - Copy Function from one sheet to another creating #REF formula not sure why

I created a Copy function to copy the row data from 1 worksheet to another worksheet. The Copy function worked fine but then when I see the cell in which data is copied I see there is formula generated on the cells as shown below . I dont know why?
"=#REF!$P20(1-$DX20)" This formula I can see in the cells in which data was copied into.
Sub Process_Calc()
Dim i As Long
Dim n As Long: n = 20
Dim wsData As Worksheet, wsCalcAndOutput As Worksheet, wsTemp As Worksheet 'specify each type
With Worksheets("Project_and_capex_costs")
For i = 10 To 10
If .Cells(i, 5) <> "" Then
Range(.Cells(i, "BP"), Cells(i, "DC")).Copy _
Worksheets("DC_price_calc").Cells(n, "D") 'start at row 7?
'n = n + 1 'update so that we don't overwrite next time
End If
Next
End With
End Sub

Add a sheet counter in a cell

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.

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

Resources