I am trying to merge multiple sheets into one single sheet using Excel VBA.
I followed the codes in this website There is a function called Sub CopyDataWithoutHeaders()
It gets the first and last row in each sheet, and paste the values at the end of the merged sheet.
'Find the last row with data on the DestSh and sh
StartRow = 2
Last = LastRow(DestSh)
shLast = LastRow(sh)
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
It copies the cell values using the following code:
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
This code only copy the values. Since there are formulas in the cells, I changed this part to :
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteFormulasAndNumberFormats
Application.CutCopyMode = False
End With
As an alternative I tried this code as well:
CopyRng.Copy DestSh.Cells(Last + 1, "A")
Or the code in the similar question
All of these three solutions, copy the formulas with an extra text (which I assume it is a reference to the original sheets).
for example the formula in the original sheet is:
=(IF([#[Actual Date]]="",[#[Forecast
Date]],[#[Actual Date]]))+[#[Shipping
(days) ]]*2
Where "Forecast Date" "Actual Date", and "Shipping (days)" are column names (Table headers)
And after the merge, it will be:
=(IF(_PMTemplate333574[#[Actual Date]]="",_PMTemplate333574[#[Forecast
Date]],_PMTemplate333574[#[Actual Date]]))+_PMTemplate333574[#[Shipping
(days) ]]*2
For each sheet this reference number (_PMTemplate333574) is different, and for some sheets it returns an error.
I tried to manually delete this reference, but it's time consuming. I was wondering what is it? and how can it be removed?
Related
I have the following code that copies rows from another sheet to the current sheet at a given location.
ws2 and ws3 are worksheets.
ws3.Range(ws3.Cells(4, 1), ws3.Cells(4,2)).Copy ws2.Cells(2, 2)
How do I change this code to paste only the values of the cells? Current working code copies cells with formulas. I would like to know how to pass pasteSpecial parameters as below ?
ws3.Range(ws3.Cells(4, 1), ws3.Cells(4,2)).Copy ws2.Cells(2, 2).PasteSpecial = xlPasteValues
Break it into two lines and use a parenthesis rather than an = sign.
ws3.Range(ws3.Cells(4, 1), ws3.Cells(4,2)).Copy
ws2.Cells(2, 2).PasteSpecial (xlPasteValues)
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
Want to do:
A.If only one row is present in the data sheet, copy and paste that lone row and paste it to the named sheet
B.if there are multiple rows of data, copy all then paste
Issues Having with Current Code:
it disregards the first if condition and goes straight to the next one which copies the range and everything below even if theres only one row of data present.
here's my code with the following condtions:
ws2 = source data sheet
wsA = sheet data will be pasted on
copied data if conditions are met should be pasted on the last available blank row in column A of WsA
k = ws2.Range("a6", ws2.Range("a6").End(xlDown)).Rows.Count
If k <= 1 Then
ws2.Activate
rngB.Select
Selection.Copy
wb2.Activate
wsA.Activate
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Else
ws2.Activate
rngB.Select
Range(rngB, ActiveCell.End(xlDown)).Select
Selection.Copy
wb2.Activate
wsA.Activate
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
data sheet
If there is no data below row 6 then ws2.Range("a6").End(xlDown) will extend down to the bottom of the sheet (so k > ~1000000)
To detect if only one row of data exists, try
If IsEmpty(ws2.Range("a6").Offset(1,0) then
' Only one row
Else
' More than one row
End If
And, head the advise to avoid select.
In a VBA Excel macro,
I copy rows from another sheet, select a different sheet, and try to find the next open cell before pasting them.
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
The page has a first row of column titles, and find then passes over these to the very last row???
If dummy data
is put in the second row, or if there is data already present,
it works.
WHY is it doing this?
Range.End(xlDown) is equivalent to pressing Ctrl + ↓.
Test it on your own - if you only have a header in Row 1, then you'll jump to the last row in the Worksheet. If you have data below the header, you'll move to that row.
Use Range.End(xlUp) to find the next available row by moving up from the last row on the Worksheet - something like this:
With Sheet1
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
standing your data layout, you can use:
Cells(WorksheetFunction.CountA(Columns(1)) + 1, 1).PasteSpecial Paste:=xlPasteValues
or, using explicit worksheet references (recommended):
With Worksheets("mySheetName")
.Cells(WorksheetFunction.CountA(.Columns(1)) + 1, 1).PasteSpecial Paste:=xlPasteValues
End With
First thing I did was create a button that would copy certain cells using this code:
Worksheets("Sheet1").Range("A:A,B:B,D:D").Copy _
and it worked fine.
Second, I found the code that would copy all details in a row based on the criteria of one, in this case if there was an "A" in the "Location" column.
Private Sub ENTIREROW_Click()
'Sub copyrows()
Dim i As Range, Cell As Object
Set i = Range("D:D") 'Substitute with the range which includes your True/False values
For Each Cell In i
If IsEmpty(Cell) Then
Exit Sub
End If
If Cell.Value = "A" Then
Cell.ENTIREROW.Copy
Sheet2.Select 'Substitute with your sheet
ActiveSheet.Range("A65536").End(xlUp).Select
Selection.Offset(1, 0).Select
ActiveSheet.Paste
End If
Next
End Sub
My question is, how do I copy all information in the specified columns (A,B,D) where there is an "A" in "Location" in one button.
Furthermore, this is my example data, the sheet I will actually use this on has 34 columns to copy. Is there a more efficient way of setting a range when you don't want an entire sequence, everything but the data in column C?
Thanks in advance and apologies for my explanation skills.
One way maybe to:
filter your source
hide column C
copy the result using .PasteSpecial xlPasteValues into the destination
Unhide column C on the source sheet
remove the autofilter
Using xlPasteValues only pastes the visible cells from the source - so no column C
The code then looks like this: .
Sub CopyRows()
With Sheets(1).Range([A2], [A2].SpecialCells(xlLastCell))
[A1].AutoFilter
.AutoFilter Field:=4, Criteria1:="A"
[C:C].EntireColumn.Hidden = True
.Copy
[C:C].EntireColumn.Hidden = False
End With
With Sheets(2)
If .Cells(Sheets(2).Rows.Count, 1).End(xlUp) = "" Then 'it's a clean sheet
.Cells(Sheets(2).Rows.Count, 1).End(xlUp).PasteSpecial Paste:=xlPasteValues
Else
.Cells(Sheets(2).Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End If
End With
Application.CutCopyMode = False
Sheet1.[A1].AutoFilter
End Sub