Create Sheets from column values, and insert values in column in specific cells on each sheet - excel

I found a macro that reads values in Column A on "Sheets Insert", creates individual worksheets based on those values, and then copies "Template" to each new page.
Sub NewSheets()
Dim i As Integer
Dim ws As Worksheet
Dim sh As Worksheet
Set ws = Sheets("Template")
Set sh = Sheets("Sheets Insert")
Application.ScreenUpdating = 0
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
Sheets("Template").Copy Before:=sh
ActiveSheet.Name = sh.Range("A" & i).Value
Next i
End Sub
It works great.
So the next step for me is to take the value the worksheet was created from in Column A of "Sheets Insert", and insert that value at G3 of the created worksheet.
Then I need it to take the value in the same row in column B of "Sheets Insert" and copy it into C3 on that page.
So for example:
"Sheets insert"
Column A | Column B
Motor A 12345
Motor B 23456
Code creates sheet Motor A and pastes Motor A to [g3] and pastes 12345 to [c3] on Motor A sheet.
Code creates sheet Motor B and pastes Motor B to [g3] and pastes 23456 to [c3] on Motor B sheet.
And so on down the list. I searched for a couple of hours and had no luck. Hoping someone can help. Thanks.

If i understood your question you have to add two rows of the code:
Sub NewSheets()
Dim i As Integer
Dim ws As Worksheet
Dim sh As Worksheet
Set ws = Sheets("Template")
Set sh = Sheets("Sheets Insert")
Application.ScreenUpdating = 0
For i = 2 To Range("A" & rows.count).End(xlUp).Row
Sheets("Template").Copy Before:=sh
ActiveSheet.Name = sh.Range("A" & i).Value
'add code
Range("G3") = sh.Range("A" & i) 'copy name into cell G3
Range("C3") = sh.Range("B" & i) ' copy data into cell C3
Next i
End Sub
Hope this helps

Related

Dump data to specific Cell in a different Sheet in the same Workbook

I was looking for a VBA script to write data from a table to specified cell in a worksheet.
For eg: In the image table - column 1 is serial number, column 2 is data to be written, column 3 is sheet to which it should be written and column 4 is the cell in the corresponding sheet.
I am looking for a VBA script to write '1' from row 1 & column 2 to cell "A1" in sheet "A".
Here's code for it:
Sub Dump2Print()
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Dim rng As Range: Set rng = Application.Range("Dump!A1:A" & LastRow)
Dim i As Integer
Set wb = ActiveWorkbook
For i = 1 To rng.Rows.Count
MyValue = rng.Cells(RowIndex:=i, ColumnIndex:="B").Value
MySheet = rng.Cells(RowIndex:=i, ColumnIndex:="C").Value
MyRange = rng.Cells(RowIndex:=i, ColumnIndex:="D").Value
Set ws = wb.Sheets(MySheet)
ws.Activate
ws.Range(MyRange) = MyValue
Next
End Sub
This code will look at all the data in the table in sheet named "Dump" and read values from column "B","C" & "D".

Trying to copy cell format from one sheet to another, cell by cell

Sub Start()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheets("1")
Set ws2 = Sheets("2")
Dim LastRow As Long, i As Long
LastRow = Cells(Rows.Count, "D").End(xlUp).row
For i = 1 To LastRow
ws1.Range("D" & i).copy
ws2.Range("A2").PasteSpecial Paste:=xlPasteFormats
Next i
If ws1.Range("A" & i) = vbNullString Then
Exit Sub
End If
End Sub
I'm trying to make it so this code pastes the interior fill from column D, Into Column A, unless there is already interior fill present.
Any input appreciated.
Edit: I'm trying to copy the format (interior fill) from Sheet 1 and Sheet 2's Column A's (until the last row) and paste it to Sheet Column A. I'm trying to copy over once cell at a time, and ignore if there is already fill present in sheet 3, because my I dont want the format from sheets 1 and 2 to overwrite eachtoher.

Replacing formulas in multiple sheets with the different values based on a different sheet VBA

My current code creates 9 copies of a sheet called "MasterCalculator". It decides the amount of copies to be named by counting the number of cells filled in Row 1 (starting at column C) in the other sheet Called 'LLP Disc Sheet'. Each of the 9 sheets created are then named. Sheet 1's name comes from C1 in the 'LLP Disc Sheet', Sheet 2's name comes from D1 in the 'LLP Disc Sheet', Sheet 3's names comes from E1 in the 'LLP Disc Sheet', and so on.
Option Explicit
Public Sub NewSheets()
Dim shCol As Integer
Dim i As Integer
Dim ws As Worksheet
Dim sh As Worksheet
Set ws = Sheets("MasterCalculator")
Set sh = Sheets("LLP Disc Sheet")
Application.ScreenUpdating = 0
Application.EnableEvents = 0
shCol = 2
sh.Activate
For i = 2 To sh.Range("A1:Z1").Cells.SpecialCells(xlCellTypeConstants).Count
shCol = shCol + 1
Application.StatusBar = "Processing " & sh.Cells(1, shCol).Text & Format(i / sh.Range("A1:Z1").Cells.SpecialCells(xlCellTypeConstants).Count, " #0.0 %")
Select Case shCol
Case Is = 3
ws.Copy After:=sh
Case Else
ws.Copy After:=Sheets(sh.Cells(1, shCol - 1).Text)
End Select
ActiveSheet.Name = sh.Cells(1, shCol).Text
Application.CutCopyMode = False
Next i
sh.Activate
Application.StatusBar = 0
Application.EnableEvents = 1
Application.ScreenUpdating = 1
Application.CalculateFull
End Sub
So now that all the sheets are created and named... I now want to update the formulas in each since they're copies of the sheet called 'MasterCalculator'. There are 2 cells in each sheet I need to update - cell B1 and cell M4. Cell B1 contains the formula "=+'LLP Disc Sheet'!C1". The sheet that was created based on C1 in the 'LLP Disc Sheet' can keep this formula. However, the next sheet (sheet 2) that was created and named based off of D1 in the "LLP Disc Sheet" needs to be updated to "=+'LLP Disc Sheet'!D1". This goes on with the rest of the sheets. The next has to change to =+'LLP Disc Sheet'!E1 and so on. How do I create a code to replace that cell in each of the newly created sheet with an updated formula that only changes it to cell referenced one cell after in the 'LLP Disc Sheet'?
ActiveSheet.Range(“B1:M4”).Replace_
What: ="LLP Disc Sheet'!C1", Replacement:="LLP Disc Sheet'!D1”,_ ‘but I want it to continue to the next sheet to replace D1 with E1 and so on until all of the B1 cells match their sheet names (it also allow all the data to be filled in). All of these will be found in cell B1 in the MasterCalculator copies
What: ="LLP Disc Sheet'!$C$1:$C$", Replacement:=" LLP Disc Sheet'!$D$1:$D$”,_ ‘but I want it to continue to the next sheet to replace $D$1 with E$1$ and $D$ with $E$ and so on until all of the M4 cells are set to 0.
SearchOrder:=xlByRows, MatchCase:=True
Use formulaR1C1
Option Explicit
Public Sub NewSheets()
Dim wb As Workbook, ws As Worksheet, wsMaster As Worksheet
Dim iLastCol As Integer, iCol As Integer
Dim s As String, n As Integer
Set wb = ThisWorkbook
Set wsMaster = wb.Sheets("MasterCalculator")
Set ws = wb.Sheets("LLP Disc Sheet")
iLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
n = wb.Sheets.Count
For iCol = 3 To iLastCol
s = ws.Cells(1, iCol) ' sheet name
If Len(s) > 0 Then
wsMaster.Copy After:=Sheets(n)
n = n + 1
wb.Sheets(n).Name = s
wb.Sheets(n).Range("B1,M4").FormulaR1C1 = "='" & ws.Name & "'!R1C" & iCol
End If
Next
MsgBox iLastCol - 2 & " sheets added", vbInformation
End Sub

Finding & filling in other Excel sheets based on criteria using VBA

I have a workbook with four worksheets: Overview, apple, banana and pear.
In the sheet overview I have a 3x3 table:
In Out Extra
apple
banana
pear
Cell H5 in Overview contains a date of 2019, which can be selected via a drop-down menu
In each of the apple/banana/pear sheets, I have a 365x3 table:
In Out Extra
1-1-2019
2-1-2019
3-1-2019
.
.
.
31-12-2019
I would like to run a macro so that the In, Out and Extra values from the Overview sheet are filled in the correct worksheet and behind the correct date in that worksheet.
The goal would be that people fill in the overview sheet (In, Out and Extra values as well as a date), they run the macro, and data is automatically stored in the right cell in the right worksheet.
This is a relatively easy example, the actual workbook for which I need this macro has more that 70 "fruits".
I know the code below doesn't work, but I'll hope to show my way of thinking
Sub export()
Dim ws As Worksheet 'worksheet
Dim currentdate As Date 'datum
Dim fruit As String 'Fruit
Worksheets("Overview").Activate 'activate worksheet Overview
currentdate = ActiveSheet.Cells(H5) 'select date value
fruit = Overview.Range(“C6, C8”) 'select range of the fruits
For Each ws In Worksheets 'loop over every worksheet except the Overview sheet
If ws.Name = fruit Then 'crossreference name worksheet with fruit in Overview sheet
ws.Activate 'activating the selected worksheet
If ws.Range("A1:A365") = currentdate Then 'looking for the correct date in the selcted worksheet
fruit = ws.Name
Next ws
End Sub
Vba solution for this:
For this solution to work properly, you should make the sheets APPLE, BANANA and PEAR share same structure. In my example, all this 3 sheets have in column A the date, column B is IN, column C is OUT and column D is EXTRA
Also, in OVERVIEW sheet, make sure the terms APPLE, BANANA and PEAR are exactly equal to names of each sheet (this means no extra spaces, blanks or different chars).
And OVERVIEW must be the active sheet.
My button IMPORT is linked to this code to import data. I want to import data from 17/05/2019 (the yellow rows)
Sub IMPORT_DATA()
Application.ScreenUpdating = False
Range("B2:D4").Clear
Dim i As Long
Dim TargetRow As Long
Dim TargetSheet As String
Dim TargetDate As Date
TargetDate = Range("B6").Value
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row Step 1 'i=2 because dats in OVERVIEW stars at row 2, and Column A
TargetSheet = Range("A" & i).Value
'first, we make sure the date from B6 exists in the target worksheet counting
With Application.WorksheetFunction
If .CountIf(ThisWorkbook.Worksheets(TargetSheet).Range("A:A"), TargetDate) > 0 Then
TargetRow = .Match(CDbl(TargetDate), ThisWorkbook.Worksheets(TargetSheet).Range("A:A"), 0)
Range("B" & i).Value = ThisWorkbook.Worksheets(TargetSheet).Range("B" & TargetRow).Value 'IN value
Range("C" & i).Value = ThisWorkbook.Worksheets(TargetSheet).Range("C" & TargetRow).Value 'IN value
Range("D" & i).Value = ThisWorkbook.Worksheets(TargetSheet).Range("D" & TargetRow).Value 'IN value
End If
End With
Next i
Application.ScreenUpdating = True
End sub
And after executing this code I get in OVERVIEW:
Now I want to export some values to data, and I use this code:
Sub EXPORT_DATA()
Application.ScreenUpdating = False
Dim i As Long
Dim TargetRow As Long
Dim TargetSheet As String
Dim TargetDate As Date
TargetDate = Range("B6").Value
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row Step 1 'i=2 because dats in OVERVIEW stars at row 2, and Column A
TargetSheet = Range("A" & i).Value
'first, we make sure the date from B6 exists in the target worksheet counting
With Application.WorksheetFunction
If .CountIf(ThisWorkbook.Worksheets(TargetSheet).Range("A:A"), TargetDate) > 0 Then
TargetRow = .Match(CDbl(TargetDate), ThisWorkbook.Worksheets(TargetSheet).Range("A:A"), 0)
ThisWorkbook.Worksheets(TargetSheet).Range("B" & TargetRow).Value = Range("B" & i).Value 'IN value
ThisWorkbook.Worksheets(TargetSheet).Range("C" & TargetRow).Value = Range("C" & i).Value 'OUT value
ThisWorkbook.Worksheets(TargetSheet).Range("D" & TargetRow).Value = Range("D" & i).Value 'EXTRA value
End If
End With
Next i
MsgBox "data exported"
Application.ScreenUpdating = True
End Sub
And after executing code, check new data (yellow rows):
Hope this helps a litte bit and you can adapt to your needs.

Vba comparing then copying two different Sheets

I realize there are a few different similar ideas on here. But I need help with this simple compare function.
My goal is to compare two different cells and if they are the same, replace it with its full non-abbreviated name.
Thank you for your time!!!
I.E
Sheet1 Sheet2
Column H Column A Column B
Dept Dept Department
This is what I have (Yes simple), but the cell H is not updating to the non-abbreviation:
Sub updateDeptNames()
'Format user ID from the email column
Dim ws As Worksheet, ws2 As Worksheet
Dim LastRow As Long, i As Long
Dim tmpArray() As String, tempDept As String
Set ws = ActiveWorkbook.Sheets("Student_Travel_DB") '--> This is the relevant sheet
Set ws2 = ActiveWorkbook.Sheets("gokoutd") '--> This is the relevant sheet
LastRow = 1000 ''Bug finding the last row, had to hard code it
For i = 2 To LastRow 'Iterate through all the rows in the sheet
For j = 2 To 112
tempDept = ws2.Range("A" & j).Value
If ws.Range("H" & i).Value = tempDept Then
ws.Range("H" & i) = ws2.Range("B" & j).Value
End If
Next j
Next i
End Sub
You can more easily use VLOOKUP either on your worksheet or with VBA:
Sub GetFullName()
Dim cl As Range, data As Range, lookUpRng As Range
Set data = Worksheets("Student_Travel_DB").Range("A1:A10")
Set lookUpRng = Worksheets("gokoutd").Range("A1:B10")
On Error Resume Next
For Each cl In data
cl = WorksheetFunction.VLookup(cl, lookUpRng, 2, False)
Next cl
End Sub
You'll need to change your range references.

Resources