Convert range to array - excel

I have the following worksheet:
I want to convert the range from C15 to last row/column to array.
I have tried the following code but is not working:
Sub rangeToArray()
Dim arr() As Variant
arr = Range("C15", Range("C15").End(xlDown).End(xlToRight))
End Sub
I get this:
Could someone help me please with this? I would like to get the range from C15 to last row/column and based on different criteria to sort it and copy rows to a different spreadsheet with the same format. I want to convert the range into an array as I have over 30k rows and will work faster.
Thank you!

arr = Range("C15", Range("C15").End(xlDown).End(xlToRight)) is just another way of saying arr = Range("C15").CurrentRegion
On top of that this would currently refer to the ActiveSheet, therefor you might want to try the following:
Sub rangeToArray()
Dim arr() As Variant
With Sheet1 'Change to whichever CodeName your sheet has
arr = .Range("C15").CurrentRegion
End With
End Sub
Note: As said in my comment, CurrentRegion will not work correctly once you start having gaps in your data. Therefor you might want to rework the code to check for the last used row in column C:C and the last used column in row 15:
Sub rangeToArray()
Dim arr() As Variant
Dim lr As Long, lc As Long
With Sheet1 'Change to whichever CodeName your sheet has
lr = .Cells(.Rows.Count, 3).End(xlUp).Row
lc = .Cells(15, .Columns.Count).End(xlToLeft).Column
arr = .Range(.Cells(15, 3), .Cells(lr, lc))
End With
End Sub

Based on this answer with the most reliable way to find the last row and column, the following range is possibly the most reliable way to select all your data to last row and column:
Arr = Range(Cells(15, 3), Cells(Range("A" & Rows.Count).End(xlUp).Row, _
Cells(1, Columns.Count).End(xlToLeft).Column))
Please note, it would be best to specify the sheet for every Cell and Range statement.

Related

How can i show the data in specific cell?

Hi i want to show an output to a specific cell at B column but i really don't have any idea on how to make it to show. Example: if "A2" has the record i want "B2" to show the output. If "A100" has the record, i want "B100" to show the output
Sub Testing()
Dim cell As Range
For Each cell In Range("A2:A4")
If cell.Value = "yes.com" Then
Range("B2:B4").Value = "Correct"
End If
Next
End Sub
The code above shows the output data "Correct" from "B2" to "B4" but what i want it to show on only the specific cell. Please Help
Currently you are looping through a range object. Per cell. One a small dataset this is fine but in your current attempt you'll need to change:
Range("B2:B4").Value = "Correct" for cell.Offset(0,1).value = "Correct"
As per my comment, you can do this a bit smarter/faster. Looping through worksheet cells is slow, certainly on large datasets (a 1000 rows is not that many yet to be honest). Nonetheless it's good to know that a good practice is to go through arrays. Let me show you below:
Sub Testing()
Dim lr As Long, x As Long
Dim arr As Variant
With Sheet1 'Change accordingly
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
arr = .Range("A2:B" & lr)
For x = LBound(arr) To UBound(arr)
If arr(x, 1) = "yes.com" Then
arr(x, 2) = "correct"
End If
Next x
.Range("A2:B" & lr).Value = arr
End With
End Sub
So you can see a few things that will be helpfull:
A reference to a sheet (through a CodeName to refer to a range's parent. Without it, the macro will simply reference the ActiveSheet which is for obvious reasons not always the correct one.
I have made use of a dynamic sized array. The lr variable will get the last used row in column A, so you don't have to work through full qualified references no more.
The arr variable is an array which takes the values from the specified range into memory. Running through data in memory is much quicker than a loop/iteration over worksheet cells. This will become much more noticable when you would have even larger datasets.
I wrote the array back to the range in one go instead of several writings.
Hopefully that helped =)
As previously mentioned a 1000 rows is still not that much. Allthough I suggest you stick with the Array approach, you can also Evaluate column A and fill column B accordingly in one go instead of stepping through a range object. It's an array formula in disguise so not very quick on actual large datasets.
Sub Testing()
Dim lr As Long
Dim rng As Range
With Sheet1 'Change accordingly
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rng = .Range("A2:B" & lr)
rng.Columns(2).Value = .Evaluate("IF(" & rng.Columns(1).Address & "=""yes.com"",""correct"","""")")
End With
End Sub
Just try:
Sub test()
Dim i As Long
For i = 1 To Rows.Count
If Cells(i, 1) = "yes.com" Then Cells(i, 2) = "Correct"
Next
End Sub
It will loop through entire A column.
Alternatively, you can enter in B1 formula:
=IF(A1="yes.com","Correct","")
and drag it all the way down.

Simple: Subtracting two columns in Excel VBA and filling down to the end of sheet in specific column

This is quite simple, I am aware, but something is going wrong for me. I simply want to subtract the values I have in column B from the values I have in column C and place these results in column Q.
I have assigned my strFormula(1) as a variant and then applied the equation to the strFormula(1). I have altered the following code from #Manhattan here on Stack Overflow :)
Sub FormulasNoLoops()
Dim strFormulas(1) As Variant
With ThisWorkbook.Sheets("Sheet1")
strFormulas(1) = "=(C2-B2)"
.Range("Q2:Q130").Formula = strFormulas
.Range("Q2:Q130").FillDown
End With
End Sub
There is no error when I run the script but also no result in column Q.
Ideally, I do not even want to enter the last cell of the column but maybe use .End(xlUp) somewhere.
Thanks all!
first
Dim strFormulas(1) As Variant
is creating an array with two items, 0,1
For one formula I would avoid the variable totally.
But if you want to use it just make it a string without the (1)
Dim strFormulas As String
Then load it:
strFormulas = "=(C2-B2)"
Also when you apply the formula to the whole range there is no need to fill down:
Sub FormulasNoLoops()
With ThisWorkbook.Sheets("Sheet1")
.Range("Q2:Q130").Formula = "=(C2-B2)"
End With
End Sub
Sub test()
Dim lastrow As Long
Dim rng As Range
last_row = ThisWorkbook.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("C2:C" & last_row)
rng.Formula = "=B2-A2"
End Sub

Can't figure it out; How to sort data into specific rows in excel

Appreciate all the help I can:
So I have got hundreds upon hundreds of rows of data (more being addeded daily)
Basically, I'm getting data that ranges from 1-80, but I want to be able to place that data into;
For example, I want 7 to be placed in 7, 10 to be placed in 10, 65 to be placed in row 65.
I can do this manually, but there has to be a code, forumla, script or something that would make it instant right?
I appreciate all the help or any advice that you can give!
Try this
Sub Demo()
Dim ws As Worksheet
Dim lastCol As Long, lastRow As Long
Dim arr, item
Dim rng As Range
Set ws = ThisWorkbook.Sheets("Sheet3") 'change Sheet3 to your data sheet
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'get last row with data in Column1
lastCol = .Cells(lastRow, .Columns.Count).End(xlToLeft).Column 'get last column with data in last row
Set rng = .Range(.Cells(lastRow, 1), .Cells(lastRow, lastCol)) 'set range for last row
arr = rng.Value 'put range values in an array
rng.ClearContents 'clear last row
For Each item In arr 'loop through each item in array
.Cells(lastRow, item).Value = item 'write item values into cell
Next item
End With
End Sub
Output :
This code will work for the last row with data in Column A.
If you want to place each random number into it's corresponding column, please use formula below. =IF(IFERROR(MATCH(CF$1,$A2:$CB2,0),"")="","",CF$1). Enter this formula in cell CF2 and drag below whole table. Enter all your random raw values in columns A:CB(80 columns in total). You should get this result (please note that I've hidden couple of columns):

select all rows for column except header in a seperate worksheet

I've tried various ways and answers to select all of the rows except the header for a certain column and none seem to work.
I've tried using (15 is the column here):
Range(Cells(2, 15), Cells(.Cells(rows.Count, 15).End(xlUp).Row, 15)).Select
I managed to use .Activate on the worksheet with a different statement to select all, but this changes the sheet and you could visibly see all the rows being selected. This isn't possible for what I need it for. Users can't have a bunch of sheets constantly being switched in front of them, makes for a bad experience.
How can I select all of the non-blank columns after the header (first) row without using .Activate?
I need to get these values, put them in an array, and check if the current cell value is in the array. Not asking for this part, but providing it as context if it matters.
You can not select a range on a non-active worksheet.
Here is how you can set a reference to all the cells in a column except the header row.
Dim TargetRange As Range
With Worksheets("Sheet1")
Set TargetRange = .Range(.Cells(2, 15), .Cells(Rows.Count, 15).End(xlUp))
End With
The following code reads the data from the Worksheet (without using Select or Activate), and puts it in a 2-dimensional array.
Option Explicit
Sub Range_WO_Headers()
Dim Sht_Source As Worksheet
Dim Rng As Range
Dim LastRow As Long
Dim LastCol As Long
Dim Rng_Array As Variant
' modify Sheet1 according to your sheet name
Set Sht_Source = ActiveWorkbook.Worksheets("Sheet1")
' assuming the table's data starts from Cell A1
LastRow = Sht_Source.Cells(Sht_Source.Rows.Count, "A").End(xlUp).Row
LastCol = Sht_Source.Cells(1, Sht_Source.Columns.Count).End(xlToLeft).Column
' resize array according to number of columns and number of rows
ReDim Rng_Array(0 To LastRow, 0 To LastCol)
' set dynamic array from Cell A1 to last row and last column found (starting the second row)
Set Rng = Sht_Source.Range(Cells(2, 1), Cells(LastRow, LastCol))
Rng_Array = Application.Transpose(Rng)
End Sub

Find the last cell address using vba excel

I have searched this site and it seems like all the answers just point to finding the row number of the cell.
I am trying to set a range so that it will go from A1 to the end of the data in the A column. This spreadsheet will be updated weekly so there will be data added to it every week.
I was wondering what code would work so that I can either find the row number and somehow tie it in with my range code so that it will equal "A" + lastrownumber? OR if there is code that will provide the column and row number together? If I have missed the link to the correct answer a simple link will do as well and I apologize for the post and your time.
Here is my code:
Sub NamedRange()
Dim Rng1 As Range
Dim newDate As Integer
Dim NumberOfRows As Range
Dim MyRange As Range
Dim lastRow2 As Range
lastRow2 = Range("A65536").End(xlUp).Row
'lastRow2 = LastRow
Set Rng1 = Sheets("Sheet1").Range(lastRow2)
ActiveWorkbook.Names.Add Name:="MyRange", RefersTo:=Rng1
Dim date1 As String
Dim dat As Date
Dim newPrice As Double
Set RgSales = Range("MyRange")
This will return the range object corresponding to the last filled in cell in column A
Range("A:A").Find("*",Range("A1"),SearchDirection:=xlprevious)
If you want the row number, use the following:
Range("A:A").Find("*",Range("A1"),SearchDirection:=xlprevious).row
This will give the last row in a given column
= Cells(Activesheet.Rows.Count, ColumnNumber).End(xlUp).Row (Fixed per #Gimp)
you then have a reference you can use to add to the data - e.g if you want to look in column "A", then that would be columnnumber 1. feed that into the function, then you can use Cells(NumberReturnedFromFunction,ColumnNumber) to address that cell, and add .Address if you want the A1 style of address
Try using something like this:
Activesheet.Cells(Activesheet.Rows.Count, "A").End(xlUp).Row
You can replace Activesheet with references to a sheet's index # like Sheets(1) or the sheet name like Sheets("Sheet1")
By using the Rows.Count it will check to see what the max rows are and be compatible across all versions of Excel.
In the end you can use this within your range reference like this:
Msgbox Sheets(1).Range("A" & Sheets(1).Cells(Sheets(1).Rows.Count, "A").End(xlUp).row).value
But I'd probably rewrite that as
With Sheets(1)
Msgbox .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).row).value
End With
In case there are gaps in the data I'd avoid using xlDown so something like the following is fine. Try it in the immediate window:
Activesheet.range("A1:A" & Activesheet.Cells(Excel.Rows.Count, 1).End(Excel.xlUp).Row).select

Resources