Excel macro column splitting - excel

Looking for help on a macro to take chunks of data on further rows, and place them into columns instead.
I've attached a picture to depict this. All of the chunks of data will split determined by the first column, 1 or 2 in the picture. I simply want to move chunk two up and next to 1. The only problem I've run into is that for each chunk, the number of columns is variable.
Edit: Image link incase the embedded isn't showing up: enter link description here
Would this be relatively close?
Sub macro()
Dim wav_name As String
Range("A1").Select
Do While ActiveCell.Value <> ""
ActiveCell.Offset(0, 2).Select
wav_name = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Do
If ActiveCell.Value = wav_name Then
ActiveCell.Offset(1, 0).Select
Else
Exit Do
End If
Loop
Range(ActiveCell.Offset(0, -2), ActiveCell.End(xlDown)).Select
Selection.Cut
ActiveCell.End(xlUp).Offset(0, 3).Select
ActiveSheet.Paste
Loop
Range("A1").Select
End Sub

What you have there is pretty workable with a one key exception.
Your cut selection is only grabbing the first row of data. You will need to change it to
Range(ActiveCell).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
To handle the variable number of columns, you can capture the last column in section one by adding a varabile (i.e. LastCol) and putting the following code in your Do Loop
LastCol = Activecell.End(xlToRight).Column
Then replace the 3 in your last offset statement with your variable
Note that you can refactor the code to remove many of the select statements (includeing the ones I have mentioned above) if you need to improve the preformance of your code, but what you have written will work for you.
EDIT: Here is what your end code would look like
Sub macro()
Dim wav_name As String
Dim LastCol as Long
Range("A1").Select
Do While ActiveCell.Value <> ""
ActiveCell.Offset(0, 2).Select
wav_name = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
LastCol = Activecell.End(xlToRight).Column
Do
If ActiveCell.Value = wav_name Then
ActiveCell.Offset(1, 0).Select
Else
Exit Do
End If
Loop
Range(ActiveCell.Offset(0, -2), ActiveCell.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Cut
ActiveCell.End(xlUp).Offset(0, LastCol +1).Select
ActiveSheet.Paste
Loop
Range("A1").Select
End Sub
I haven't tested this, so you may have to do some debugging... but it is now logically correct.

Related

Increment Rows in ElseIf-Loop

I would like to copy certain cells from a report to another worksheet.
There are two conditions and two rows to copy if one of the conditions applies.
As I am new to VBA, I don`t get the loop to work.
Main problem: How to increment the row that is to be searched and that is the paste target (B1->B2->B3..).
The loop is supposed to check and copy until the last row of the report.
So the
Can somebody help me how to code the loop or explain in an easy way how to do that? Thank you! :)
Sub CopyRow()
'Return to Sheets("CS15 Download"), Find Last Row and LastRow = that row
Sheets("CS15").Select
Range("A8").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.End(xlUp).Select
LastRow = ActiveCell.Row
Dim ObjDes As Variant
Const Lvl As Integer = 1
ObjDes = Range("Q1").Value
Range("B3").Select
If Range("B3").Value = Lvl Then
Sheets("Report").Range("B2").Value = Range("F3").Value
Sheets("Report").Range("C2").Value = Range("G3").Value
ActiveCell.Offset(1, 0).Range("A1").Select
ElseIf InStr(Range("G3").Value, ObjDes) > 0 Then
Sheets("Report").Range("B2").Value = Range("F3").Value
Sheets("Report").Range("C2").Value = Range("G3").Value
ActiveCell.Offset(1, 0).Range("A1").Select
Else
ActiveCell.Offset(1, 0).Range("A1").Select
End If
End Sub

VBA to look for information in between two dates that are written manually in a specific cell

I'm looking for a VBA code that will look in the 2nd row for specific dates.
Basically, I'm writing a "Begin" date (ex: 2018-07-01) and an "End" date (ex: 2018-07-31) and I want my code to look for everything in between, including those dates, and copy paste all the information in another excel sheet. What the function will do at the end is it'll look into many sheets for those dates and copy every bit of information that are below those dates, and paste them all into one main sheet.
What I'm attaching below is what I've done so far:
Sub Copie()
Sheets("1").Select
Range("B1:H1").Select
Selection.Copy
Sheets("Per Employe").Select
Range("A4").Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Range("J1:P1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Cells(1, 1).Select
ActiveCell.Offset(1, 0).Select
Selection.Copy
Sheets("Per Employe").Select
Cells(4, 1).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("1").Select
Range("B2:H2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Range("J2:P2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
End Sub
What I'm trying to add is the function to look up the dates and only copy/paste the dates that were asked for in the main excel sheet, which would be "Per Employe". Would anyone have any solution to this? The cells that contain the begin and end date would be D1 & F1.

Select range "not working"

I have tried to select one range in Excel whose first column is filled with continuous data (10-20 rows) and in the range there can be empty cells. I recorded one macro but when I run this, it is not working.
Where is the mistake?
'Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select`
First there seems to be a typo in the code. At the end of the last statement you have a stray ` character.
What it seems like you want excel to do is the equivalent of CTRL+Shift+Down,Right,Right,Right. What the code is actually doing is Ctrl+[Arrow key] then expand the original selection to this new cell. Microsoft tells us that CTRL+[ArrowKey] brings us to the edge of the current region. As an illustration:
So since you have a range selected you will just be reselecting the same range every time!
What might be a solution for you is using the last column when trying to select ranges which require calling .End(xlToRight) multiple times:
'Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.Cells(1,Selection.Columns.Count).End(xlToRight)).Select
Range(Selection, Selection.Cells(1,Selection.Columns.Count).End(xlToRight)).Select
Range(Selection, Selection.Cells(1,Selection.Columns.Count).End(xlToRight)).Select
Which is the equivalent to pressing CTRL+Shift+Down,Right,Right,Right.
Let me know if you have more problems :)
You could try something like this (you may want to vary this a little)
Cells(ActiveCell.Row, 1000).Select
Selection.End(xlToLeft).Select
Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, ActiveCell.Column)).Select
Range(Selection, Selection.End(xlDown)).Select
Instead of trying to keep going across until you hit the final filled cell - this starts off 1000 columns to the right then selects a range from column one to the last filled one (regardless of gaps)
I believe this would fit to the scenario mentioned - as it also would then go to the end of the filled selection.
You could however, adapt the code, to do the same thing for the rows.
The table had empty cells in C2, E2
' Sub Macro2()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
Sheets("Sheet2").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select -Select first Column until exists data
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select -Did not make step over C2 (empty cell)
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Sheet3").Select
Range("A1").Select
sourceCol = 1
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
For currentRow = 1 To rowCount + 1
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
End If
Next
ActiveSheet.Paste
Sheets("Sheet2").Select
Range("A2:A100").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("C2:C100").Select
Selection.ClearContents
ActiveWorkbook.Save
Range("A2").Select
End Sub'

VBA Loop and if statement issue

I have an issue with the following code.
I want each cell with the value "long" in the column "U" to be copied in a new sheet.
But the code I developed only retrieves the first result. It does stop after "U6".
Can you please help me?
Sub reportcrea ()
Worksheets("TLM").Select
ActiveSheet.Range("U3").Select
Do
If ActiveCell.Value = "long" Then
ActiveCell.EntireRow.Select
Selection.Copy
Sheets("report").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Else
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell.Offset(1, 0))
End sub ()
I found a bug in your code in this line:
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Offset takes two parameters, so it should be something like this:
Range("A" & Rows.Count).End(xlUp).Offset(1,0).Select
Also, you should cancel CutCopy mode right after you paste what is in the clipboard:
ActiveSheet.Paste 'Paste is done here
Application.CutCopyMode = False 'This is a good practice
See if that helps. Also, a screenshot of the TLM sheet would help us analyze the problem more accurately.
First up, End Sub shouldn't have trailing brackets. When I copied it into a module it highlighted an error straight away.
Your loop is using ActiveCell.Offset(1, 0).Select twice:
If ActiveCell.Value = "long" Then
ActiveCell.EntireRow.Select
Selection.Copy
ActiveCell.Offset(1, 0).Select 'first Offset
Sheets("report").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("TLM").Select
ActiveCell.Offset(1, 0).Select 'second Offset
Else
so you're only looking at every second row after each "long" is found.
I tested your code on 10 contoguous "long" cells and got 5 back in the report sheet. I couldn't reproduce your U6 stop.

Copy and paste array of data to remove blanks rows of data

I'm currently trying to develop a script which moves from one sheet to another and copies the data from one table to another. The problem I’m having is the source table doesn't have all rows populated with data and the destination needs to be presented with the data collapsed without blank rows.
The source data can vary from 100 to 1000 rows each time the script is used.
I have tried a number of solutions, remove blanks, remove duplicates, and these don't work.
Here is the script I have been using.
Sub AS1055datacrunch()
Sheets("Data Extract").Select
Range("BI3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("AS 1055 Table").Select
Range("C8").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True
Call RemoveGaps
End Sub
Sub RemoveGaps()
With Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
.value = .value
.RemoveDuplicates Columns:=1, Header:=xlNo
On Error Resume Next
.SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
On Error GoTo 0
End With
End Sub
I'm wondering is there any way I can have the data copied into an array of some kind and then pasted in a consolidated table of data.
this should work, it deletes blank rows
Sub RemoveGaps()
Dim ro As Integer, first As Integer, last As Integer
first = Selection.Row
last = first + Selection.Rows.Count - 1
For ro = last To first Step -1
''checking for blank columns in column c to e
If Application.WorksheetFunction.CountA(Range("C" & ro & ":" & "E" & ro)) = 0 Then
Range(ro & ":" & ro).Rows.Delete Shift:=xlUp
End If
Next ro
End Sub

Resources