Null rows is also read when reading data from excel sheet dynamically - excel

endofrow = excelWorksheet.get_Range("X3", "Y3").get_End(XlDirection.xlDown).Row.ToString();
//endofrow value must be 5 but it comes as 8 and null rows appear
this function must read only 2 rows as only two rows of data are present ,but it reads extra rows and null rows are appearing during insertion.

I use this code to return last row. hope this might help you
Sub GetLastRow()
Dim ws As Worksheet
Dim lRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lRow = 1
End If
End With
MsgBox "The last row in Sheet1 which has data is " & lRow
End Sub
you can change it according to your need.

Related

Selecting a range until the last used row

I am trying to select a range until the last used row in the sheet. I currently have the following:
Sub Select_Active_Down()
Dim lr As Long
lr = ActiveSheet.UsedRange.Rows.Count
If Cells(ActiveCell.Row, ActiveCell.Column) = Cells(lr, ActiveCell.Column) Then
MsgBox "There isn't any data to select."
Else
Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(lr, ActiveCell.Column)).Select
Cells(lr, ActiveCell.Column).Activate
End If
End Sub
The issue is that I need to select multiple columns, and this will only select the first column of the active range. How can I modify this to select multiple columns rather than just the first?
What about selection the entire region? This can be done as follows in VBA:
Selection.CurrentRegion.Select
There also is the possibility to select the entire array. For that, just press Ctrl+G, choose Special and see over there.
I would do this slightly different. I would use .Find to find the last row and the last column (using the same logic shown in the link) to construct my range rather than using Selection | Select | ActiveCell | UsedRange | ActiveSheet.
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim rng As Range
'~~> Change it to the relevant sheet
Set ws = Sheet1
With ws
'~~> Check if there is data
If Application.WorksheetFunction.CountA(.Cells) = 0 Then
MsgBox "No Data Found"
Exit Sub
End If
'~~> Find last row
LastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'~~> Find last column
LastColumn = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
'~~> Construct your range
Set rng = .Range(.Cells(1, 1), .Cells(LastRow, LastColumn))
'~~> Work with the range
With rng
MsgBox .Address
'
'~~> Do what you want with the range here
'
End With
End With
End Sub

Find LastColumn: 2 methods tried, each gives different column as last

This is my method #1. The correct result should yield Row 1 and Column 384. This method gives the correct result But it is highly inefficient with Activate/Select, etc.
Workbooks.Open Filename:=sDest, UpdateLinks:=0
Windows(sDestFile).Activate
Sheets(sDestTab).Select
'Find LastColumn
Dim colLast As Integer
With ActiveSheet
colLast = Cells(rowTop, Columns.Count).End(xlToLeft).Column
End With
Then I created Method #2. This method is less of an eyesore, but it consistently gives me Row 1 and Column 386 as the answer (instead of Column 384). I cannot for the life of me figure out why a change in code would shift my LastColumn by 2.
Workbooks.Open Filename:=sDest, UpdateLinks:=0
'Find Last Column
Dim colLast As Integer
colLast = Workbooks(sDestFile).Worksheets(sDestTab).Cells(rowTop, Columns.Count).End(xlToLeft).Column
Use .Find. Try this
Dim lastcol As Long
With Workbooks(sDestFile).Worksheets(sDestTab)
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastcol = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Else
lastcol = 1
End If
End With
MsgBox lastcol
If you want to find the last column in Row 1 then try this
Dim wb As Workbook
Dim colLast As Long
Set wb = Workbooks.Open(Filename:=sDest, UpdateLinks:=0)
With wb.Sheets(sDestTab)
colLast = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
when I want to find the last row or column containing the value:
Option Explicit
Public Function Row_Last(ws As Worksheet) As Long
On Error Resume Next
Row_Last = _
ws.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
If Row_Last = 0 Then Row_Last = 1
End Function
Public Function Col_Last(ws As Worksheet) As Long
On Error Resume Next
Col_Last = _
ws.Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
If Col_Last = 0 Then Col_Last = 1
End Function

Using Select Case with InStr for multiple named sheets

My first post here.....
I was able to search and find this code from Siddharth Rout which is the basis for what I want to do....append multiple sheets of data to a single data sheet. However I'm having trouble modifying it to fit my case. What I have below doesn't currently work......
Problem 1) How can I use Select Case InStr with multiple sheets (3) that do not have a common name such as "Legende" as the original poster had in her case.
Problem 2) In my case each sheet will have different columns that I need copied to the Tab_Appended sheet, Sheet1 will have x rows and I want column B, D, M, AR, etc and Sheet2 will have XXXX rows and I want to copy column B, D, N, AS, AT, etc for 15 sheets.
Credit to Siddharth Rout for this original code:
Sub SummurizeSheets()
Dim wsOutput As Worksheet
Dim ws As Worksheet
Dim wsOLr As Long, wsLr As Long
Application.ScreenUpdating = False
'~~> Set this to the sheet where the output will be dumped
Set wsOutput = Sheets("Tab_Appended")
With wsOutput
'~~> Get Last Row in "Tab_Appended" in Col A/M and Add 1 to it
wsOLr = .Range("A:M").Find(What:="*", After:=.Range("A1"), _
Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row + 1
'~~> Loop through sheet
For Each ws In Worksheets
'~~> Check if the sheet name has Legende
'Select Case InStr(1, ws.Name, "Legende", vbTextCompare)
Select Case InStr(1, ws.Name, "Test2", vbTextCompare) + _
InStr(1, strData, "Test", vbTextCompare) + _
InStr(1, strData, "Sheet2", vbTextCompare)
'~~> If not then
Case 0
With ws
'~~> Get Last Row in the sheet
wsLr = .Range("A:M").Find(What:="*", After:=.Range("A1"), _
Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
'~~> Copy the relevant range
.Range("A2:M" & wsLr).Copy wsOutput.Range("A" & wsOLr)
'~~> Get Last Row AGAIN in "Tab_Appended" in Col A/B and Add 1 to it
wsOLr = wsOutput.Range("A:M").Find(What:="*", After:=wsOutput.Range("A1"), _
Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row + 1
End With
End Select
Next
End With
Application.ScreenUpdating = True
End Sub
Thanks,
Don

Find last row of a specific range of columns

I have a table that ranges from "A:EV".
I want to find the last row of only range "A:DD".
The columns might have blank cells, so I need to go through all and find the furthest row of columns A to DD.
How can I code it?
Modified from HERE
Sub foo()
With Sheets("Sheet9") 'Change to your sheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastrow = .Range("A:DD").Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
'Used 1000 to prove that it was not defaulting to this.
'Change to 1 when using in actual code.
lastrow = 1000 'Change to 1 when using.
End If
MsgBox lastrow
End With
End Sub
And to adjust the range a bit, change the two column letters in these two lines.
This one searches over columns A:DD.
lastrow = .Range("A:DD").Find(What:="*", _
After:=.Range("A1"), _
This version narrows the search range to Y:DD
lastrow = .Range("Y:DD").Find(What:="*", _
After:=.Range("Y1"), _
And with a little voodoo, if you are interested in also getting the column where the last row was found but don't want to extract it from .Address, use this.
sub voodoo()
Dim theresult As Variant
With Sheets("Sheet9") 'Change to your sheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
Set theresult = .Range("Y:DD").Find(What:="*", _
After:=.Range("Y1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
Else
'Used 1000 to prove that it was not defaulting to this.
'Change to 1 when using in actual code.
lastrow = 1000 'Change to 1 when using.
End If
MsgBox ("LastRow " & theresult.Row & " column " & theresult.Column)
End With
End Sub

Delete all rows below the last row of data for import process from Access

Microsoft Excel 2010:
From month to month, the number of lines of data can be variable. When I paste new data into the ILS_IMPORT tab, there may be 3,500 records and the next month could be 2,500. When I go to import the data into Access, and extra 1,000 lines will appear unless I delete all records from line 2,501 on. I would like to have Excel VBA to do this and have made attempts, but nothing has worked thus far. I know that Column O will always have data to the end because it is the quarter indicator (ex. Q2).
However, this code keeps deleting the last row and I don't know if it is truly deleting all the way to the end. Can someone point me in the right direction?
Sub test()
Dim rng As Range
Dim lastRow As Long
With ThisWorkbook.Sheets("ILS_IMPORT")
'Find anything in the cells
Set rng = .Cells.Find(What:="*", _
After:=.Range("O1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
'if data is NOT found - exit from sub
If rng Is Nothing Then Exit Sub
'find last row
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastRow = .Cells.Find(What:="*", _
After:=.Range("O1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lastRow = 1
End If
'I use lastRow + 1 to prevent deletion data above when it is on lastrow
.Range(rng.Row + 2 & ":" & lastRow + 2).Delete Shift:=xlUp
End With
End Sub
Could you clear/delete the blank range before you paste data in?
range(cells(2,1),cells(2,1).end(xldown)).EntireRow.Clear

Resources