Copy value to multiple worksheets - excel

I have one column of values in the master sheet to copy to multiple sheets in a specific cell. Each cell value is copied to each sheet once and is repeated over 625 times. I have one column of values (A2 to A626) in the master sheet to copy to multiple sheets in a specific cell. A2 is copied to sheet 2, cell R4. This is repeated until A626 is copied to sheet 626, cell R4. The sheet containing the A2 to A626 data is "unique"
I have lifted this code from another site and it did not work to my expectation.
Sub copyPasteData()
Dim strSourceSheet As String
Dim strDestinationSheet As String
Dim lastRow As Long
strSourceSheet = "Unique"
Sheets(strSourceSheet).Visible = True
Sheets(strSourceSheet).Select
Range("A2").Select
Do While ActiveCell.Value <> ""
strDestinationSheet = ActiveCell.Value
Selection.Copy
Sheets(strDestinationSheet).Visible = True
Sheets(strDestinationSheet).Select
lastRow = LastRowInOneColumn("R")
Cells(lastRow + 1, 0).Select
Selection.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Sheets(strSourceSheet).Select
ActiveCell.Offset(0, 2).Select
ActiveCell.Offset(1, 0).Select
Loop

You provided very little details, so I will give you some general tips.
To access (set or get) the value of specific cell from specific sheet you can use following syntax:
Worksheets("sheetName").Cells(row, col).Value (row and col are integers, specifying row and column of a cell), so in order to paste the value to cell B3 from sheet "Sheet2" from cell C4 from sheet "Sheet1" you should write:
Worksheets("Sheet2").Cells(3, 2).Value = Worksheets("Sheet1").Cells(4, 3).Value.
Moreover, you can access sheet using integers (1 - first sheet, etc.) instead of names, this way you can use loops to go through every sheet:
Worksheets(i).Cells(row, col).Value, where i, row, col are integers.
After consulting with OP, the code should look like:
Sub CopyToMultipleSheets()
Dim i As Long
For i = 2 To 626
'cell A2 will be referenced to as Cells(2, 1).Value
'generally Ai cell is Cells(i, 1)
'R4 cell is Cells(4, 18)
Worksheets(i).Cells(4, 18).Value = Worksheets("unique").Cells(i, 1).Value
Next i
End Sub

Related

Excel Copy Cell Data from one sheet to another using VBA

I am new to VBA, I have to copy cell value from one sheet to another. The existing code was
'go to the team sheet and select col 3-5 on last row and copy
Sheets(strname).Activate
ActiveCell.Offset(0, -10).Select
Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.Column + 2)).Select
Selection.Copy
DoEvents
'select the col 2 on team line and paste
Sheets("dtl overview").Select
ActiveCell.Offset(0, -6).Select
ActiveSheet.paste Link:=True
DoEvents
The problem is , I have added one more column in the 'team' sheet. So the above copy script has to read one cell backward.
Say for example, the above code is reading the data from D,E & F cells. I dont know how...
I am looking for to change the above code to read the value from C,D&E.
Inputs are Welcome & Highly appreciable!
I don't know how you consistently copy from columns D:F using that code either.
What your code does is:
'Activate sheet indicated in the "strname" variable.
'"strName" must be set elsewhere in the code?
Sheets(strname).Activate
'When the sheet is activated a cell will already be selected on there.
'This will be whatever cell was active when the sheet was previously looked at.
'This could easily change if a user selects another cell.
'The "OFFSET" command looks at the same row and ten columns to the left of the ActiveCell.
'If the ActiveCell is not in at least column J (11th column) then this
'will throw an "Application defined or Object Defined error" as it will try and select
'a column before column A.
'The offset cell is then selected - hopefully it will be column D.
ActiveCell.Offset(0, -10).Select
'This will select a range from the ActiveCell plus 2 columns on the same row.
'Hopefully columns D:F
Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.Column + 2)).Select
'Copy the selection.
Selection.Copy
'Don't need this line unless other code you haven't included needs it.
DoEvents
'Select the "dtl overview" sheet.
Sheets("dtl overview").Select
'Again, whichever cell was last active on "dtl overview" and select the cell 6 columns to the left.
ActiveCell.Offset(0, -6).Select
'Paste a link to the original cells.
'So if you copied D4:F4 on the original sheet (which I'll call "Sheet1") then this will paste
'=Sheet1!D4 , =Sheet1!E4 and =Sheet1!F4
ActiveSheet.Paste Link:=True
'Definitely shouldn't need this now.
DoEvents
At the moment your code looks 10 columns to the left of whichever cell is currently active - so depends which cell you have selected when you run the code.
You don't say which row you want copying, so this code copies row 1 and pastes to cell D1.
Sub Test()
Dim strName As String
strName = "Sheet1"
'ThisWorkbook means the file containing this code.
Dim wrkSht As Worksheet
Set wrkSht = ThisWorkbook.Worksheets(strName)
'Cells(1,4) is row 1, column 4.
'Range(Cells, Cells) shows a start & end cell for the range.
With wrkSht
.Range(.Cells(1, 4), .Cells(1, 6)).Copy _
Destination:=ThisWorkbook.Worksheets("dtl overview").Cells(1, 4)
End With
End Sub
Further reading: With

Match Columns and if match return specific value

How can I create a VBA code that will match 2 reference numbers in different columns and return data in third column.
The reference numbers are located in column A (Sheet1) and column A (Sheet2)
If a match is found, then the Dept. in Sheet 1, Column C, will be copied into an empty column in Sheet 2 Column B.
The code that I have written so far matches data in both columns but only for specific words.
Private Sub CommandButton1_Click()
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Cells(i, 3).Value = "North" Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("sheet2").Activate
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select
End Sub
You don't need VBA for this: Juse use a VLOOKUP
See formula used in Column H

Dynamically fill the next empty row with data from another sheet

In advance, I would like to thank anyone who reads this for taking the time to make any suggestions! I have tried other examples I've found on here and none of them seem to work so thanks for any advice!
So essentially I have 3 sheets. In sheet 1, I will be manually entering data into the next empty row (The data spans from Column A to Column U). Sheet 2 is linked to Sheet 1 in a manner to where if I select a row and autofill down to the next one, it will display the data from Sheet 1 (and also increases the values in each cell to account for inflation).
So essentially after I enter data into a new row on Sheet 1, I want to run a macro that will then dynamically autofill the last row on Sheet 2 to the next empty row. I also want this to be repeated going from Sheet 2 to Sheet 3.
An example would be, if Sheet 1 and 2 both have data down to row 35, I want to be able to manually enter data in row 36 and then my macro will autofill row 35 to 36 on Sheet 2.
The code I have written so far is below. To explain, base/basee and home/homee are cells I have named to compare values from specific columns for my if/then statement. I keep getting Error 1004 on the last line where I try and autofill down to the next cell wit Offset(1,0)
Sub PracticeTool()
Dim current1 As Integer
Dim current2 As Integer
Worksheets("City1").Select
Application.Goto Reference:="base"
Selection.End(xlDown).Select
Selection.End(xlDown).Select
current1 = Selection
Worksheets("Inflation").Select
Application.Goto Reference:="basee"
Selection.End(xlDown).Select
Selection.End(xlDown).Select
current2 = Selection
If (current1 <> current2) Then
Application.Goto Reference:="homee"
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFill Destination:=Selection.Offset(1, 0), Type:=xlFillDefault
End If
End Sub
Sheet 1 Sample Data: https://i.stack.imgur.com/pTFo5.png
Sheet 2 Sample Data: https://i.stack.imgur.com/kufrV.png
I didnt't get exactly what you wanted to compare, but I think you're close.
This code should solve the requirement.
Read the comments and adjust it to fit your needs.
Public Sub AutoFillSheets()
AutoFillRange "Sheet2", "A", "U"
AutoFillRange "Sheet3", "A", "U"
End Sub
Private Sub AutoFillRange(ByVal targetSheetName As String, ByVal fromColumnLetter As String, toColumnLetter As String)
Dim targetSheet As Worksheet
Dim targetRange As Range
Dim targetLastRow As Long
Set targetSheet = ThisWorkbook.Worksheets(targetSheetName)
' Get the last row in source sheet
targetLastRow = targetSheet.Cells(targetSheet.Rows.Count, 1).End(xlUp).Row
' Set the range to copy
Set targetRange = targetSheet.Range(fromColumnLetter & targetLastRow & ":" & toColumnLetter & targetLastRow)
' You had the error in this line (below). Problem is that to use autofill you need to include the rows from which Excel would calculate the source range (see that I took the last row in the first column, and added one to the second column)
targetRange.AutoFill Destination:=targetSheet.Range(fromColumnLetter & targetLastRow & ":" & toColumnLetter & targetLastRow + 1)
End Sub

Provide sum totals from within column, move to the next title name on same row column, copy paste

Very new at complex Macro's, my question is the following. I have column titles within excel sheet1, I am trying to copy all (Total SUM) data into "Sheet2"; several of the columns have merged columns, I am looking to have my macro read the last row title on column C38 and match the copy the total SUM on F38.
Example: Macro is suppose to read the title column, Row C38 with title "Total Assets" on column C and then search for the provided total on column F38, however, columns F, G, H are currently merged, F20 across F38,G38,H38.
After move down the empty Row, search for the next different title name, such like, "Total Funds" and repeat the process. I tried with 2 different scripts. Listed below I have provided my macro scripts. Perhaps, there is a better way at gathering all data per column.
Macro script:
Sub Macro3()
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=VLOOKUP(R[18]C[-3],R[3]C[-3]:R[18]C[8],4,FALSE)"
Selection.Copy
Sheets("sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub
If they stay the same you don't need VBA to do it, simply put =Sheet1!F38, =Sheet1!F45 and =Sheet1!F90 in the corresponding cells on Sheet2.
If they are variable you can loop over the cells of Sheet1 searching for your titels like this (if I understood it correctly):
Sub CopyFromSheet1()
Dim i as Long
For i = 1 to Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F
Select Case CStr(Sheet1.Cells(i, 3).Value) ' Looks at the Value in Column C
Case "Total Amount"
Sheet2.Cells(1, 1).Value = Sheet1.Cells(i, 6).Value
Case "Other Assets"
Sheet2.Cells(2, 1).Value = Sheet1.Cells(i, 6).Value
Case "Total Other Assets"
Sheet2.Cells(3, 1).Value = Sheet1.Cells(i, 6).Value
End Select
Next i
End Sub
This loop is looking at the values in column C, if it matches "Total Amount", "Other Assets" or "Total Other Assets" in copies the values of that row from column F of Sheet1 to Sheet2 into cells "A1", "A2" and "A3" respectively. In this code Sheet1 and Sheet2 refer to the CodeName of the sheets.

Find Match, Copy Row from Sheet1 and Insert Into Sheet2

In Sheet1, I have around 10,000 rows representing different people. Each person has a unique ID located in column D, which is a number sequence stored as text.
In Sheet2, I have around 1,200 person entries that have have a reference to a matching person in Sheet1 located in column A. This reference is the same unique ID used in Sheet1.
What I would like is to have a macro do is this:
read-in the value of cell A1 on Sheet2
find the matching value in column D on Sheet1
copy the matching row in Sheet1
insert the matching row underneath on Sheet2 (row 2)
insert a blank row (row 3)
repeat the steps for the remaining 9,999 entries on Sheet2 so that the matching data always falls underneath the read-in value, followed by a blank row
Any help would be appreciated.
May I advise that in future you show evidence of trying to solve the problem you are having. That way we know you are participating in the community and not attempting to extract free labour from it.
Here is a solution you can try. It starts from the currently selected cell in sheet2.
Function DoOne(RowIndex As Integer) As Boolean
Dim Key
Dim Target
Dim Success
Success = False
If Not IsEmpty(Cells(RowIndex, 1).Value) Then
Key = Cells(RowIndex, 1).Value
Sheets("Sheet1").Select
Set Target = Columns(4).Find(Key, LookIn:=xlValues)
If Not Target Is Nothing Then
Rows(Target.row).Select
Selection.Copy
Sheets("Sheet2").Select
Rows(RowIndex + 1).Select
Selection.Insert Shift:=xlDown
Rows(RowIndex + 2).Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(RowIndex + 3, 1).Select
Success = True
End If
End If
DoOne = Success
End Function
Sub TheMacro()
Dim RowIndex As Integer
Sheets("Sheet2").Select
RowIndex = Cells.row
While DoOne(RowIndex)
RowIndex = RowIndex + 3
Wend
End Sub

Resources