I'm taking data that is listed across multiple columns and putting it into a single column (A). If there is data in column B, it grabs that data, sticks it at the end of the data in column A, then goes back and deletes the now empty column B, which moves all the other columns over one so now there is data in column B again, up until the point there are no more columns of data except for column A. The way I'm doing this currently is by listing multiple blocks of the same code below which is not efficient obviously and sooner or later the code will break. Any advice is appreciated!!
Range("B1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Selection.End(xlUp).Select
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.Delete Shift:=xlToLeft
ActiveCell.Offset(0, -1).Range("A1").Select
I like Christmas007's answer. I wanted to share this solution too:
Sub MoveIt()
Dim mysht As Worksheet
Set mysht = ActiveSheet
Set myrng = mysht.UsedRange
nextrow = mysht.Cells(mysht.Rows.Count, 1).End(xlUp).Row
For i = 2 To myrng.Columns.Count
lastColrow = myrng.Cells(mysht.Rows.Count, i).End(xlUp).Row
If lastColrow <> 1 Or myrng.Cells(1, i) <> "" Then
For j = 1 To lastColrow
nextrow = nextrow + 1
mysht.Cells(nextrow, 1) = myrng.Cells(j, i)
Next j
End If
Next i
Range(myrng.Columns(2), myrng.Columns(myrng.Columns.Count)).Clear
End Sub
I like it because it doesn't use the copy, paste, and delete functions. In my experience these functions start to cause the macro to drag if you are dealing with big workbooks and they also require that the sheet is activated.
There is a pretty simple way to do this:
Sub MoveIt()
Dim LastRow As Long
Dim ws1 as Worksheet
Set ws1 = Sheets("Name of Sheet")
Do While (ws1.Range("B1").Value <> "")
LastRow = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row + 1
ws1.Range("B1:B" & ws1.Range("B" & ws1.Rows.Count).End(xlUp).Row).Copy
ws1.Range("A" & LastRow).PasteSpecial
ws1.Range("B1").EntireColumn.Delete xlToLeft
Loop
End Sub
Related
I am trying to copy data from multiple tabs to one single tab. The data need to be filtered first then copied from different tabs to a new tab. Data from different tabs (has random number of lines)should be continuous within the new tab. Due to the size of the data, it is divided into multiple tabs. So merging tabs into one tab first is not an option.
I have below difficulties that need help:
From second tab, I don’t need to copy the header of data. Any command can be added to the code?
Current codes not copying all four tabs, I am not too sure what is the issue
Can my active sheet be a general command instead of specific like ActiveSheet.Range("$A$1:$U$493692")?
See below code
Sub Filter_FSI()
'
' Filter_FSI Macro
'
'
Dim lastRow As String
lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Train 3-8").Select
ActiveSheet.Range("$A$1:$U$493692").AutoFilter Field:=4, Criteria1:="FSI"
ActiveSheet.AutoFilter.Range.Copy
Sheets("Sheet1").Select
Range("A1").Select
Sheets("Sheet1").Paste
Sheets("Train 9-14").Select
ActiveSheet.Range("$A$1:$U$539243").AutoFilter Field:=4, Criteria1:="FSI"
ActiveSheet.AutoFilter.Range.Copy
Sheets("Sheet1").Select
Range("A" & lastRow).Select
ActiveSheet.Paste
Sheets("Train 15-25").Select
ActiveSheet.Range("$A$1:$U$528028").AutoFilter Field:=4, Criteria1:="FSI"
ActiveSheet.AutoFilter.Range.Copy
Sheets("Sheet1").Select
Range("A" & lastRow).Select
ActiveSheet.Paste
Sheets("Train 27-41").Select
ActiveSheet.Range("$A$1:$U$298055").AutoFilter Field:=4, Criteria1:="FSI"
ActiveSheet.AutoFilter.Range.Copy
Sheets("Sheet1").Select
Range("A" & lastRow).Select
ActiveSheet.Paste
Sheets("Sheet1").Copy
Windows("Train Data JULY_Sam Edit.xlsb").Activate
End Sub
So a couple things I noticed with your code - you're declaring lastrow as a string, but that should really be a long since it's representing a number.
Personally, I'm not a fan of autofiltering - and like Peh said above, you want to avoid using Select and Copy/Paste when you can. Try this solution below - it's my personal preference of doing things. We loop through all your worksheets, then loop through every cell in Column D - if it is equal to "FSI", we bring it to Sheet1:
Option Explicit
Sub Filter_FSI()
Dim sht As Worksheet, sht2 As Worksheet
Dim lastrow As Long, i As Long, j As Long, k As Long
Dim myworksheets As Variant
Set sht = ThisWorkbook.Worksheets("Sheet1")
myworksheets = Array("Train 3-8", "Train 9-14", "Train 15-25", "Train 27-41")
'Bring in headers
sht.Range("A1:U1").Value = Worksheets("Train 3-8").Range("A1:U1").Value
k = 2
For i = 0 To UBound(myworksheets)
Set sht2 = Worksheets(myworksheets(i))
lastrow = sht2.Cells(sht2.Rows.Count, 4).End(xlUp).Row
For j = 2 To lastrow
If sht2.Cells(j, 4).Value = "FSI" Then
sht.Range("A" & k & ":U" & k).Value = sht2.Range("A" & j & ":U" & j).Value
k = k + 1
End If
Next j
Next i
End Sub
On the same worksheet, I'm trying to compact all my cell data, i.e. move all the cells with value to be next to each other instead of spread apart. The original sheet looks like this:
The desired output would be something like this:
I have tried below code to solve this problem, and sorry I'm new here so don't know how to ask the question
Sub SelectRangea()
Sheets("Sheet1").Select
Range("a1:cf1").Select
Application.CutCopyMode = False
Selection.Copy
With Sheets("Sheet1")
lst = .Range("A" & Rows.Count).End(xlUp).Row + 1
.Range("A" & lst).PasteSpecial xlPasteColumnWidths
.Range("A" & lst).PasteSpecial xlPasteValues
End With
End Sub
This code solves the problem as per your sample data.
Dim c As Long
c = 1
With Worksheets("sheet6")
c = .Cells(1, c).End(xlToRight).End(xlToRight).Column
Do While c < .Columns.Count
With .Range(.Cells(1, c), .Cells(1, c).End(xlToRight))
.Parent.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, .Columns.Count) = .Cells.Value
.Clear
End With
c = .Cells(1, c).End(xlToRight).Column
Loop
End With
If one of the 'islands' of data in the first row is a single cell then you will have to accommodate that special condition.
I have a macro which is part of a few, however this is the first to tidy up sheet before running
To determine the column and table to tidy I am trying to find the last empty value and create a column and table range to use as variable throughout my modules. However failing at an early bit whether I choose cells C4 or C5 or refer to them in R1C1 style it breaks as if cells were empty however they are not.
It breaks at
LRow = ws.Cells(Rows.Count, C5).End(xlUp).Row
Unsure how to get it to proceed.
Sub Tidy()
'
' Tidy Macro
'
'
Dim table_1 As Long
Dim table_2 As Long
Dim col_len, table_len As Range
Dim LRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
LRow = ws.Cells(Rows.Count, C5).End(xlUp).Row
Set col_len = ws.Range("C4:C" & Cells(LRow).Address(False, False))
Set table_len = ws.Range("A4:F" & Cells(LRow).Address(False, False))
table_2 = Worksheets("DumpSheet").Cells(Row.Count, R5C10).End(xlUp).Row
Range("A5").Select
ActiveCell.FormulaR1C1 = "=R1C1"
Range("A5").Select
Selection.AutoFill Destination:=Range("A5:A" & col_len)
Range(table_len).Select
Selection.Copy
Range("H5").Select
ActiveSheet.Paste
Range("B5").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-4]C"
Range("B5").Select
ActiveCell.FormulaR1C1 = "=R1C2"
Range("B5").Select
Selection.AutoFill Destination:=Range("B5:B" & col_len)
Range("B5:B" & table_1).Select
Range("I5").Select
ActiveCell.FormulaR1C1 = "=R1C9"
Range("I5").Select
Selection.AutoFill Destination:=Range("I5:I29")
Range("I5:I" & table_2).Select
End Sub
C5 is being treated as a variable that hasn't been assigned. It is not a cell reference. You're looking for something more like:
LRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row
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 have been trying to create a macro that will go through a spreadsheet and copy figures in Cells E, then paste them into Cell K and L, then repeat as the macro transverse column E. i.e. E1 will be copied to K1 and L1, E2 will be copied to K2, L2 etc...
This is what i have done so far:
Sub uy()
'
' Macro1 Macro
' lo
'
Range("E299").Select
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value < 0 Then
Selection.Copy
Range("K299").Select
Do Until IsEmpty(ActiveCell)
ActiveSheet.Paste
Loop
Range("L299").Select
Do Until IsEmpty(ActiveCell)
ActiveSheet.Paste
Loop
Else
Range("L299").Select
Do Until IsEmpty(ActiveCell)
ActiveSheet.Paste
Loop
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
When i run the macro it just highlights cell E229 with a broken line box and Cells K299, L299 are left blank. I feel Range("K299").Select, Do Until IsEmpty(ActiveCell), ActiveSheet.Paste part is selecting and copying a blank cell, so it will terminate itself as it meets the "Do Until IsEmpty(ActiveCell)" criteria.
Is there a way for me to fix this?
I'm not quite sure if i got you right.
but if you want to just copy one range to an other then
this would do it.
Private Sub CommandButton1_Click()
For i = 1 To Cells(Rows.Count, 5).End(xlUp).Row
If Cells(i, 5) <> "" Then
Cells(i, 11).Value = Cells(i, 5).Value
Cells(i, 12).Value = Cells(i, 5).Value
End If
Next i
End Sub
This marco works as long as cell 'i' in column E isn't empty
and takes the value of cell 'i' in column E and puts it into column K and L
Kind regards
First, don't use Activate or Select. They're 99% useless in most code. Next, don't use copy and paste. It's slow for this kind of approach.
The following code is much more streamlined and faster.
Sub EtoKL()
Dim WS As Worksheet
Dim LRow As Long, Iter As Long
Set WS = ThisWorkbook.Sheets("Sheet1") 'Change as necessary.
With WS
LRow = .Range("E" & .Rows.Count).End(xlUp).Row 'Get last used row in Column E.
For Iter = 1 To LRow 'Iterate from 1 to last used row.
Union(.Range("K" & Iter), .Range("L" & Iter)).Value = .Range("E" & Iter).Value
Next
End With
End Sub
Let us know if this helps.
I think something like this would work.
Sub Copy()
Dim intRowCount as Long
Dim intLastRow as Long
intRowCount = 2
intLastRow = Application.CountA(Sheets(1).Range("e:e"))
For intRowCount = 2 To intLastRow
Sheets(1).Range("K" & intRowCount ).Value = Sheets(1).Range("E" & intRowCount ).Value
Sheets(1).Range("L" & intRowCount ).Value = Sheets(1).Range("E" & intRowCount ).Value
Next
End Sub