I am new to Excel Scripts in Sharepoint but I am familiar (not an expert) with VBA. I need to find how I can find the last row in excel sharepoint similar to using
Dim L and Long
L = Range("A" & Rows.Count).End(xlUp).Row
This is to apply a formula to the cells. Currently I have
//Set range F1 on selectedSheet
selectedSheet.getRange("F1").setFormulaLocal("=max(F3:F1050)")
The final cell was recorded using CRTL+SHIFT+DOWN but in this column, there is the possibility of blank data in range F where as column A has complete data so would define the cell number.
I am hoping to get some such as
selectedSheet.getRange("F2).setFormulaLocal("=max(F3:F.....)")
Thanks in advance
Related
I am working on a macro to clean up exported data from one application to upload it into another. I'm new to vba, but not programming, relatively.
I was hoping to get this out of it:
Set the range equal to the last row of column B
Insert the Formula2 = "=B1&""-""&C1" into column AS, but only down to the range of column B's last cell
I found a post suggesting this to set the last row and use it for range, but i'm unsure how to tie it into the rest of my script
myLastRow = Cells(Rows.Count, "B").End(x1Up).Row
This code works, but the current range spits it out against the entireity of the AS column, and adds a "-" to everything after columns B and C are empty.
Worksheets("export-data").Range("AS:AS").Formula2 = "=B1&""-""&C1"
Worksheets("export-data").Range("AS:AS").Copy Worksheets("Acumatica").Range("G:G")
I'm hoping to just insert that Formula in a range of AS equal to populated cells in column B
I am trying to clear a data in a worksheet in excel using vba. I want to clear the cells with data in them, not including Row 1. I am trying to get the variable of the last row and column with data but I keep getting an out of range error.
Dim lRow As Long
Dim lCol As Long
lRow = Sheets("Sheet1").Cells(Sheets("Sheet1".Rows.Count,1).End(xlUp).Row
lCol = Sheets("Sheet1").Cells(1, Sheets("Sheet1").Columns.Count).End(xlToLeft).Column
I want to use this logic to replace what I have hard coded:
Sheets("Sheet1").Range("A2:D1000").ClearContents
I am using multiple sheets so that's why I am specifying Sheet1. How can I do this?
Thanks.
You were missing a Parenthesis in the last row line
you do not need to find the last column unless you are limiting the clear contents to preserve data.
One more thing, you are finding the last row in column A only, so if there is data in another column lower that the last one in column A, you won't clear that data.
lRow = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count,1).End(xlUp).Row
Sheets("Sheet1").Rows("2:" & lRow).ClearContents
Dim ws As Worksheet
set ws = Sheets ("Sheet1")
ws.Range(ws.Cells(2,1),ws.Cells(ws.UsedRange.Rows.Count,1).EntireRow.Delete
should do the trick. UsedRange tracks the smallest rectangle in the worksheet containing all cells with data and starting from A1.
I am very new to vba and basically only use it when amending a range value (having previously recorded the script using a macro in excel). Since I add/delete rows in excel I have to go to the vba script and manually update the range part. So if my range on the excel sheet has expanded from A2:R83 to A2.R84 heres one of the parts I update from:
Range("A2:R83").Select
To:
Range("A2:R84").Select
Is there a way I can specify a cell that vba can take the range values from? eg can I, on the excel sheet cell X1 input A2 and in cell Y2 input R84 and have the vba script reference these cells to determine the current range?
Appreciate any help!
I believe this will do what you want, :
Sub test()
Dim s1 As String, s2 As String
s1 = Sheet1.Range("A1"): s2 = Sheet1.Range("B1")
Range(s1 & ":" & s2).Select
End Sub
You will, however, run into trouble if the values in A1 and B1 are not valid cell-names, so some input validation may be a good idea.
I found that it is possible to make the validation range dinamic using INDIRECT.
1.- In location that you choose (in the example I use X1 in sheet1) put
="'Sheet1'!"&"A2:"&"R"&COUNTA(R1:R2000)
I put R2000 to have plenty space in case the range grows, change to a size that suits you. The result of this formula will be a range. Its size will change every time you put something new in R because of the Counta.
2.- In the validation section place this formula when you record it.
=INDIRECT('Sheet'!$X$1)
This makes the validation read the range based on what x1 says.
This will figure your range,
Sub SelectRng()
Dim Rws As Long, Rng As Range
Rws = Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Range(Cells(2, "A"), Cells(Rws, "R"))
Rng.Select 'or whatever you want to do with it.
End Sub
Sorry if this is a simple question but I cant find a specific answer anywhere. Tried CONCATENATE but that didnt work.
I have a list of excel rows which are all times in the format HH:MM:SS
For example A1 = 20:33:15
I have 10's of thousands of entries and would simply like an excel command that copies the HH to the second B column.
How would I do this?
get the last row by
Dim ws As Worksheet
Dim lastRow As Integer
lastRow = ws.Range("A" & Rows.Count).End(xlUp).row
and then loop on the particular Range and enter =Hour(Range(<row><col>))
I would like to populate columns in sheet2 from sheet1. If I have column A in Sheet1 I want A in Sheet2 to have the same information.
I tried using =sheet1!A1 but it only returns the value from A1 in sheet1. I tried using =sheet1!A but it only returns #NAME?.
If Column A from Sheet1 has a dynamic range (it can be empty or have 500 or 1000 rows (I'm populating sheet1 from my database)). How do I use some of those columns in another sheet showing all 500 or 1000 rows?
If I understood you right you want to have sheet1!A1 in sheet2!A1, sheet1!A2 in sheet2!A2,...right?
It might not be the best way but you may type the following
=IF(sheet1!A1<>"",sheet1!A1,"")
and drag it down to the maximum number of rows you expect.
I have used in Google Sheets
={sheetname!columnnamefrom:columnnameto}
Example:
={sheet1!A:A}
={sheet2!A4:A20}
Below code will look for last used row in sheet1 and copy the entire range from A1 upto last used row in column A to Sheet2 at exact same location.
Sub test()
Dim lastRow As Long
lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A1:A" & lastRow).Value = Sheets("Sheet1").Range("A1:A" & lastRow).Value
End Sub
In Google Sheets you can use =ArrayFormula(Sheet1!B2:B)on the first cell and it will populate all column contents not sure if that will work in excel
Use the 'EntireColumn' property, that's what it is there for. C# snippet, but should give you a good indication of how to do this:
string rangeQuery = "A1:A1";
Range range = workSheet.get_Range(rangeQuery, Type.Missing);
range = range.EntireColumn;