I have a production workbook that has a tab for each month of the year and a YTD summary tab. Each monthly worksheet has the rows that show the different events or actions that make up a workday and the columns have the date as the header row. We do not work on Saturday or Sunday but those days appear on each of the spreadsheets. I am wanting to know how can i create a macro or VBA code to be able to automatically hide columns if the cell in that columns contains Sat or Sun. As there are multiple Saturdays and Sundays in a month it would hide multiple columns on each spreadsheet. Row 34 is the day of wek on each of the spreadsheets, utilizing a three digit day of week - Mon, Tue, Wed, etc, so i need something that says if that cell is Sat or Sun - hide that column and my columns go from B to AG. We have one of these spreadsheets for each of our over 50 workers so I could go in and manually hide them but that would take a lot of time and I know that there is a more efficient solution.
Thanks
I'm assuming you know how to set up and run VBA code as a macro? If not, see this: http://office.microsoft.com/en-us/excel-help/create-or-delete-a-macro-HP010342374.aspx#BMcreatemacrovba
Try this for the code.
Sub HideWeekends()
Dim i as integer
Dim sht as Worksheet
For each sht in ActiveWorkbook
For i = 1 to 31
If sht.Cells(34,i) = "Sat" Or "Sun" then
sht.Cells(34,1).EntireColumn.Hidden = True
End if
Next
Next
End Sub
You will have to modify this to match your spreadsheet. If the first day of the month is actually in column C instead of column A, then change
sht.Cells(34,i)
to
sht.Cells(34,i+2)
and so on.
I'm also doing this on a computer without excel, so let me know how that works and I can work with you!
Tested this based on your description above. Should work as expected.
Sub Hide_Columns_Based_On_Criteria()
Dim iCntr As Long, WS_Count As Integer, I As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
For I = 1 To WS_Count
ActiveWorkbook.Worksheets(I).Select
For iCntr = 2 To 33 Step 1
If Cells(34, iCntr) = "Sat" Then
Columns(iCntr).EntireColumn.Hidden = True
End If
If Cells(34, iCntr) = "Sun" Then
Columns(iCntr).EntireColumn.Hidden = True
End If
Next iCntr
Next I
End Sub
You can make adjustments to iCntr if the column range changes from B to AG
For iCntr = 2 To 33 Step 1
Related
I have a workbook that is used to schedule the next upcoming task on a job. each row has 28 cells, each cell represents a day of the week within the 4 weeks lookahead. I made a formula to check the date of the cell with the start and end date of the task and fill the cell accordingly.
Here is the formula:
=IFERROR(IF(AND(ISNUMBER(SEARCH("Delivery",$D16)),VALUE(F$10)=VALUE('Calculation
New'!$AO53)),"D",IF(AND(ISNUMBER(SEARCH('Calculation
New'!$BH$13,$AJ16)),VALUE(F$10)>=VALUE('Calculation
New'!$AO53),VALUE(F$10)<=VALUE('Calculation
New'!$AP53)),"N",IF(AND(ISNUMBER(SEARCH('Calculation
New'!$BH$12,$AJ16)),VALUE(F$10)>=VALUE('Calculation
New'!$AO53),VALUE(F$10)<=VALUE('Calculation
New'!$AP53)),"E",IF(AND(VALUE('Calculation
New'!$AO53)=VALUE('Calculation New'!$AP53),F$10='Calculation
New'!$AO53,NOT(ISNUMBER(SEARCH('Calculation
New'!$BH$9,$D16)))),"SF",IF(AND(ISNUMBER(SEARCH('Calculation
New'!$BH$9,$D16)),VALUE(F$10)>=VALUE('Calculation
New'!$AO53),VALUE(F$10)<=VALUE('Calculation
New'!$AP53)),"I",IF(AND(VALUE(F$10)>VALUE('Calculation
New'!$AO53),VALUE(F$10)<VALUE('Calculation
New'!$AP53)),"X",IF(VALUE(F$10)=VALUE('Calculation
New'!$AO53),"S",IF(VALUE(F$10)=VALUE('Calculation
New'!$AP53),"F","")))))))),"")
a few things to that formula:
D16:D85 on the sheet "SIS" is the Task description where to look for certain words
BH9 on sheet "Calculation New" contains a word to compare to. The range of words is BH3:BH13
F10:AF10 on the sheet "SIS" contains the date for the cells below of the day of the week
AO53:AO122 on sheet "Calculation New" contains the start date of a task
AP53:AP122 on sheet "Calculation New" contains the End date of a task
currently, I got 70 Rows times 28 cells and each cell has this formula in it. Now I want to rather use a VBA code do the same thing, but I am having a hard time to get started. I am not very experienced with VBA. I researched in regards to nesting For each loop but so far I am not succeeding.
I would appreciate any help I can get.
Thank you in advance
Dan
here is the code I have written so far not complete but I am stuck and need some advice
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim SDate As Range
Dim EDate As Range
Dim WDate As Range
Set SDate = Worksheets("Calculation New").Range("SDate")
Set EDate = Worksheets("Calculation New").Range("EDate")
Set WDate = Worksheets("Calculation New").Range("WDate")
For SDate = 1 To Worksheets("Calculation New").Range("SDate").End(xlDown) 'lenght of range varies
'For WDate = 1 To 28 ' length is always same
'If cell = WDate Then 'i want to compare each cell of WDate with the start date
'cell = "X"
'Next
Next
End Sub
To get you started with VBA, you might want to start here. There's a lot of questionable sites offering VBA code but this is directly from Microsoft and covers the basics. Happy coding!
https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office
For loops can be tricky - generally you can start with an array saying r = ActiveSheet.UsedRange and loop through it
Sub nestedLoop()
r = ActiveSheet.UsedRange
For i = LBound(r) To UBound(r)
For j = LBound(r, 2) To UBound(r, 2)
'evaluate r(i,j) do something
'Debug.print r(i,j)
Next j
Next i
End Sub
I am attempting to learn some vba programming for excel, Long story short
I have a machine using an allen bradley plc, I have created a program in the plc to record hourly run statitistics, I have managed to get these to update live into an excel sheet, it uploads each hour for 24 hours. The machine runs on 3 shifts 6am to 2pm, 2pm to 10 pm, 10 pm to 6 am. In the factory we class each day as 6am to 6am.
I have written the following code, which copies the values from the plc and pastes them to the matching date, cell "c10" contains =today() then on sheet 2 it will paste the values to a calender under the matching date.
this is now working fine however i would like to change it so that under each date it contains 6am to 6 am values rather than 24 hours worth.
the issue i have is that cell c10 (todays date) will update after 12am and therefore the paste destination will change.
heres my code
Private Sub work_test()
'set variables
Dim Day As Date
Dim rfound As Range
Dim frow, fcol As Integer
Dim sh1, sh2 As Worksheet
'set sheets
Set sh1 = Sheets("sheet1")
Set sh2 = Sheets("sheet2")
'sets day as current day, finds matching day in sheet2
Day = sh1.Range("c10")
Set rfound = sh2.Range("7:11").Find(Day, LookIn:=xlValues)
If Not rfound Is Nothing Then
frow = rfound.Row
fcol = rfound.Column
sh1.Range("c11:c34").Copy sh2.Cells(9, fcol)
Else
MsgBox "No match found"
End If
'runs timer
Call timer
End Sub
Sub timer()
'repeats cell update timer
Application.OnTime Now + TimeValue("00:01:00"), "work_test"
End Sub
Hope someone can help, not looking for a complete solution, just a bit of help in the correct direction
Thanks
These are two of many ways achieving what yo want:
1.- Replace formula in C10 with this one:
= TODAY() + IF( NOW() - TODAY() < TIME(6,0,0) , -1 , 0 )
Formula above validates the time and it less that 06:00:00 then rest one to the date. Thus anything between midnight and 06 AM will be taken as the day before
2.- In your code replace this line:
Day = sh1.Range("c10")
with this:
Day = Date + IIf(Time < TimeSerial(6, 0, 0), -1, 0)
Same as in point 1 above, only that since you are using VBA there is no need to have the date as a formula in the worksheet, the date of the machine can be obtain that directly in the VBA and proceed from there.
I have made a calendar with the dates throughout the year which I will use to register new objects every month. The month itself isn't important - I just am using month as a reference to find the correct range of dates so at the moment looks.
FEB 01/02/2014
FEB 02/02/2014
FEB 03/02/2014
FEB 04/02/2014
FEB 05/02/2014
MAR 01/03/2014
MAR 02/03/2014
JUN 02/06/2014
Jun 03/06/2014
The whole year is in place. I have a drop down menu on the first page detailing the month, I would like a macro that uses the month selected as a reference and then copies all the dates associated with that month to a separate column.
Any ideas?
The following code should be close - adapt as needed. It was not written for efficiency - unless you have many thousands of items to copy, this will take "no time at all". The Application.ScreenUpdating trick stops the screen from flickering (and makes it faster) during the update.
Option Compare Text
Sub moveStuff()
Dim rLabel As Range
Dim rLabelSource As Range
Dim rDestination As Range
Dim c, L
' first label:
Set rLabel = ActiveWorkbook.Worksheets("source").Range("A2")
' extend all the way down:
Set rLabel = Range(rLabel, rLabel.End(xlDown))
Set rLabelSource = ActiveWorkbook.Worksheets("destination").Range("A1")
Set rLabelSource = Range(rLabelSource, rLabelSource.End(xlToRight))
Application.ScreenUpdating = false
' labels in the top row:
For Each L In rLabelSource.Cells
' write results in the next row down:
Set rDestination = L.Offset(1, 0)
For Each c In rLabel.Cells
If c.Value = L.Value Then
rDestination.Value = c.Offset(0, 1).Value
Set rDestination = rDestination.Offset(1, 0)
End If
Next c
Next L
Application.ScreenUpdating = true
End Sub
In this case, dates and labels are in the sheet called "source":
And the destination sheet (with labels in the top row, and copied dates appearing below them) in sheet called "destination":
Obviously there are many ways to make this cleaner (clear all the space below the labels in destination before copying, so no old values are left behind, for example). And in "real" code you would add error handling, etc.
This should get you going though.
I have an Excel report that generates everyday that have dates that differ from anywhere to three days to two due to the uneven amount of calendar days in any given month. The report, in column F looks like this below. Today's date is 07/16/13 and the process generating the report is configured to show anything greater then 180 days so that we catch the right data.
01/12/2014
01/15/2014
01/15/2014
01/12/2014
01/15/2014
I'd like to delete all rows that are not equal to 1/15/14. I don't know if there is a way to add another column with just 1/15 and then delete the rest? Any help or direction would be greatly appreciated.
We are now going into the file everyday and doing a sort by date and then delete. I'd like to automate it.
Thank you
Use a loop to iterate through the rows and remove if less than your criteria. I referenced this thread for row deletion code: Delete a row in Excel VBA.
I'm assuming your dates are in Column A and start at Row 1.
Private Sub Remove_Rows()
Dim CutoffDate As Date
CutoffDate = Date + 180
Dim ws As Worksheet
Set ws = ActiveSheet
Dim RowCounter As Integer
RowCounter = 1
Do While ws.Cells(RowCounter, 1) <> ""
If ws.Cells(RowCounter, 1) < CutoffDate Then
ws.Rows(RowCounter).Delete
End If
RowCounter = RowCounter + 1
Loop
End Sub
I have an excel workbook created by an executable with data for days of the month on separate worksheets. 'Sheet 1' of the executable also has the days of the month listed. I would like to write a macro that will show/hide the worksheets based on the date in 'Sheet 1'.
For Instance, if the data for the month of Jan has days 1,2,3,4,5,11,12 displayed then the macro should show only the corresponding worksheets for Day1, Day2, Day3, Day4,Day5 and hide Day6 through Day10 and show Day11 and Day12. Any pointers are appreciated.
Thank you.
public sub setSheetVisiblity()
'Load the data from sheet 1 into a collection
'I'm making the assumption that you just have days listed horizontally from
'1A to 1*
Dim currentColumn as Integer
Dim activeDayCollection as Collection
currentColumn = 1
Set activeDayCollection = new Collection
While Cells(currentColumn, 1).Value <> ""
activeDayCollection.add Cells(currentColumn, 1).Value
currentColumn = currentColumn + 1
Wend
'Make every sheet invisible/visible
For each currentWorksheet as Worksheet in Worksheets
If currentWorksheet.Name == "Day" + activeDayCollection.Item 1 Then
currentWorksheet.Visible = true
activeDayCollection.Remove 1
Else
currentWorksheet.Visible = false
End If
Next currentWorksheet
end sub
The code works off of the assumption that the days in your first sheet are in increasing order, the sheets are named Day###, where ### is the day number, and you will probably have to add another line to manually unhide your first sheet. I don't have vba with me so this code might have some syntax errors, but it should get you going in the right direction.