Someone can help me.
Have the following code to stack information from multiples worksheets into one ("database") - (Loop)
The only thing it is not working is the last action to copy the names of the workweeks in the "Database" Column "Aw".
Macro does not bring any error but the sheet names don't appear in the column "AW"
Any suggestion?
Sub Update()
' Update Templates
Dim All As Worksheet
Dim J As Integer
Dim Last As Long
Dim CopyRng As Range
Application.ScreenUpdating = False
' Update Consol
On Error Resume Next
Sheets("Database").Select
Range("A2:AL1048576").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Clear
For J = 6 To Sheets.Count
Sheets(J).Activate
Range("A2:AL2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Database").Range("A1048576").End(xlUp)(2)
.PasteSpecial xlPasteValues
Database.Cells(Last + 1, "AW").Resize(CopyRng.Rows.Count).Value = All.Name
End With
Next
End Sub
Try this (I removed all selections, because they didn't seem necessary):
Sub Update()
'Update Templates
Dim All As String
Dim j As Integer
Dim Last As Long 'Missing value
Dim CopyRng As Long 'No need for Range in this Sub
Application.ScreenUpdating = False
' Update Consol
On Error Resume Next
With ActiveWorkbook 'modify
For j = 6 To .Sheets.Count
.Sheets(j).Range("A2:AL" & _
.Sheets(j).Range("A2:AL2").End(xlDown).Row).Copy
CopyRng = .Sheets(j).Range("A2:AL2").End(xlDown).Row - 1
All = .Sheets(j).Name
End With
With .Sheets("Database").Range("A1048576").End(xlUp)(2)
.PasteSpecial xlPasteValues
'EDIT:
debug.print All 'check immediate window for correct string
.Sheets("Database").Cells(Last + 1, _
"AW").Value = All
' .Sheets("Database").Cells(Last + 1, _
' "AW").Resize(CopyRng).Value = All
End With
Next
End Sub
Didn't know what 'Last' var is for, so don't forget to address it before running the code. Hope this helps.
Related
I'm trying to add a vba code that looks in a column on sheet YTDFigures and sees if there is a duplicate in sheet EeeDetails. If there isn't then I want to copy the YTDFigures data and paste in a new sheet.
The code I've tried gets an error run time error 91 on the line FinName = Worksheets("EeeDetails").Range("A:A").Find(What:=SearchName, LookIn:=xlValues) I thought this would work as if a match isn't found the .Find function returns nothing.
Sub CheckMatch()
Application.ScreenUpdating = False
Dim SearchName As Range, SearchNames As Range
Dim Usdrws As Long
Dim row As Integer
Usdrws = Worksheets("YTDFigures").Range("A" & Rows.Count).End(xlUp).row
Set SearchNames = Worksheets("YTDFigures").Range("A2:A" & Usdrws)
For Each SearchName In SearchNames
row = Split(SearchName.Address, "$")(2)
FinName = Worksheets("EeeDetails").Range("A:A").Find(What:=SearchName, LookIn:=xlValues)
If FinName Is Nothing Then
Range("A" & row & ":S" & row).Copy
LastRow = Worksheets("Errors").Range("AA" & Rows.Count).End(xlUp).row + 1
Worksheets("Errors").Activate
Range("A" & LastRow).Select
Selection.PasteSpecial
Worksheets("EeeDetails").Activate
End If
Next
Application.ScreenUpdating = True
End Sub
You can place the raw data into an array, place the array on a temporary sheet, remove the duplicates, copy the data, then delete the temp sheet.
See below:
Sub CheckMatch()
Application.ScreenUpdating = False
Dim ws As Worksheet, tRows As Long
Set ws = ThisWorkbook.Worksheets(1)
Set RngA = ws.UsedRange.Columns("A")
tRows = ws.Rows(ws.Rows.Count).End(xlUp).row
Dim valA As Variant
valA = ws.Range(ws.Cells(1, 1), ws.Cells(tRows, 1)).Value
Dim tempWs As Worksheet
Set tempWs = ThisWorkbook.Worksheets.Add
tempWs.Name = "Temp1"
With tempWs
.Range(.Cells(1, 1), .Cells(tRows, 1)) = valA
With .UsedRange.Columns("A")
.RemoveDuplicates Columns:=1, Header:=xlYes
.Copy
End With
End With
' Do what you need to do with your copied data
Application.DisplayAlerts = False
tempWs.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Edit:
I just tested this with sample data of over 10k rows, and it works in less than a half a second. It's very fast.
I am using the following code:
Sub CSVParser()
Dim i As Integer
Dim x As Integer
Dim values As Range
Sheets("CSV Paste").Select
Range("A3").Select
For i = 1 To Range("A3", Range("A3").End(xlDown)).Rows.Count
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Working Sheet 1").Select
Range("A1").Select 'problem code
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
ActiveSheet.Paste
Sheets("CSV Paste").Select
ActiveCell.Offset(1, 0).Select
Next
End Sub
However, the line Range("A1").Select just after Sheets("Working Sheet 1").Select is kicking up a run-time error '1004'
Does anyone know why? I have rearranged this in every way I can think of an have typed it out from scratch again.
Give this version of your code a try:
Sub CSVParser()
Dim wb As Workbook
Dim wsCSV As Worksheet
Dim wsWork As Worksheet
Set wb = ActiveWorkbook
Set wsCSV = wb.Sheets("CSV Paste")
Set wsWork = wb.Sheets("Working Sheet 1")
wsCSV.Range("A3").CurrentRegion.Copy wsWork.Cells(wsWork.Cells.Count, "A").End(xlUp).Offset(1)
End Sub
Using .Select and .Activate is not considered 'best practice'. See How to avoid using Select in Excel VBA macros. Yes, using the code from the macro recorder is a good place to start but you have to get away from the practice at some point.
Performing bulk operations is preferred to looping through an indeterminate number of rows or columns.
Option Explicit
Sub CSVParser()
Dim lastCol As Long
With Worksheets("CSV Paste")
With .Range(.Cells(3, "A"), .Cells(.Rows.Count, "A").End(xlUp))
lastCol = .CurrentRegion.Columns.Count
With .Resize(.Rows.Count, lastCol)
.Copy Destination:=Sheets("Working Sheet 1").Range("A1")
End With
End With
End With
End Sub
I think this is what you are trying to achieve (without all the unnecessary Select):
Option Explicit
Sub CSVParser()
Dim i As Long
Dim x As Long
Dim LastRow As Long
Dim PasteRow As Long
With Sheets("CSV Paste")
LastRow = .Range("A3").End(xlDown).Row
For i = 3 To LastRow
PasteRow = Sheets("Working Sheet 1").Cells(Sheets("Working Sheet 1").Rows.Count, "A").End(xlUp).Row
.Range(.Range("A" & i), .Range("A" & i).End(xlToRight)).Copy Destination:=Sheets("Working Sheet 1").Range("A" & PasteRow + 1)
Next i
End With
End Sub
Can someone help me with an VBA script that copy entire rows with the condition TRUE from column U in TempSheet over to TempSheet2.
use something like this:
Sub test()
Dim i&, z&, oCell As Range
Application.ScreenUpdating = 0
z = 1: i = Sheets("TempSheet").Cells(Rows.Count, "U").End(xlUp).Row
For Each oCell In Sheets("TempSheet").Range("U1:U" & i)
If oCell.Value = True Then
oCell.EntireRow.Copy Sheets("TempSheet2").Rows(z)
z = z + 1
End If
Next
Application.ScreenUpdating = 1
End Sub
This macro checks each row for the value "True" in the U column.
The columns with true value in column U as then copied to the other sheet.
Option Explicit
Sub CopyRow()
Dim Row As Integer
Dim sRow As String
Dim i As Long
Application.ScreenUpdating = False
i = 1 'To ensure each time the macro is run it starts at row 1
For i = 1 To 1048576 'for each row in the sheet
If Range("U" & i).Value = True Then 'If the U value is true then copy it
Row = i
sRow = CStr(Row) 'convert row number to string
Rows(sRow & ":" & sRow).Select
Selection.Copy
Sheets("Sheet2").Select
Rows(sRow & ":" & sRow).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Application.CutCopyMode = False
End If
Next i
Range("A1").Select
Application.ScreenUpdating = True
End Sub
I am uncertain as to how you want the macro triggered but a worksheet change may suit.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' trigger the macro if they are changed
Set KeyCells = Range("U:U")
Call CopyRow
End If
End Sub
Note: Worksheet_Change goes in the code for sheet1 and the macro goes in a module
I have the following VBA code that takes a single row from Sheet "Tabled data", copies the data, then pastes the data into the next available row in Sheet "Running list". However the original row has formulas and I need the values to paste, not the formulas. I've seen numerous ways to do it with Range.PasteSpecial but this code didn't use Range and I'm not sure how to incorporate it.
Note: I modified this code from here: http://msdn.microsoft.com/en-us/library/office/ff837760(v=office.15).aspx. It originally had an IF statement to match content in a cell then paste it in a certain sheet according to the content in the cell. I only had one sheet to copy to and didn't need the IF. I don't really need to find the last row of data to copy either as it will only ever be one row with range of A2:N2. But if I take out the FinalRow section and the For and replace with Range("A2:N2") it doesn't work so I left those in.
Any guidance on how to add in the PasteValues property without making this more complicated? I'm also open to simplification of the For or FinalRow variable such as using Range. I'm only sort of familiar with VBA, having done a few things with it, but usually after much searching and modifying code.
Public Sub CopyData()
Sheets("Tabled data").Select
' Find the last row of data
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each row
For x = 2 To FinalRow
ThisValue = Cells(x, 1).Value
Cells(x, 1).Resize(1, 14).Copy
Sheets("Running list").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("Tabled data").Select
Next x
End Sub
Hopefully we can actually make this more simple.
Public Sub CopyRows()
Sheets("Sheet1").UsedRange.Copy
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
'check if the last cell found is empty
If IsEmpty(ActiveSheet.Cells(lastrow, 1)) = True Then
'if it is empty, then we should fill it
nextrow = lastrow
Else
'if it is not empty, then we should not overwrite it
nextrow = lastrow + 1
End If
ActiveSheet.Cells(nextrow, 1).Select
ActiveSheet.Paste
End Sub
edit: I expanded it a little so that there won't be a blank line at the top
I found a working solution. I recorded a macro to get the paste special in there and added the extra code to find the next empty row:
Sub Save_Results()
' Save_Results Macro
Sheets("Summary").Select 'renamed sheets for clarification, this was 'Tabled data'
'copy the row
Range("Table1[Dataset Name]").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
' paste values into the next empty row
Sheets("Assessment Results").Select
Range("A2").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' Return to main sheet
Sheets("Data Assessment Tool").Select
End Sub
Just copy the data all at once, no need to do it a row at a time.
Sub CopyData()
With ThisWorkbook.Sheets("Tabled data")
Dim sourceRange As Range
Set sourceRange = .Range(.Cells(2, 1), .Cells(getLastRow(.Range("A1").Parent), 14))
End With
With ThisWorkbook.Sheets("Running list")
Dim pasteRow As Long
Dim pasteRange As Range
pasteRow = getLastRow(.Range("A1").Parent) + 1
Set pasteRange = .Range(.Cells(pasteRow, 1), .Cells(pasteRow + sourceRange.Rows.Count, 14))
End With
pasteRange.Value = sourceRange.Value
End Sub
Function getLastRow(ws As Worksheet, Optional colNum As Long = 1) As Long
getLastRow = ws.Cells(ws.Rows.Count, colNum).End(xlUp).Row
End Function
Private Sub Load_Click()
Call ImportInfo
End Sub
Sub ImportInfo()
Dim FileName As String
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim ActiveListWB As Workbook
Dim check As Integer
'Application.ScreenUpdating = False
Set WS2 = ActiveWorkbook.Sheets("KE_RAW")
confirm = MsgBox("Select (.xlsx) Excel file for Data transfer." & vbNewLine & "Please ensure the sheets are named Sort List, Second and Third.", vbOKCancel)
If confirm = 1 Then
FileName = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*),*.xls*", _
Title:="Select Active List to Import", MultiSelect:=False)
If FileName = "False" Then
MsgBox "Import procedure was canceled"
Exit Sub
Else
Call CleanRaw
Set ActiveListWB = Workbooks.Open(FileName)
End If
Set WS1 = ActiveListWB.Sheets("Sort List")
WS1.UsedRange.Copy 'WS2.Range("A1")
' WS2.Range("A1").Select
WS2.UsedRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'WS2.Range ("A1")
ActiveWorkbook.Close False
'Call ClearFormulas
' Call RefreshAllPivotTables
Sheets("Key Entry Data").Select
'Sheets("Raw").Visible = False
'Application.ScreenUpdating = True
MsgBox "Data has been imported to workbook"
Else
MsgBox "Import procedure was canceled"
End If
Application.ScreenUpdating = True
End Sub
Sub CleanRaw()
Sheets("KE_RAW").Visible = True
Sheets("KE_RAW").Activate
ActiveSheet.Cells.Select
Selection.ClearContents
End Sub
i need some help as i am experienceing a problem, pretty new to VBA. i want to copy that data from L5-L18, excluding some cells and paste it to column B of sheet(In) and create a button that every time i push it to copy the data from column B ,sheet(Data) to the sheet(in) and move columnto the right. like first time column b, next time column c...every time i push the button.. much appreciated
Sub Macro2()
Sheets("Data").Select
Range("L5,L6,L7,L8,L9,L10,L13,L14,L15,L16,L17,L18").Select
Range("L18").Activate
Selection.Copy
Sheets("In").Select
Range("B5").Select
ActiveSheet.Paste
Range("B5").Offset(0, 1).Select
End Sub
Method A
To insert into column B and shift everything else to the right try this:
Sub offsetCol()
Dim wksData As Worksheet
Set wksData = Sheets("Data")
Dim wksIn As Worksheet
Set wksIn = Sheets("In")
Application.CutCopyMode = False
Columns("B:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
wksIn.Range("B5:B10").Value = wksData.Range("L5:L10").Value
wksIn.Range("B11:B16").Value = wksData.Range("L13:L18").Value
End Sub
Method B
Find last column in sheet and tack on information to next available:
Sub offsetCol()
Dim wksData As Worksheet
Set wksData = Sheets("Data")
Dim wksIn As Worksheet
Set wksIn = Sheets("In")
Set rLastCell = wksIn.Cells.Find(What:="*", After:=wksIn.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
wksIn.Range(Cells(5, rLastCell.Column + 1), Cells(10, rLastCell.Column + 1)).Value = wksData.Range("L5:L10").Value
wksIn.Range(Cells(11, rLastCell.Column + 1), Cells(16, rLastCell.Column + 1)).Value = wksData.Range("L13:L18").Value
End Sub
Method C
Find last column in row 5 and tack on info in next available column:
Sub offsetCol()
Dim wksData As Worksheet
Set wksData = Sheets("Data")
Dim wksIn As Worksheet
Set wksIn = Sheets("In")
Dim rLastCol As Integer
rLastCol = wksIn.Cells(5, wksIn.Columns.Count).End(xlToLeft).Column + 1
wksIn.Range(Cells(5, rLastCol), Cells(10, rLastCol)).Value = wksData.Range("L5:L10").Value
wksIn.Range(Cells(11, rLastCol), Cells(16, rLastCol)).Value = wksData.Range("L13:L18").Value
End Sub
Starting Data:
Results (Method C):
Part of the question - copy from location A to location B, is not very clear, but I am guessing this is what you need. Put this under a macro of a button:
Sub Macro2()
Dim rng As Range
Sheets("IN").Range("B:B").Insert Shift:=xlToRight
Sheets("Data").Select
Set rng = Range("L5,L6,L7,L8,L9,L10,L13,L14,L15,L16,L17,L18")
rng.Copy Sheets("IN").Range("B:B")
End Sub