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
Related
I got little project in VBA and stuck on below topic
I need to Sum selected range in first empty cell in B column. I tried a small macro in which it sums the same row which mentioned in the vba.
This is what I've found and try to use
Sub Macro9()
'
' Macro9 Macro
'
'
Range("A3").Select
Selection.Copy
Range("A2").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=+SUM(R[1]C:R[3]C)"
Range("B2").Select
Selection.Copy
Range("B3").Select
Selection.End(xlToRight).Select
Range("S2").Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveSheet.Paste
Selection.End(xlToLeft).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Final.xlsx").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False
Range("A6").Select
Windows("copy.xlsm").Activate
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Selection.End(xlToLeft).Select
End Sub
I tried to searched for last non-empty cell in selected range so it won't search the whole column
To get the sum of all the non empty cells in Column "B" and return it to Column "B2" as I can understand with the image, following is the code which help you:
Range("B2").FormulaR1C1 = "=Sum(R3C:R" & ActiveSheet.UsedRange.Rows.Count + 1 & "C)"
You may use following code to get the sum of till first empty cell:
Range("B2").FormulaR1C1 = "=Sum(R3C:R" & Range("B3").End(xlDown).Row & "C)"
use SpecialCells method of Range object to get not empty cells in a given range
then use Areas property of Range object to loop through each group of consecutive not empty cells
Option Explicit
Sub SumThem()
Dim c As Range
For Each c In Intersect(ActiveSheet.UsedRange, Columns(2)).Offset(1).SpecialCells(xlCellTypeConstants).Areas ' loop through each "area" (i.e.: group of consecutive cells with "constant" values) of column B from row 2 downwards
c.Resize(1).Offset(-1, 0).Formula = "=+SUM(" & c.Address & ")" ' place the sum of current "area" in the cell right above it
Next
End Sub
I have to transpose rows to columns in excel using vba and the data is of around 500000.
The problem is that the data is not regular and is consistent.
Like there will be 4 rows then a blank then it can be three rows or one as well.
I want to transpose the group of data separated by a blank cell to be transposed to the the respective column in-front of the first entry.
Sub Transpose()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Loop
End Sub
I used this code but the problem is that it is skipping the data which is present in the single row.
Then this should do it, beware that I'm assuming where your data is and where is going to get paste, don't forget to change that:
Option Explicit
Sub Transpose()
Dim LastRow As Long 'last row on the sheet
Dim TransposeRow As Long 'row where we transpose
Dim x As Long 'columns
Dim C As Range 'faster looping through cells with For Each C in range
With ThisWorkbook.Sheets("MySheet") 'change this to your sheet
'To assign the last row im gonna assume your data is in column A or 1(B would be 2 and so...)
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Last row with data
TransposeRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 'on column B will be pasting the data
x = 2 'initialize x being 2 as for B column
For Each C In .Range("A2:A" & LastRow)
If C = vbNullString Then 'in case the cell is blank we jump a row
TransposeRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 ' recalculate row for transposing data
x = 2 'reinitialize column counter
Else
.Cells(TransposeRow, x) = C 'we copy the value to the row and column empty
x = x + 1 'add 1 column
End If
Next C
End With
End Sub
I have edited your code to show an approach that can work for you. You need to add a condition for one cell data.
Sub Transpose2()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
If IsEmpty(ActiveCell.Offset(1, 0).Value) Then
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Else
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
End If
Application.CutCopyMode = False
Selection.End(xlDown).Select
Loop
End Sub
Note: Using select is not generally a good idea. An example of cutting down select would be:
Sub Transpose3()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
If IsEmpty(ActiveCell.Offset(1, 0).Value) Then
ActiveCell.Copy ActiveCell.Offset(0, 1)
Else
Range(ActiveCell, ActiveCell.End(xlDown)).Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").End(xlDown).Select
End If
Application.CutCopyMode = False
Selection.End(xlDown).Select
Loop
End Sub
I need a macro to append a comma onto the beginning of a column of text strings. I recorded the action myself, but it limited itself to Column C (often, the text strings I need to do this with appear in a different column), and also limited the application of the range to the specific number of rows in the worksheet I recorded it on (in this case, 114).
Here is the original Record Macro output:
Sub AddCommaToESIID()
'
' AddCommaToESIID Macro
'
'
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C2").Select
ActiveCell.FormulaR1C1 = ","
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C114")
Range("C2:C114").Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]&RC[-1]"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E114")
Range("E2:E114").Select
Selection.Copy
Range("D2").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Columns("E:E").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
End Sub
I would like to modify this to achieve the following:
Make the macro apply to whatever Column I have selected, as opposed to Column C
Once I have selected a Column, make the macro apply to however many rows there are in the particular worksheet I'm working on.
Thanks in advance for your help!
This will change all non-formulas in whatever range you select
Sub AddCommaToESIID()
Dim rCell As Range
If TypeName(Selection) = "Range" Then
For Each rCell In Selection.Cells
If Not rCell.HasFormula Then
rCell.Value = "," & rCell.Value
End If
Next rCell
End If
End Sub
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'
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.