Order sheets based on input in Excel spreadsheet - excel

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

Related

Hide multiple row if their requirement is met

I am new to coding in anything, this project is the first time I have coded. I am trying to hide multiple row based on individual requirement. The requirement is if in a specific cell of the same row there is a space or is empty, the row will be hidden, if it is hidden and there is anything else, the row will be shown. The code need to work on specific worksheet as I have multiple worksheet where there is row to hide or columns to hide at different place.
There are 2 different pieces of code that I tried which don't work.
This picture represent the Excel sheet I am currently trying to hide row:
My goal is to hide row between 8 to 37 if there is there is a space or if it is empty, depending what the code inside the cell point at for the cell A8 to A37. if I activate the code, in the image only the row 8, 9 and 10 should be visible, 11 to 37 should be hidden.
So far I have tried these two pieces of code:
Sub C1()
Set ws = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")
Dim C As range
For Each C In range("A8:A37")
If C.Value = " " Then
C.EntireRow.Hidden = True
Else
If C.Value = Empty Then
C.EntireRow.Hidden = True
Else
C.EntireRow.Hidden = False
End If
End If
Next C
End Sub
This code work as intended except that it is not tied to a sheet. "Set ws = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")" is not working as well as a couple other code I tried, they point to an error. So when I try to use this code it will work on the active sheet and not "FR-3-06_Jeux Prod."
Sub Hide_column_and_Row_F_3_6()
Dim NbreLigne As Integer
Dim tableau As range
Set wrkshtDoc = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")
Set tableau = wrkshtDoc.range("A8:A37")
NbreLigne = tableau.Rows.Count
For k = 1 To NbreLigne
If tableau(1, k) = " " Then
tableau(1, k).EntireRow.Hidden = True
ElseIf tableau(1, k) = Empty Then
tableau(1, k).EntireRow.Hidden = True
Else
tableau(1, k).EntireRow.Hidden = False
End If
Next k
End Sub
This code only works as intended when I try to hide columns as in replace "row" in the code with "columns". There is sheet in my file where is it columns I need to hide and since this code is working I tried to reuse it... what it is currently doing is hiding row with "test", line 8 only. It wont hide the empty cell.
what would be the error or what would be needed to hide row with the requirement? I know that code #2 work with columns...
You are almost there with code1, you only need to add:
For each C in ws.Range("A8:A38")
Because you add ws. in front of the Range, it knows which sheet to apply it on.
Good luck!
Hide Blank Rows
Option Explicit
Sub HideBlankRows()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets("FR-3-06_Jeux Prod.")
Dim Cell As Range
For Each Cell In ws.Range("A8:A37").Cells
Cell.EntireRow.Hidden _
= IIf(Len(Trim(CStr(Cell.Value))) = 0, True, False)
Next Cell
End Sub

Excel VBA 2 Sheets compare and fill if empty

I got 2 Sheets each with a table.
The tables on those sheets have the same format, same length etc.
Row 1 is the days of the month and column A are the employees.
Now I want to compare those two sheets. Sheet1 is the main, if one cell is empty I would like to check if there is data in Sheet2 same cell. If yes, copy it into Sheet1. If no, leave empty.
Is this possible?
VLOOKUP semms like the simple solution but I cant figure out how to do it with 2 criterias (Name and Date)
For filling empty cell i got this code but dont know if thats the best method.
Sub Fill_empty_cell()
Set MR = Range("C3:X600")
For Each cell In MR
If IsEmpty(cell.Value) = True Then
cell.Value = "VLOOKUP????"
End If
Next
End Sub
Thanks for any help!
Daniel
Try this:
Sub Fill_empty_cell()
Dim sheet1 As Worksheet
Dim sheet2 As Worksheet
Dim rangeToCheck As Range
Set sheet1 = Sheets("Test1")
Set sheet2 = Sheets("Test2")
Set rangeToCheck = sheet1.Range("A2:D30")
For Each cellToCheck In rangeToCheck
If IsEmpty(cellToCheck.Value) = True Then
cellToCheck.Value = sheet2.Cells(cellToCheck.Row, cellToCheck.Column).Value
End If
Next
End Sub
Here you can set a Range by String...
Yes this is possible the easiest way would be using For loops and defining all cells in the range by number (cells(x,y)) and use an if statements to compare those for the different sheets (eg. sheet1.cells(x,y) to sheet2.cells.(x,y))
The if checks if its empty if sheet2 is empty if will stay empty but if somethings is their it will be put in sheet 1
Sub Fill_empty_cell()
Dim x As Long
Dim y As Long
x = 3
y = 3
For x = 3 To 21 'this is the column number of x I think please check
For y = 3 To 300
If ThisWorkbook.Sheets(1).Cells(y, x) = "" Then
ThisWorkbook.Sheets(1).Cells(y, x) = ThisWorkbook.Sheets(2).Cells(y, x)
End If
Next y
Next x
End Sub

Change a range of cells on one or more sheets based on a selected range of cells in a main sheet

I'd like to preface this by saying that I'm largely inexperienced with coding. I've been working on a project at work on my own, and finally ran into a wall that I can't solve by simple googling.
To explain briefly with pertinent details, I have a workbook with a varying number of worksheets that each have a specific cell for text (explained later), and a checkbox.
My goal is to select a range of cells, usually 1-5 in a row, and then have a button to change the color of the same selected cells of all sheets given a specific criteria. The criteria being that only the sheets that have "Office" listed in the aforementioned cell, and the check box un-checked, will be changed.
While I have no issue having Excel cycle through the sheets and do something only on the sheets that meet the criteria, the problem comes with trying to edit the same cells as are selected on the main sheet.
The coding I have come up with so far:
Dim cell As Range
Dim n As Integer
Set cell = Selection
If Range("AN6").Text = "Office" Then
For n = 1 To Sheets.Count - 2
If Sheets(n).Range("AN6").Text = "Office" And Sheets(n).CheckBox1.value = False Then
For Each cell In Selection
Sheets(n).Range(cell).Interior.ColorIndex = 56
Next cell
End If
Next n
End If
The error I am getting is: Application-defined or object-defined error.
Hopefully there is a way to execute this. I'd greatly appreciate any help I can get.
You can use the .Address property of a range. I'm pretty sure you're getting an error because your selection is in one sheet, and you try to refer to that range in another sheet. Using just the address of the range should fix that issue.
Dim cell As Range
Dim n As Integer
Set cell = Selection
If Range("AN6").Text = "Office" Then
For n = 1 To Sheets.Count - 2
If Sheets(n).Range("AN6").Text = "Office" And Sheets(n).CheckBox1.value = False Then
For Each cell In Selection
Sheets(n).Range(cell.Address(False,False)).Interior.ColorIndex = 56
Next cell
End If
Next n
End If
The only testing I did on this code was using this sub. I selected a range on my worksheet and ran the macro and the msg box said Q12:U26
Sub test()
Dim cell As Range
Set cell = Selection
MsgBox cell.Address(False, False)
End Sub
This should work:
Sub test()
With Selection
startRow = .Cells(1).Row
startColumn = .Cells(1).Column
endRow = .Cells(.Cells.Count).Row
endColumn = .Cells(.Cells.Count).Column
End With
If Range("AN6").Text = "Office" Then
For n = 1 To Sheets.Count - 2
If Sheets(n).Range("AN6").Text = "Office" And Sheets(n).CheckBox1.value = False Then
For Each cell In Range(Sheets(n).Cells(startRow, startColumn), Sheets(n).Cells(endRow, endColumn))
cell.Interior.ColorIndex = 56
Next cell
End If
Next n
End If
End Sub

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.

Re: Take a value (that is summed) in multiple sheets and insert into a master sheet

Re: Creating a master sheet from multiple sheets.
Multiple sheet description: table with many rows and columns. Columns headings are identical but rows vary. Each sheet is a date.
Task: to take a single value from a specific column (always happens to be column M). the value I want is the total of that column. Take this summed value and insert into a master sheet.
My attempt so far is:
Sub append_master_sheet()
Dim wAppend As Worksheet, wSheet As Worksheet
Dim LastRow As Long
Set wAppend = Worksheets("Master")
For Each wSheet In Worksheets
If wSheet.Name <> wAppend.Name Then
LastRow = WorksheetFunction.Max(3, wAppend.Cells(65536, 2).End(xlUp).Row)
wSheet.UsedRange.Resize(, 13).Copy Destination:=wAppend.Cells(LastRow, 2)
End If
Next wSheet
End Sub
1). it takes all 13 columns rather than only the 13th column. (I see that is because I have set it at 13 as I do not know how to cycle through the preceding columns and skip them to only return the 13th column data (and within this column return the total of the column, not the discrete line items
2) Besides returning all the data which is a problem, it actually consistently skips the final value in the column M.
Can you advise how to amend above code to
1) only return the summed value from column M in the multiple sheets (calendar dates) and insert into master.
thanks,
N
Is this what you are trying (UNTESTED)
Like I mentioned in the comment above, see THIS link on how to find a last row in a column.
I have commented the code so that you will not have a problem understanding it. But if you do, simply post back :)
Note: I am assuming that the last cell in Col M has the SUM
Option Explicit
Sub append_master_sheet()
Dim wAppend As Worksheet, wSheet As Worksheet
Dim wApLRow As Long, wShLRow As Long
Set wAppend = ThisWorkbook.Worksheets("Master")
'~~> Get the last row where the ouput should be placed
wApLRow = wAppend.Range("B" & wAppend.Rows.Count).End(xlUp).Row + 1
For Each wSheet In Worksheets
If wSheet.Name <> wAppend.Name Then
With wSheet
'~~> Fuind the last row in Col M which has the sum
wShLRow = .Range("M" & .Rows.Count).End(xlUp).Row
'~~> Copy over the values to Master Sheet
wAppend.Range("B" & wApLRow).Value = .Range("M" & wShLRow).Value
'~~> Increment the row for next output
wApLRow = wApLRow + 1
End With
End If
Next wSheet
End Sub

Resources