Efective method to find last row vba excel [duplicate] - vba

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 4 years ago.
i have the following method to set to define a ranga:
Set RegJan = Sheets("BD").Range("T3:T50000")
the range is smaller than this, but in order to make it work for some months, i set it to 50000 rows.
i then use this code in the following line of vba:
ws.Cells(8, 2).Value = Application.WorksheetFunction.SumIfs(RegJan,
EquipaBd, EquipaForm, AgenteBd, AgenteForm) 'Soma dos Registos
This last line i already transformed in a loop so that it calculates in every line i need it,the problem is that i have to do the first line of code for every month of the year an for 6 more variables
is there a better way of doing this? possibly a dynamic way?

This might give you some ideas:
With Sheets("BD")
' Since T is column #20 and goes to 6 more
For col = 20 To 26
' get last row
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
' set range
Set yourRng = .Range(.Cells(3, col), .Cells(lastRow, col))
' you might want to change 8,2 to make dynamic
ws.Cells(8, 2).Value = Application.WorksheetFunction.SumIfs(yourRng , EquipaBd, EquipaForm, AgenteBd, AgenteForm)
Next
End With

Related

Delete Blank Cells And Shift Up Fail [duplicate]

This question already has answers here:
Delete blank cells in range
(6 answers)
Closed 4 years ago.
I've searched and tried alooot of solutions regarding this issue. But non of them worked. So im trying to attach the excel file here.
My issue is:
Column A
1324
12312
14
4323
12
11234
I want B to look like:
Column B
1324
12312
14
4323
12
11234
Looks simple. But it doesn't work since the Blank cells doesn't actually appear to blank. And I cant find a way to get rid of them. I'm attaching the excel file for your reference.
Excel File:
https://drive.google.com/open?id=1PDskY1GJKYhzzj905KrX988F8tTaNQSs
I believe the following will achieve your desired result, simply copy the code under your command button.
This will loop through row 1 to Last on Column A and if the cell is not blank it will pass the value to the next free row on Column B:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set the worksheet you are working with, amend as required
LastRowA = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 1 To LastRowA
'loop from row 1 to Last
LastRowB = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1
'get the last row with data on Column B and offset by one (next empty row)
If ws.Cells(i, "A").Value <> "" Then ' if Column A's value is not empty
ws.Cells(LastRowB, "B").Value = ws.Cells(i, "A").Value 'pass that value to the next available row on Column B
End If
Next i
End Sub

How to get last column used in excel using vba [duplicate]

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 5 years ago.
Hi I am having issues getting to the last column used in my sheet.
I do know that as it stands my last column is 43 and i want it to enter a value at column 44, so then when i search again it will go to column 44 as last column used and then enter a value at 45 and so on and so on. I am using a ref num to find the correct row.
The code I am using is as follows to try get the last column
'find last empty Col in database
LastCol = sou.Cells(2, Columns.count).End(xlToLeft).Column + 1
MsgBox LastCol
'For Loop to cycle through the columns
For x = 2 To LastCol
'If the row value at column 1 = the ref number then continue
If sou.Cells(x, 1).Value = refNum Then
sou.Cells(x, LastCol).Value = Me.commentBox.Text
Exit For
End If
Next
The issue is that for some reason it only goes to column 22. I have tried to use the other ways to get the last column but they have just given me the last column in the whole entire sheet which again is not what I want.
If someone can lend me a hand as I'm a newbie to this it would be greatly appreciated!
To find the last row used, have you tried
lastRow = ActiveSheet.UsedRange.Rows.Count
If that didn't work, please elaborate what went wrong and I can submit more code.

Finding last used row in a table then adding data to the remaining rows below

I've been googling this one for a while now with no success - I have a table with around 850 rows and in columns 8 to 13 I need to find the last used row, and then add data to the remaining empty rows below (if any). I've found plenty of examples working with ranges but they all just find the last row in the table and as such don't work for me. I'm a beginner with VBA and I've tried working with the ListColumns object but to no avail, and as such have no current code to post. Can anyone provide some insight into this?
This will do it:
Sub Macro1()
Dim i, iColumn, Ending_Column As Long ' Declare you long data types
Ending_Column = 13 ' Assing the right-most column
i = Range("H" & Range("H" & Rows.Count).End(xlUp).Row) + 1 ' Find the next row column H (this lets you enter the data on the same line)
For iColumn = 8 To Ending_Column ' Asign left-most column and loop to the right-most one
Cells(i, iColumn).Value = "Your input" ' The "Your input" needs to be modified
Next ' next column
End Sub

Using a variable to define a range [duplicate]

This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Closed 7 years ago.
I am attempting to store the value of an 8 x 1 range into a range of identical dimensions, but on another sheet in the workbook. This would be easy except that my script is looping through different ranges of these same dimensions and I need to store them all on the second sheet. Currently my code looks like this:
Sheets("Sheet1").Range(Cells(i, 2), Cells(i + 7, 2)).Value = Sheets("Sheet2").Range("OriginalData").Value
Where "i" is the variable being used as the iterator in the loop.
This code throws an error "Error 1004 "Application-defined or Object-defined error"". Can someone explain what I'm doing wrong, and how to properly define range objects dynamically in this fashion?
Your problem is that the Cells inside the Sheets("Sheet1").Range don't know that they are supposed to belong to Sheets("Sheet1").
with Sheets("Sheet1")
.Range(.Cells(i, 2), .Cells(i + 7, 2)) = _
Sheets("Sheet2").Range("OriginalData").Value
end with
'alternate
Sheets("Sheet1").Range("B" & i).Resize(8, 1) = _
Sheets("Sheet2").Range("OriginalData").Value
The With ... End With statement allows you to definitively pass the parent worksheet into both .Range and .Cells with the prefix period (aka full stop).
Would a copy and paste work? I'm not sure of the specifics of your data but seems like a copy and paste would work better.
So like
Sheets("Sheet1").Range("x:x").Copy Sheets("Sheet2").Range("x:x")

detection of the first full cell in an excel sheet with vb.net [duplicate]

This question already has an answer here:
Find first non blank cell in column with vb.net
(1 answer)
Closed 7 years ago.
I want to find the first cell containing data.
I use this code but it doesn't work.
I get an error in 'selection'.
Range("A1:BN1").Select
y = Selection.Find("*", After:=Range(Selection(1, 1).Address), LookIn:=xlValues).Column
From the OP's comments:
I tyr [sic] this
With xlWorkSheet
lastrow = .Cells(.Rows.Count, 1).End(Excel.XlDirection.xlDown).Row
lastcol = .Cells(1, .Columns.Count).End(Excel.XlDirection.xlToRight).Column
End With
The first cell in Column A that contains data (i.e. is not blank) can be found with
Cells(1, 1).End(xlDown)
It appears that all you want is a number representing the column index with the first value in row 1. If you want the first, you have to look to the next one after the last. If you look for the next one after Selection(1, 1) then you are going to get the second if Selection(1, 1) is not blank.
Dim y As Long '◄ note: a long, not a range
With Range("A1:BN1")
y = .Find("*", After:=.Cells(.Rows.Count, .Columns.Count), LookIn:=xlValues).Column
End With
Debug.Print y
That is the (better) way to get the column index number without selection. If you feel that must keep using Selection then your code would be closer to the following.
Dim y As Long
Range("A1:BN1").Select
y = Selection.Find("*", After:=Selection(Selection.Rows.Count, Selection.Columns.Count), LookIn:=xlValues).Column
Debug.Print y
You were getting the selection's address and converting that back to a range object with Range(...) so I cut a couple of operations out of that process.
Addendum:
Your comments indicate that you want either the cell or the entire column with the first value in row 1. If that is the case, then this is more appropriate.
Dim y As Range '◄ note: now a range
With Range("A1:BN1")
Set y = .Find("*", After:=.Cells(.Rows.Count, .Columns.Count), LookIn:=xlValues) '◄ note no .Column at the end
End With
Debug.Print y.Address
Debug.Print y.EntireColumn.Address