How to automatically choose the whole column data? For example theSUM,
When data is A1:A20, then the result is =SUM(A1:A20), when we change data into A1:A30, then the result automatically become =SUM(A1:A30), i.e choose the all effective data this column or A1:End.
Simply try using: =SUM(A:A)
It should ideally work.
Using VBA, you can use the method .End(xlDown) with .Select to select the last non-empty row in a column.
Range("A1").End(xlDown).Select
Or instead of .Select, you can use .Row to get the last non-empty row number.
Example code:
Sub SumData()
Dim LastRow As Long
LastRow = Range("A1").End(xlDown).Row
Cells(LastRow + 2, "A").Formula = "=SUM(A1:A" & LastRow & ")"
End Sub
This will work to find the last row used in the range:
MAX(IF(A:A<>"",ROW(A:A))
To incorporate that in the SUM formula:
=SUM(INDIRECT("A1:A"&MAX(IF(A:A<>"",ROW(A:A)))
Please note that this formula will need to be entered as an array (When in the formula bar hit Ctrl+Shift+Enter) and will slow down your spreadsheet if you have a lot of data in there
Related
IF(ISBLANK(B5),"",IF(ISBLANK(O5)=TRUE,"Missing PSD",TODAY()-O5))
This is my formula that calculates the difference between the date in column O and current date. My first filled row with values is 5. The row in which the formula calculation is being done is AC.
I want to automatically calculate this until the last filled row and the row values should also increment like it does while dragging down.
I am not good with VBA so any help would be highly appreciated.
Try the next code, please:
Sub testFilFormulaDown()
Dim sh As Worksheet, lastRow As Long
Set sh = ActiveSheet 'use here the necessary sheet
lastRow = sh.Range("O" & rows.count).End(xlUp).row 'chosen O:O column, being involved in the formula...
sh.Range("AC5:AC" & lastRow).Formula = "=IF(ISBLANK(B5),"""",IF(ISBLANK(O5)=TRUE,""Missing PSD"",TODAY()-O5))"
End Sub
In order to properly calculate the last row, you must choose a fully filled column (A:A, C:C etc.). I used one involved in the formula, but since there is a check for blank cells, column O:O could not be the most appropriate one...
Need a little help here.
In the "Data" Tab I want to copy values in column "c2:c1000" and paste in column "a1" of another Tab.
This is what i have so far,
Dim x As Long
Dim lastRow As Long
lastRow = Worksheet("Data").Cells(3, Columns.Count).End(xlUp).Column
For x = 1 To lastRow
If Worksheets("Sheet2").Cells(2, "A") = "" Then
Worksheets("Data").Range("c2:c1000").Copy Destination:=Worksheets("Sheet2").Range(1, "A")
Sheets("Sheet2").Range("A1").Value = Format(Now, "mm/dd/yyyy HH:mm:ss")
Else
Worksheets("Data").Range("c2:c1000").Copy Destination:=Worksheets("Sheet2").Cells(2,
Columns.Count).End(xlToLeft).Offset(, 1)
'Sheets("Sheet2").Range("A1").Value = Format(Now, "mm/dd/yyyy HH:mm:ss") --> can't figure how to increment this as this will need to be on the subsequent empty column
End If
Next
End Sub
Your help will be greatly appreciated!
Thank you.
Pasting values first into range A1 and down and then next time to cell B1 and so on, leaves no space for the timestamp to A1, B1 etc. So, I assume that you would like to paste the random values to row 2. So cells A1, B1, ... are left for the timestamp.
Inside the With statements we can refer to properties of the wsAudit so we can replace the "Worksheets("Audit")." reference with just "."
The column.count expression just checks the amount of columns in the worksheet.
The expression .Cells(2, Columns.Count) just points to last cell in the row 2.
The .End(xlToLeft).Column then looks from this column to left and is supposed to find the last not empty cell on this row. It's basically the same idea that in Excel's sheet you would go to cell XDF2 and hit CTRL+Arrow Left from keyboard.
But instead of activating the cell we just want to get the columns index number and then add 1 (the new column) and save it into variable. Now the new column is known.
The expression Range(.Cells(2, newColAudit), .Cells(1000, newColAudit)).Value is really the same as e.g. Range("B2:B1000"), but with this we can use the row and column index numbers instead. This is useful as the column number varies.
And as Samuel pointed out the copy paste operation can be avoided by setting the areas equal.
Dim wsAudit As Worksheet
Dim newColAudit As Long
Set wsAudit = Worksheets("Audit")
With wsAudit
newColAudit = .Cells(2, Columns.Count).End(xlToLeft).Column + 1
Range(.Cells(2, newColAudit), .Cells(1000, newColAudit)).Value = Worksheets("Data").Range("C2:C1000").Value
.Cells(1, newColAudit).Value = Format(Now, "mm/dd/yyyy HH:mm:ss")
End With
Much like your LastRow* variable for your source sheet, create a LastColumn variable for your destination sheet, which will find the last used column the same way you are finding your last used row.
Like so:
Dim LastColumn As Long
LastColumn = Sheets("Audit").Cells(1, Columns.Count).End(xlToLeft).Column
Then use the variable like so:
Destination:= Worksheets("Audit").Cells(1, LastColumn)
It seems that your code contradicts your question too, in your question you explained the data will be written to the Audit sheet in row 1, using the next column each time but your code looks for values in row 2 in your If statement:
If Worksheets("Audit").Cells(2, "A") = "" Then is the same as If Worksheets("Audit").Range("A2") = "" Then.
If you mean to check the first row, change the 2 to 1.
To help improve your codes efficiency:
(Also see the link to 'how to avoid select' in that question):
You can achieve 'copy/paste' without actually using the 'copy' and 'paste' methods by assigning the value of one range to the other, as example, like so:
Worksheets("Audit").Cells(1, LastColumn).Resize(999, 1) = Worksheets("Data").Range("c2:c1000").Value
Note: Change the Resize Property rows to suit the source range (in this case you are wanting to move values from C2:C1000).
*The LastRow variable is a bit confusing, as it is looking for the last used column in row 3.
If it's meant to find a column, consider renaming it to avoid confusion later on in debugging.
If it's meant to find the last row, try like this:
LastRow = Worksheet("Data").Cells(Rows.Count, 1).End(xlUp).Row
I need code for the below requirement, wherein the below code is not working:
Sub sample48()
Worksheets("Sheet").Select
Range("AG1").Select
Range("A1:AQ1").Autofilter Field:=33, Criteria1:="COD*"
Range("AG:AG").Value = Range("AM:AM").Value
End Sub
I need to filter a word called **COD** in column AG, and then I need to copy and paste values from Column AG to Column AM only for the those filtered line items.
Note that in excel you can copy from filtered area, but when you are pasting, data will not paste on filtered(visible) cells, instead pasted continuously. There is 2 way to do your task:
1: Use loop
Sub MyCopyPaste()
Dim RngCnt As Range
Dim LastRow As Long
LastRow = Range("AM" & Rows.Count).End(xlUp).Row
For Each RngCnt In Range("AM1:AM" & LastRow).SpecialCells(xlCellTypeVisible)
Range("AG" & RngCnt.Row).Value = Range("AM" & RngCnt.Row).Value
Next
End Sub
2: Without Loop
If in some case you don't want use loop or vba, you should create another column and put an ID number into it. This column will used to sort the worksheet to retain rows order to original. Then create another column with name custom_Order and after filtering, put numbers into it, then show all data and sort worksheet based on custom_order and then filter it. In this case all your expected rows will be filter and come continuously and you can easily copy and paste.
Sub sample48()
Range("A1:AQ1").AutoFilter Field:=33, Criteria1:="COD*"
Range("AG:AG").Copy
Range("AM:AM").PasteSpecial
End Sub
This does quite literaly what you asked:
filter your range
copy colmun AG (because it is filtered, only filtered values are copied)
paste filtered values in AM
you can not use an equation on a range for an entire range. Also, judging by your description, I assume your equation had to be the other way around.
I have a form I made for users to enter data. The data get's inserted into the next available row. That works well, but now I need the formula from the cell above in Column F to be copied into the new cell. Is there a code I could use this to work along with the copying of the input values to the sheet?
Any help with this would be greatly appreciated.
Edit: Method 2 is probably better suited for your purpose as it will adapt the cell references in the previous line to the next line.
Method 1: To set the formula of a cell, use
Sheets("sheet name").cells(row, column).Formula = ....
Row and column must be specified as integer values, I.e. For column F it would be 6. Assuming you have the number of the next free line in a variable called NextFreeLine:
Sheets("sheet name").cells(NextFreeLine, 6).Formula = Sheets("sheet name").cells(NextFreeLine-1, 6).Formula
Method 2: If you insist on literally copying the formula from somewhere else, try this (this will adapt the cell references of the formula for the previous line to the next one-- just like copy/pasting it using Ctrl-C, Ctrl-V):
Sheets("sheet name").cells(NextFreeLine-1, 6).Copy
Sheets("sheet name").cells(NextFreeLine, 6).PasteSpecial Paste:=xlPasteFormulas
The method you are using to put into 'the next available row' would have been nice to see but perhaps you can translate this for your own purposes.
'put the newval into the next available cell in column A
cells(rows.count, 1).end(xlup).offset(1, 0) = newval
'copy the formula in column F to the new row
cells(rows.count, 1).end(xlup).offset(-1, 5).resize(2, 1).filldown
Your value goes into the next available row based on column A. The same column is used in much the same manner but offset up a row and resized to two rows in height before executing the fill down command.
Hello from an unexperienced vba user.. I'm having trouble with the code for autofill to the last row when trying to use named ranges for the column. Everything seems to work fine when I use a hard coding for the column, in this case Column CW, and what I need is to replace this column CW with a named range so that the macro will still work when adding or deleting columns in the worksheet.
I used the following named ranges:
First_Date: This is the header cell of one of the columns (in this case AP5)
Second_Row: This is the range of columns I want to copy the formulas from (AP7:CW7)
Second_Cell: The cell where I want to start to autofill (AP7)
Last_Column: This is column CW that I want to use in the code. Autofill would be up to this column and down to the last row.
After searching in different threads, I came up with the following code that seems to work fine. How can I change column CW to a named range? Or do I need to change the code?
Dim Lr As Integer
Lr = Range("First_Date").End(xlDown).Row 'Searching last row
Rows(Lr).Insert Shift:=xlDown 'Inserting new row
Range("Second_Row").AutoFill Destination:=Range(Range("Second_Cell"), Range("CW" & Lr))
Can anyone assist me here please?
This will get the job done :-)
Sub RangerFiller()
'The Cell that holds the formula B1
OriginalFormula = Cells(1, 2).Formula
'copies formula down to the last column next to B but use can use another column as
'a counting column....the column that hold the last value
Range("B2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = OriginalFormula
End Sub
Someone gave me the solution:
Change
Range("CW" & Lr)
To
Cells(Lr, Range("Last_Column").Column)
I faced a similar problem because I don't want to hard code the cell reference. I found this solution below to be useful, by using "& ______ &" to replace the cell number that can be calculated using input box or formula.
Eg.
cell1 = last row of column A
Range("CW " & cell1 &" :CW & Lr),
where cell1 = any number that can be added via input box/formula.
Hope this helps!