Insert a new column after every 7 columns with a formula - excel

I have a spreadsheet with 4000 records that keeps on updating new data. The column headers are Employee Name, Project, Activity, 06/29/2013, 06/30/2013... etc. These are individual dates starting from October of last year to this week and each of these columns contains numeric values. I'm trying to modify this week-wise. So if Sunday is considered as the 7th day of a week, then I want to subtract the values from this week's Sunday with the last week's Sunday. And I'd want it to continue doing that for the rest of the columns since the dates will keep on updating so it's going to be taxing to do it manually.
FYI I'm using Excel 2013.
I know how to perform basic operations but this one is beyond my understanding so any help would be much appreciated!
Sub insert_column_after_interval_7()
For colx = 7 To 200 Step 8
Columns(colx).Insert Shift:=xlToRight
Next
End Sub
I have specified the range as 200 but the data might exceed later on. So how do I modify it without giving any specific range?

you could try something along the lines of this:
Sub insert_column_after_interval_7()
dim iLastCol as integer
iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column ' same as CTRL+RIGHT ARROW
For colx = 7 To iLastCol Step 8
Columns(colx).Insert Shift:=xlToRight
Next
End Sub
of course, as you are likely adding more than 8 columns, you might be better off with a DO WHILE loop

Related

VBA to Copy Range on Year to Date basis

I have a worksheet with three columns. Column C:C has all calendar dates, N:N contains Data 1 and R:R Data 2.
The macro I am trying to write should check dates in Column C to find dates that are (EDIT) less than today and copy data of Column R:R to Column N:N.
Does this make sense? I have a feeling it's a super basic thing, but oh well, I spent one hour Googling without success.
Example
Thanks!
In Excel a date is just the number of days which starts counting from January 1st 1900. So January 1, 1900 is number 1. With this in mind, it is very easy to calculate with dates. Hours and minutes are fractions.
This sub should do the job:
Sub CopyData()
For i = 1 To 100
If Cells(i, 3) < DateTime.Date Then
Cells(i, 14) = Cells(i, 18)
End If
Next
End Sub
Replace the number 100 with the amount of lines you have or with some logic to count the number of lines.
Succes.

Modify each row matching several criteria

I'm designing a staffing schedule for projects.
Staff members are assigned on day-to-day basis to different projects. For planning purposes, we want both a "staff per project"-view as well as a "project per staff"-view.
This led me to create a dataset consisting of one entry for each day/staffmember combination. With a dozen-or-so staff members and 365 days, this is a lengthy list to manually modify.
Basically, this is the database:
The real thing is some 5000 lines. Changing anything a big CTRL+F party.
Ergo; a macro to add staff planning.
On a different sheet, I built a simple form for data-entry.
People select a staff member, enter a project name, beginning date, and ending date. The macro is then supposed to find the row (s) matching the staff member and the selected date range, and change the project in the dataset to whatever is given as input.
I built the dataset, and begun working on code to locate the correct lines. I figured first making some code to set everybody's 24th of December to "Christmas" would be easiest.
To make things easier on myself, I figured coloring it red would be best.
Sub AddHoliday()
Dim i As Long
For i = 1 To Sheets("2019-2020").Rows.Count
Next i
If Sheets("2019-2020").Cells(i, 1).Value = "24-12-2019" Then
Sheets("2019-2020").Cells(i, 1).Font.Color = vbRed
End If
End Sub
I expected to see every date equal to 24th of December to turn red, but I get the following error:
error 1004: Object-defined error.
On the first line of the IF-statement.
Problem:
If should have been inside For Loop
Date should be entered as Number, to be sure 24-12-2019 is recognized as 43823 by Excel
Loop was being run on all the Rows, it should have been on Active Rows in the sheet
Use:
Sub AddHoliday()
Dim i As Long
With Sheets("2019-2020")
For i = 1 To .Cells(.Rows.Count, "A").End(xlUp).row
If .Cells(i, 1).Value = 43823 Then ' 24-12-2019
.Cells(i, 1).Font.Color = vbRed
End If
Next i
End With
End Sub

How to Add New Rows and Fill with "0" in VBA?

I'm trying to help a colleague out with their spreadsheets (on MAC Excel V16.16.8), since I have some experience with coding, mostly in SQL, only very very basic level of VBA.
They receive daily data (which is why VBA is needed) which I have managed to split into separate sheets for them using basic macros.
The name of the sheet is "Birmingham" in this example.
Column B "Interval" are the hours of the day (24 hour clock). They only receive any data for the hours of the day where data actually exists in other columns. However, for their reports, they need to add/insert new rows even where there isn't any data from 0-23 (midnight-11pm).
The "Interval" column needs the correct hour/number in this descending order as seen in the example, with the Date and Campaign columns just being the same throughout. And have the rest of the cells for Total_Calls, Closed, etc, containing "0"s.
How do I add the new rows, "Intervals", and the "0"s?
I have tried a couple of different ways mostly around attempting to merge a mostly blank separate table only containing all of the "Intervals" 0-23. However, I have failed miserably in each method.
I am almost 100% sure there is a relatively simple method of doing this, but I lack specific VBA knowledge.
Any help would be most appreciated.
Thanks
You can get the current date and current campaign and insert missing rows like this:
Private Sub FillAllHours()
Dim i As Long
Dim myDate As Date ' value from date column
Dim myCampain As String ' value from campaign column
With ActiveSheet
myDate = .Cells(.Rows.Count, 1).End(xlUp).Value
myCampain = .Cells(.Rows.Count, 3).End(xlUp).Value
For i = 2 To 25
If .Cells(i, "B") <> i - 2 Then ' if row is missing
.Rows(i).Insert ' insert row above
.Cells(i, "B") = i - 2 ' insert hour number
.Cells(i, "A") = myDate ' insert date
.Cells(i, "C") = mycampaign ' insert campaign
.Cells(i, "D").Resize(1, 9).Value = 0 ' fill 9 cells with 0
End If
Next i
End With
End Sub

Delete within a Range, without deleting on Sheet

I apologize for the poor question name, if anybody has better suggestions at the end of this, let me know.
I have a range, say A1:M4 with the months in row 1 and a total for each row in column M. For each cell, there are values except for the months that have not yet happened. So if in November, everything up to November is filled in and December, column L, is empty and column M has the total from Jan to Nov (col A to col L). Another example, if in May, Jan, Feb, March, April and May and filled in, but not the other months, and column M has the total.
I want to create code that copies all the values up to the current month including the total for each row and paste into a powerpoint (I know how to copy and paste into a ppt). How do I copy this?
Current code:
rng=Range("a1",Range("a1").End(xldown).End(xlright))
Copies the entire range. How do I delete the columns without values in it. I thought of creating a count to count the values from column A to L, but having trouble incorporating how to remove the difference between total columns and filled in columns
This procedure can help you to find empty columns.
Sub FindBlankColumns()
'Step1: Declare your variables.
Dim MyRange As Range
Dim iCounter As Long
'Step 2: Define the target Range. Change to fit your needs
Set MyRange = ActiveSheet.UsedRange
'Step 3: Start reverse looping through the range.
'You can modify this according to your needs.
For iCounter = MyRange.Columns.Count To 1 Step -1
'Step 4: If entire column is empty then do some stuff.
If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
' Insert your code here to do what you want with empty column.
End If
Next
End Sub

Hiding columns if cell in column equeals Sat or Sun

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

Resources