How to shift an excel UsedRange to begin at cell A1 its worksheet (i.e. delete all blank rows and columns outside UsedRange)? - vba

I am new to VBA and have not yet been able to figure out how to select and delete the "non-UsedRange" from a sheet at once (i.e. shift the UsedRange to begin at cell A1).
In other words, I need a dynamic solution to move a UsedRange of variable size from its variable location to the very top left of its worksheet (i.e. all empty columns/rows on the left/top side of the UsededRange should disapear).
I have (probably) found a one-liner to do this (only) row-wise for (only) one specific column:
ActiveSheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
How to shift an excel UsedRange to begin at cell A1 its worksheet (i.e. delete all blank rows and columns outside UsedRange)?

Just keep deleting row 1 and column A while they are blank.
with worksheets("sheet1")
do while not cbool(application.counta(.rows(1).cells))
.rows(1).entirerow.delete
loop
do while not cbool(application.counta(.columns(1).cells))
.columns(1).entirecolumn.delete
loop
end with

What about Cut >> Paste:
ActiveSheet.UsedRange.Cut
ActiveSheet.Paste Range("A1")
or, as #JohnyL suggested in comment, use short version:
ActiveSheet.UsedRange.Cut Range("A1")

Related

excel: correct column position

Actually, it is a simple question, although I don't know if it's possibl to do what I wanted to.
I'm just copying a column from another using this sub:
Sub copy_column()
Sheets("FROM").Columns("A").Copy Destination:=Sheets("TO").Columns("A")
End Sub
In fact, in my sheet "FROM" my first row with data is the row 3 and then when I copy to "TO" sheet it's starting from row 3 too. My idea was to place it at the row 1 at "TO" sheet.
Is there any way to write something like Columns("A"-2) to put it in the right place?
An alternative which avoids deleting rows (if you have data you want to keep in other columns for example) is to take an intersection of a range overlaid with itself, but offset...
The following code assumes you have some data in row 1 in the sheet so UsedRange would start from the correct row, but you can change the offset to accommodate missing data in the rows above
Dim CopyRange As Range
With Sheets("From")
Set CopyRange = Application.Intersect(.UsedRange, .Columns("A"))
Set CopyRange = Application.Intersect(CopyRange, CopyRange.Offset(2, 0))
CopyRange.Copy Destination:=Sheets("TO").Cells(1, 1)
End With

Excel : Copy and paste multiple rows and columns from one worksheet to another

i would like to copy multiple rows from one worksheet to another , i have data starting in one worksheet at A2 row and ends at A108850 and it starts at A2 column and ends at I2 column , how can i copy all that data into another worksheet where row starts with A4 and column starts with A4 and ends with I4.
How could i possibly do it through some macro?.
Thanks.
Try this
Worksheets("Sheet1").Range("A2:I108850").Copy Worksheets("Sheet2").Range("A4")
Change the range reference and worksheet's name accordingly.
Copy&Paste is an "expensive" operation, the greater the range the more expensive the operation
Should you be interested in values only, you could try this:
Worksheets("DestinationSheetName").Range("A2:I108850").Value = Worksheets("SourceSheetName").Range("A2:I108850").Value
edited after OP's comment
Should the code in your last comment have the same aim of pasting values only, then change the second statement into the following:
With Worksheets("Sheet1").Range("A2:I108850") '<--| reference the "source" range you want to paste values from
.Range("A4").Resize(.Rows.Count, .Columns.Count).Value = .Value '<--| resize "destination" range accordingly to "source" one and paste values into it
End With
of course you must check for sheet names to be valid ones for the currently active workbook

Variable Named Ranges in Excel

I have a table in Excel formatted as follows:
Date Asset Return
1/3/2005 0.003582399
1/4/2005 -0.01908258
1/5/2005 0.002080625
1/6/2005 0.005699497
1/7/2005 -0.008040505
1/10/2005 -0.00339116
1/11/2005 -0.009715187
1/12/2005 0.002371855
1/13/2005 -0.00580783
1/14/2005 0.001058481
1/18/2005 0.015483842
1/19/2005 -0.014690715
1/20/2005 -0.015714799
1/21/2005 -0.010796326
I need a named range to reference each column. The workbook is a template, so the named range won't always cover the same number of rows depending on the data. I want to set it so that the named range "Date" and the named range "Asset Return" are automatically sized to cover the entire column from the first value until the last, without going past the last value in the column.
It will always start at cell B8, but might end at a different row depending on the size of the data.
How can I set a dynamic named range to accomplish this?
This named range formula will do it:
=Sheet1!$B$8:INDEX(Sheet1!$B:$B,COUNTA(Sheet1!$B:$B)+8)
Remember to add the sheet name as the named range will operate on the active sheet otherwise.
The formula starts takes B8 as it's starting point: Sheet1!$B$8
It then counts how many cells are not blank in column B: COUNTA(Sheet1!$B:$B)
It adds 8 to the count (assuming your first rows are blank).
It then uses INDEX and the COUNTA to reference the last cell.
https://support.office.com/en-gb/article/INDEX-function-a5dcf0dd-996d-40a4-a822-b56b061328bd
https://support.office.com/en-gb/article/COUNTA-function-7dc98875-d5c1-46f1-9a82-53f3219e2509
Try this VBA code
Sub test()
application.DisplayAlerts = false
Range("B8").currentregion.createnames _
top:true, right:=false, left:=false, bottom:=false
application.DisplayAlerts = true
end sub

Highlight a cell if its copied to another sheet

I have a formula that will copy values from one column on Sheet B to another column on another Sheet A. What I'd like to do is highlight the cells that were copied on Sheet B and highlight the cells in Sheet A that are not on Sheet B, essentially the inverse of the first part. On Sheet B only columns G and H would be highlighted but Sheet A could be from column A to H.
=IFERROR(INDEX(Sheet2!G$3:G$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)),G3)
You could set up conditional formatting with the same logic to change the colour or either the cell that you are setting or the one that you are copying from.
Beware: this kind of code can make your spreadsheet very slow if over used.
So in one range (sheet 2) , you would have conditional formatting set up to highlight the cell if
this match failed
MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)
In the other range (sheet 1 a:h)
you would highlight if ISERROR(INDEX(Sheet2!G$3:G$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)))
has picked the value from sheet2
I may have incorrectly butchered your code, but you should get the idea. Where you currently have a range of values, select the range, insert the conditional formatting, but edit the range to only check the first cell, it will automatically increment for you (if you remove the appropriate $ signs)
conditional formatting intro

VBA Code for Conditional Copying of Columns Not Adjacent to each other

Project Master
In MS Excel using VBA, I would like some help on conditional copying between worksheets within the same workbook. As per the attached image, I have a master list of projects on the worksheet "Master". For all the projects that have a "yes" in column I (Defect), I would like to copy the values in columns A (Works Package Issue Date), B (Project No.), E (City) and H (Contract Value) to another worksheet "Defects", within the same workbook.
Can you please provide a coding which could:
a) collapse all the rows so there is no blank rows in "Defects" worksheet; and
b) leave all the rows so if the "Defect" column has a "No", the relevant row from the "Master" worksheet is copied as a blank row in the "Defect" worksheet,
if possible.
Please help me with the coding - I have very basic knowledge of macros, and in a process of learning how to code.
Thanks & Regards, CK
Sub CopyValues()
'Declare variables
'Declare sheet variables
Dim Masterws as Worksheet
Dim Defectws as worksheet
'Declare counter variables
Dim I as Integer
Dim n as Integer
'Set value of sheet variables
Set Masterws=ThisWorkbook.Sheets("Master")
Set Defectws=ThisWorkbook.Sheets("Defects")
'Set value of counter to track first available row on Defects sheet
n=1
'Start a For loop to check each row on Master sheet, starting with row 2
For I = 2 to WorksheetFunction.CountA(Masterws.Columns.EntireColumn(1))
'If the cells in row I, column I have a value of, "Yes," then execute some code. If not, continue on.
If Cells(I, "I").value= "Yes" Then
'Set the value of cells in row n of the Defects sheet to the corresponding values of row I in the Master sheet. If n is replaced with I, then the value of cells in row I on Defects will be set to the values of Row I on Master, leaving blank rows where no, "Yes," was found because no copying took place.
Defectws.Cells(n,"A").Value=Masterws.cells(I,"A")
Defectws.Cells(n,"B").Value=Masterws.cells(I,"B")
Defectws.Cells(n,"C").Value=Masterws.cells(I,"E")
Defectws.Cells(n,"D").Value=Masterws.cells(I,"H")
'Add 1 to the n counter. The next time a row is found in the Master sheet with, "Yes," it will be written to the next available row down on the Defects sheet.
n=n+1
End If
'End of the For loop. Move on to the next row on Master sheet
Next
End Sub