Fill down sheet name in a column in multiple sheets - excel

I have code to insert the sheet name into a field of each sheet (there are 80+ sheets).
I would like to insert the sheet name to a cell and fill down to the last row of each sheet.
I get errors:
Sub nameSheet()
For Each x In Worksheets
x.Range("F2" & LastRow) = x.Name
Next x
End Sub

As #BigBen said, you need to define LastRow
there is a special cell property called xlCellTypeLastCell. This is handy to use, as your last cell can be anywhere. Additionally I would do Lastrow + 1, that way you don't overwrite anything.
Sub nameSheet()
Dim x As Worksheet
For Each x In Worksheets
lastrow = x.Cells.SpecialCells(xlCellTypeLastCell).Row
x.Range("F" & lastrow + 1) = x.Name
Next x
End Sub

Related

Copy a cell to the cell next to the result of a lookup vba

I have a sheet called "containers"
Where I have the following data like this :
On my other sheet "planner" I have a full list of around 900 containers with lots of data.
What i am trying to do is to do a lookup of Column B from my sheet "containers"
And copy the responding value of column A into the field "agreed_unload_hour" of the looked up value.
So basically in this case I would like that for container1 the value 5u00 from sheet "containers" gets pasted to sheet "planner" in the agreed_unload_hour of sheet "planner".
And this for all the containers on the sheet "containers" (a fixed range).
I know I could do this with a formula index/match, but i cant put my formula on sheet "planner" because that is populated with data, and I cant overwrite this data with my formula.
I need it in VBA as it is part of a bigger vba project.
Thank you so much for ending my struggle, as i find a lot of similar things in here, but they always seem to work the other way round, copying data from my sheet "planner" to sheet "containers" like a normal vlookup would do.
Assuming you have only one table on the planner sheet.
Option Explicit
Sub Update()
Dim arID, arName, i
Dim lastrow As Long, n As Long
Dim tbl As ListObject, r As ListRow
Dim a As Long, b As Long
With ThisWorkbook.Sheets("Containers")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
arID = .Range("A2:A" & lastrow).Value2
arName = .Range("B2:B" & lastrow).Value2
End With
With ThisWorkbook.Sheets("Planner")
Set tbl = .ListObjects(1) ' or table name "Containers"
a = tbl.ListColumns("AGREED_UNLOAD_HOUR").Index
b = tbl.ListColumns("CONTAINER").Index
For Each r In tbl.ListRows
i = Application.Match(r.Range(, b), arName, 0)
If Not IsError(i) Then
r.Range(, a) = arID(i, 1)
n = n + 1
Else
r.Range(, a) = "tba" ' or ""
End If
Next
End With
MsgBox n & " rows updated", vbInformation
End Sub

VBA Excel - Remove a row from a multiple sheet if it is equal to

I am currently working on deleting rows. I have already made it work in one sheet, but I just want to ask if there is any way to delete rows in several sheets at the same time? I have a unique key which is the student ID that is in Column C of all the sheets that will be affected. So, by clicking on the delete button, all data with this student ID will be deleted.
Using the code below, I can delete a row from the STUDENTS_INFO sheet.
Sub del_stud()
Set ws = ActiveWorkbook.Worksheets("STUDENTS_INFO")
LastRow = ws.Cells(Rows.Count, "C").End(xlUp).Row
For r = 10 To LastRow
If CStr(ThisWorkbook.Sheets("HOME").Range("K11").Value) = ws.Cells(r, 3) Then
ws.Rows(r).EntireRow.Delete
MsgBox "Student's data is now deleted!"
Unload Me
End If
Next r
End Sub
The sheets that will be affected are STUDENTS_INFO, G1-Q1, G1-Q2, G1-Q3, G1-Q4, G2-Q1, G2-Q2, G3-Q3, G4-Q4, and so on... I also have sheets that, hopefully, will not be touched. Is this possible?
Based on my research, it uses the For Each ws In ThisWorkbook.Sheets. I tried to use it, but it still deletes the row in STUDENTS_INFO sheet and not on multiple sheets.
Here's the code that I tried.
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Sheets
LastRow = ws.Cells(Rows.Count, "C").End(xlUp).Row
For r = 10 To LastRow
If CStr(ThisWorkbook.Sheets("HOME").Range("K11").Value) = ws.Cells(r, 3) Then
ws.Rows(r).EntireRow.Delete
MsgBox "Student's data is now deleted!"
Unload Me
End If
Next r
Next ws
Application.ScreenUpdating = True
I’d agree with #urdearboy’s suggestion of using a filter to delete the rows – plus looping through an array of sheets that you designate. The following code assumes the Student ID is sourced from the cell K11 on the HOME sheet. You can add/remove sheets from the array as you see fit.
Try the following & let me know how you go.
Option Explicit
Sub del_stud()
Dim StudID As String, ws As Worksheet
'Get the filter criteria from cell K11 in the HOME sheet
StudID = ThisWorkbook.Sheets("HOME").Range("K11").Value
'Do the STUDENTS_INFO sheet by itself
With ThisWorkbook.Sheets("STUDENTS_INFO").Cells(8, 3).CurrentRegion
.AutoFilter 1, StudID
.Offset(1).EntireRow.Delete
.AutoFilter
End With
'Do the other generic sheets next - add/remove sheets as required
For Each ws In Sheets(Array("G1-Q1", "G1-Q2"))
With ws.Cells(9, 3).CurrentRegion
.AutoFilter 1, StudID
.Offset(1).EntireRow.Delete
ws.AutoFilterMode = False
End With
Next ws
End Sub

Use VBA to paste a formula if there is something in column A of that row

I'm trying to run some VBA that will count how many rows there are which are not empty in a given range, and then paste a formula in column 13 (M) the number of rows down which were not empty.
This is the code I have:
Sub CountCells()
MsgBox WorksheetFunction.CountA(Sheets("DATA").Range("A7:A750"))
Worksheets("DATA").Range("M7:M500").Formula = "=MYFORMULAR"
End Sub
This code currently counts the number of cells which are not empty in column A but then how do I take this number and use it for the next equation?
If there were 200 columns in range A7:A750 with content in, I would like to paste my formular from M7 to M207.
Option Explicit
Sub CountCells()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("DATA")
Dim LRow As Long
'Determine last row
LRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
'Apply formula from rows 7 to last row
ws.Range("M7:M" & LRow).Formula = "=MYFORULAR"
End Sub

Copy and paste based on cell value

I am attempting to loop through various workbooks and paste data into another "merge" workbook based on cell values.
Workbook A sheet contains the data I want to loop through and copy along with the adjacent two columns.
Workbook B is where I want to paste the data.
Workbook A - Column A contains numeric values. I want to copy the cells A2, B2, C2 of data only if column A2's value is between 1-299. Paste these copied cells in two workbook B- columns A, B, C starting below where the last values where pasted.
Then loop through the next row checking if its value is between 1-299.
I will then apply this to many spreadsheets, but for now I just want to get one copy and paste working so I can understand how to apply it.
How much knowledge of VBA do you have? You could try to change the following code that I've been using for something similar, although it has an input box.
Option Compare Text
Public Sub TeamReport()
Dim strFind As String
Dim i As Long, j As Long
Dim wsFind As Worksheet
Dim wsPaste As Worksheet
strFind = InputBox("Enter Team Name Here")
Set wsFind = Sheets("Complete Listing")
Set wsPaste = Sheets("Team Report")
j = 3
Worksheets("Team Report").Range("A3:DJ300").ClearContents
For i = 2 To wsFind.UsedRange.Rows.Count
If wsFind.Range("C" & i) = strFind Then
wsFind.Range(i & ":" & i).Copy Destination:=wsPaste.Range(j & ":" & j)
j = j + 1
End If
Next i
Worksheets("Team Report").Select
End Sub
It has a input box to find a specific value, but might be able to get you started.

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