I try to copy last entire row to another sheet but failed
with this method it"s only copying single cell to all the row
Dim lrow As Long
With Worksheets("101")
lrow = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("B" & lrow - 1, "M" & lrow).Copy
Worksheets("EOM").Range("B4").PasteSpecial xlPasteAll
End With
with this code it gives error
Dim shRead As Worksheet
Set shRead = ThisWorkbook.Worksheets("101")
Dim lastRow As Long, lastCol As Long
lastRow = shRead.Cells(shRead.Rows.Count, 2).End(xlUp).Row
lastCol = shRead.Cells(lastRow, shRead.Columns.Count).End(xlToLeft).Column
With shRead
shRead.Range(lastRow, lastCol).Copy_
Worksheets("EOM").Range(B4, M4)
End With
error on
shRead.Range(lastRow, lastCol).Copy_
In place of your code
With shRead
shRead.Range(lastRow, lastCol).Copy_
Worksheets("EOM").Range(B4, M4)
End With
You have to put start and end cell for a range
With shRead
.Range(.Cells(lastRow, 1), .Cells(lastRow, lastCol)).Copy Worksheets("EOM").Range("B4")
End With
for the destination the cell reference should be in double quotes
EDIT
You can use a code similar to the below one; to get data from all worksheets a loop is needed, you can amend the code according to your requirements
Dim i As Integer
i = 4
For Each ws In ActiveWorkbook.Worksheets
With ws
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
lastCol = .Cells(lastRow, .Columns.Count).End(xlToLeft).Column
.Range(.Cells(lastRow, 1), .Cells(lastRow, lastCol)).Copy Worksheets("EOM").Range("B" & i)
i = i + 1
End With
Next
Related
Novice in VBA so bear with me. I am trying to have an xlookup function working through a loop in VBA to populate a worksheet taking values from another worksheet.
So far I have this:
Sub prep_BG()
Dim sh As Worksheet, ws As Worksheet, sh1 As Worksheet
Dim lr As Long, lcol1 As Long, x As Long, lr1 As Long, lc As Long
Set sh = Sheets("BG_N")
Set sh1 = Sheets("BG_N_1")
Set ws = Sheets("BG_fin")
'Lookup and paste data from first sheet to target sheet
With sh
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
lcol1 = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column + 1
.Range("A1:A" & lr).Copy
ws.Cells(1, 1).PasteSpecial
End With
'Lookup and paste data from second sheet to target sheet
With sh1
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
lcol1 = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column + 1
.Range("A2:A" & lr).Copy
lr1 = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
ws.Range("A" & lr1 + 1).PasteSpecial
End With
'Work in the target sheet to remove duplicate, have proper formatting, sorting and populating it with past data using xlookup
With ws
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A1:A" & lr).RemoveDuplicates Columns:=1, Header:=xlYes
.Range("A1:A" & lr).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
.Cells(1, 2).Value = "BG n-1"
.Cells(1, 3).Value = "BG n"
lc = sh1.Cells(1, .Columns.Count).End(xlToLeft).Column + 1
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
For x = lr To 2 Step -1
.Cells(x, 2).Formula = "= XLookup(ws.Cells(x, 1), sh1[A:A], sh1.Columns(lc), 'NA', 0)"
Next x
End With
End Sub
The code works fine up to the xlookup function where it throws
"Run-time error '1004': Application-defined or object-defined error".
I think it is due to my referencing in the xlookup formula but I can't find a solution...
So far, all the comments I have read don't seem to apply to my problem.
If anyone has any clue on how to solve this or where to look it would be greatly appreciated.
Thank you in advance.
I'm trying to merge several sheets into one.
Configuration
DataSheet1 : First sheet
DataSheet2 : Second sheet
ConsolidatedSheet : Consolidated sheet
Code
Set consolidatedSheet = Worksheets("ConsolidatedSheet")
consolidatedSheet.Activate
startRow = 2
startCol = 1
Worksheets("DataSheet1").Activate
lastRow = Cells(Rows.Count, startCol).End(xlUp).row
lastCol = Cells(startRow, Columns.Count).End(xlToLeft).Column
Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy _
consolidatedSheet.Range("A" & consolidatedSheet.Cells(Rows.Count, 1).End(xlUp).row + 1)
Worksheets("DataSheet2").Activate
lastRow = Cells(Rows.Count, startCol).End(xlUp).row
lastCol = Cells(startRow, Columns.Count).End(xlToLeft).Column
Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy _
consolidatedSheet.Range("A" & consolidatedSheet.Cells(Rows.Count, 1).End(xlUp).row + 1)
Issue
Two arrays are created in the consolidated sheet. It means I can't sort on the consolidated sheet.
How do I copy data as values instead of arrays?
Sub consSheets()
Dim ws As Worksheet
With Worksheets("ConsolidatedSheet")
.Cells.Delete ' clear the assignment sheet first
For Each ws In Sheets(Array("DataSheet1", "DataSheet2"))
ws.Cells(2, 1).CurrentArray.Copy
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
Next ws
End With
Application.CutCopyMode = False ' "clears" the clipboard
End Sub
Edit2: (not copy headers from DataSheet1 and DataSheet2 and keep existing header in ConsolidatedSheet)
Sub consSheets()
Dim ws As Worksheet
With Worksheets("ConsolidatedSheet")
.Rows("2:" & .UsedRange.Row + .UsedRange.Rows.Count).Delete ' clear (without header in Row 1) the assignment sheet first
For Each ws In Sheets(Array("DataSheet1", "DataSheet2"))
Set Rng = ws.Cells(2, 1).CurrentRegion
Set Rng = Intersect(Rng, Rng.Offset(1)) ' eliminate headers
If Not Rng Is Nothing Then
Rng.Copy
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End If
Next ws
End With
Application.CutCopyMode = False ' "clears" the clipboard
End Sub
I'm not sure what you mean by it creating arrays, and I don't think that code is actually the code using as it's not doing what you describe.
But here's something that does what your intending.
Option Explicit
Sub Test()
Dim cSht As Worksheet
Set cSht = Worksheets("ConsolidatedSheet")
Dim StartRow As Integer, StartCol As Integer
StartRow = 1
StartCol = 1
'Split out to a sub and don't need to repeat self
Call ConsolidateData(cSht, "DataSheet1", StartRow, StartCol, True)
Call ConsolidateData(cSht, "DataSheet2", StartRow, StartCol)
End Sub
Private Sub ConsolidateData(cSht As Worksheet, FromSheet As String, StartRow As Integer, StartCol As Integer, Optional IncludeHeader As Boolean)
Dim FromRow As Integer
If IncludeHeader Then
FromRow = StartRow
Else
FromRow = StartRow + 1
End If
With Worksheets(FromSheet)
lastrow = .Cells(.Rows.Count, StartCol).End(xlUp).Row
lastcol = .Cells(StartRow, .Columns.Count).End(xlToLeft).Column
'Just transfering value is faster then copy, but doesn't bring formatting
cSht.Cells(cSht.Rows.Count, 1).End(xlUp).Resize(lastrow - FromRow, lastcol - StartCol).Value2 = .Range(.Cells(FromRow, StartCol), .Cells(lastrow, lastcol)).Value2
End With
End Sub
With the code I am currently using it will paste the information from Worksheet 1 to worksheet 2 in the Top line of worksheet2. What I want next is to use the same code but for different cell values and to copy the information from worksheet 1 to worksheet 2 but in the next available line in worksheet 2.
I have been researching about excel macros and vba for a while now and I am still having trouble. I have worked on not using select and activate within my excel code but I am still having trouble with my code now. I am trying to automate my excel workbook as much as I can for easier use.
Sub Copy()
Dim Cell As Range
Dim myRow As Long
myRow = 1
With Sheets("Sheet1")
For Each Cell In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
If Cell.Value = "Tuck Chow" And Cell.Offset(0, 1).Value = "OPT" Then
.Rows(Cell.Row).Copy Destination:=Sheets("Sheet2").Rows(myRow)
myRow = myRow + 1
End If
Next Cell
End With
End Sub
I would do something like this:
Sub Copy()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim newRow As Long
'setting sheets
Set sh1 = ThisWorkbook.Worksheets("Sheet1")
Set sh2 = ThisWorkbook.Worksheets("Sheet2")
With sh1
For Each cel In .Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))
If cel.Value = "Tuck Chow" And cel.Offset(0, 1).Value = "OPT" Then
'getting new row on Sheet2
If sh2.Cells(1, 1) = "" Then
newRow = 1
Else
newRow = sh2.Cells(Rows.Count, 1).End(xlUp).Row + 1
End If
'copying
cel.EntireRow.Copy Destination:=sh2.Cells(newRow, 1)
End If
Next cel
End With
'deselecting row
sh2.Cells(1, 1).Select
End Sub
Try:
Option Explicit
Sub test()
Dim LastRow1 As Long, LastRow2 As Long, i As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow1
If .Range("A" & i).Value = "Tuck Chow" And .Range("B" & i).Value = "OPT" Then
LastRow2 = ThisWorkbook.Worksheets("Sheet2").Cells(ThisWorkbook.Worksheets("Sheet2").Rows.Count, "A").End(xlUp).Row
.Rows(i).Copy ThisWorkbook.Worksheets("Sheet2").Rows(LastRow2 + 1)
End If
Next i
End With
End Sub
I'm trying to display the contents of a merged cell in a For loop in Excel using VBA.
I have the a worksheet with very simple data in it
Here is my code:
'finding last record in my initial list
sheet_last_row = Sheets("mylist").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To sheet_last_row
last_row = Sheets("results").Cells(Rows.Count, 1).End(xlUp).Row
If Sheets("mylist").Cells(i, 1).Value = 2 Then
'test if cell is merged
If Sheets("mylist").Cells(i, 2).MergeCells Then
RowCount = Sheets("mylist").Cells(i, 2).Value
End If
Sheets("mylist").Cells(i, 1).EntireRow.Copy Sheets("results").Cells(last_row + 1, 1)
End If
Next i
I'm getting the following result with this code;
I'm new at this. Can anyone show me how to make this work.
You could try:
Option Explicit
Sub test()
Dim LastRowA As Long, LastRowB, LastRowC As Long, LastRowE As Long, MaxRow As Long
Dim cell As Range, rng As Range
With ThisWorkbook.Worksheets("Sheet1")
'Find the lastrow for all the available columns
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
LastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row
LastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row
'Get the longer last row in order to avoid losing data if the last cell of a column is merge or empty
MaxRow = WorksheetFunction.Max(LastRowA, LastRowB, LastRowC)
'Set the area to loop
Set rng = .Range("A2:C" & MaxRow)
'Start looping
For Each cell In rng
'If the cell is merger
If cell.MergeCells Then
'Find the last row of column E
LastRowE = .Cells(.Rows.Count, "E").End(xlUp).Row
'Paste cell value in column E
.Range("E" & LastRowE + 1).Value = cell.Value
'Paste cell address in column F
.Range("F" & LastRowE + 1).Value = cell.Address
End If
Next
End With
End Sub
Results:
I have a problem with the following vba script.
I want to copy some cells from one sheet to another.
The first sheet is selected based its name. The sheet where i want to paste the cells is selected based on cell B1 in the first sheet.
I am using the following code:
Dim ws as Worksheet
Dim LR3 as Long
Dim LR4 as Long
Dim LR5 as Long
Dim ws3 as Worksheet
For Each ws In ActiveWorkbook.Worksheets
If Not ws.Name Like "BC-*" Then
LR3 = ws.Cells(Rows.Count, 1).End(xlUp).Row
ws.Range("E" & LR3 + 1).Formula = "=SUM(E4:E" & LR3 & ")"
Dim i As Long, n As Long
n = ws.Cells(Rows.Count, 1).End(xlUp).Row
With ws.Range("S1")
.Formula = "=myJoin(A4:A" & n & ","""")"
.Value = .Value
End With
LR4 = ws.Cells(Rows.Count, 6).End(xlUp).Row
ws.Range("F4:F" & LR4).Copy
ws.Range("M4:M" & LR4).PasteSpecial Paste:=xlPasteValues
ws.Range("M4:M" & LR4).RemoveDuplicates Columns:=1, Header:=xlNo
LR5 = ws.Cells(Rows.Count, 13).End(xlUp).Row
ws.Range("M4:M" & LR4).Cut
Set ws3 = ws.Range("B1").Value
ws3.Range("A30").PasteSpecial xlPasteValues
You need to use:
Set ws3 = ActiveWorkbook.Worksheets(ws.Range("B1").Value)
for example. Adjust the workbook if required.