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'
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 a workbook with sheets containing data. I need the unique number to be filtered and copy the filtered data to another sheet, sheet name would be the unique number.
'i have tried to get all the numbers and remove duplicates, the remaining should be the filtered number to be copied.
'the error is i can copy the data to different sheets but not filtered according to their unique number
Sub filter()
Dim i As Integer
Dim ST As String
On Error Resume Next
i = 1
Application.ScreenUpdating = False
Do
ST = Sheets("duplicateshipto").Range("A" & i).Value
If ST <> "" Then
Sheets.Add.Name = ST
With Sheets("Template")
.Select
.Range("C1:BQ4").Select
Selection.Copy
Sheets(ST).Select
Sheets(ST).Range("A1").Select
Sheets(ST).Paste
.Select
ActiveSheet.Range("$A$4:$BU$88").AutoFilter Field:=26, Criteria1:=gsd
.Range("Z4", .Range("BS" & .Rows.Count).End(xlUp)).Select
Range("Z4").Select
Selection.End(xlDown).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
.Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets(ST).Select
Sheets(ST).Range("x5").Select
Sheets(ST).Paste
.Select
Range("BQ4").Select
Selection.End(xlDown).Select
.Range(Selection, Selection.End(xlDown)).Select
.Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets(ST).Select
Sheets(ST).Range("BO6").Select
Sheets(ST).Paste
.Select
Range("Y4").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
'Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Sheets(ST).Select
Sheets(ST).Range("a5").Select
Sheets(ST).Paste
End With
i = i + 1
End If
Loop Until ST = ""
Application.ScreenUpdating = True
End Sub
You are essentially trying to do something called Advanced Filter. However your code needs some improvement on all of the select options (as the comments state). Here's a sample macro that has several components you'll need for what you're trying to do such as:
It dynamically captures the range of all to the populated cells to the left and down of cell F6.
Uses the green range as what to filter (if not just leave F3:H3 blank)
Inserts values starting in cell A1 with a dynamic number of columns based on the number of data columns.
Excludes duplicates using Unique:=True (only one duplicate in sample)
Before Macro
After Macro
Code used in above illustration.
Sub exampleRefresh()
Dim cRng As Range, WS As Worksheet
Set WS = ActiveSheet
With WS
Set cRng = Sheet1.Range("F6")
Set cRng = Range(cRng, cRng.End(xlToRight))
Set cRng = Range(cRng, cRng.End(xlDown))
Dim fRng As Range
Set fRng = WS.Range("F2:H3")
Dim PRNG As Range
Set PRNG = WS.Range("A1")
Set PRNG = Range(PRNG, PRNG.Offset(, cRng.Columns.Count - 1))
End With
cRng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=fRng, CopyToRange:=PRNG, Unique:=True
End Sub
Also, as a teaser, Microsoft is going to be deploying a new Spill Feature soon. With this functionality, if you wanted to list the distinct values of a column, you could use a formula such as =Unique(A:A) in any cell and it will create a distinct list. No VBA or excessive clicking needed!
I'm having a problem with setting up a macro. Let me try to explain:
I've got two sheets, in sheet1 I've got pure data, in sheet2 I've got a macro that calculates how many pieces you can put into a certain container by using dimensions given in certain cells. As I said I've got a pure data in sheet1 which contains a box number, its width, length a container number its going to be packed and that container's width and length. Now what I want is I want the macro to copy the dims from sheet1, put them in the specific cells in sheet2, run the calculating macro, copy the cell with result, paste it in the sheet1 and carry on with the next row until there's no data in the column next to it.
This what I've got so far but I don't know how to loop it for the next rows. I think there might be something with selecting cells that will make this possible rather then specifying the cells by name. Any help will be much appreciated.
Sub AutoCalculating()
Sheets("Sheet1").Select
Range("A2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("B2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("B2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("B3").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("B4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("E2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("B5").Select
ActiveSheet.Paste
Call CalculateBoxes
Range("B6").Select
Selection.Copy
Sheets("Sheet1").Select
Range("F2").Select
ActiveSheet.Paste
End Sub
`
Found the solution, I did the code by offsets not by named cells and then looped it down until it finds an empty cell in the next row. Here's the is the code if somebody would struggle with such thing like I did. You just need to make sure before playing this macro we need to make active the first cell in the first empty column next to the data
Sub AutoCalc2()
Do
ActiveCell.Offset(0, -1).Select
If IsEmpty(ActiveCell) Then
Exit Do
Else
ActiveCell.Offset(0, 1).Select
ActiveCell.Offset(0, -5).Copy
Sheets("Sheet2").Select
Range("B2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy 1st cell which is +5 cells to the left next to the acive
ActiveCell.Offset(0, -4).Copy
Sheets("Sheet2").Select
Range("B3").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy 2nd cell which is +4 Cells to the left next to the acive
ActiveCell.Offset(0, -2).Copy
Sheets("Sheet2").Select
Range("B4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy 3rd cell which is +2 cells to the left next to the acive
ActiveCell.Offset(0, -1).Copy
Sheets("Sheet2").Select
Range("B5").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy 4th cell which is +1 cells to the left next to the acive
Call CalculateBoxes
'Call your macro
Sheets("Sheet2").Select
Range("B6").Select
ActiveCell.Copy
Sheets("Sheet1").Select
ActiveCell.PasteSpecial
ActiveCell.Offset(1, 0).Select 'selects next cell to be active so it won't make it lasts forever block on first cell
End If
Loop
End Sub
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
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.