我试图定义一个lastrow代码,该代码将返回excel表中最后一个非空单元格的最后一行值。 (以表格形式表示)
我的exceltable在COL A中的值从1到1005,COL B从1到414,在COL C中从414到1005.
我想要的是有一个返回414的lastrow代码和一个返回1005的一个。我得到的问题是因为它在一个表中。我的代码
lastrow3 = ThisWorkbook.Worksheets("DataÖnskemål").Range("A" & Rows.Count).End(xlUp).Row
lastrow2 = ThisWorkbook.Worksheets("DataÖnskemål").Range("B" & Rows.Count).End(xlUp).Row
两者都返回1005.我可以用我的桌子解决这个问题,还是某种格式问题?
提前致以最诚挚的问候和谢意 / d
答案 0 :(得分:4)
如果工作表上的Excel表格下面有数据,您将遇到问题。在查找excel表中的最后一行时,最好引用表列。
import { Component, Input } from "@angular/core";
import { ComponentFixture, async, TestBed, tick, fakeAsync } from
"@angular/core/testing";
import { FormsModule } from "@angular/forms";
import { By } from "@angular/platform-browser";
@Component({
selector: 'test',
template: `
<input type="text" [(ngModel)]="myValue" />
<div>{{ myValue }}</div>
`
})
class TestComponent {
@Input() myValue: string
}
describe('Example', () => {
let component: TestComponent
let fixture: ComponentFixture<TestComponent>
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
providers: [],
imports: [FormsModule]
})
.compileComponents()
}))
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent)
component = fixture.componentInstance
fixture.detectChanges()
})
it('should test two-way binding by setting the component member',
fakeAsync(() => {
const testValue = 'Component member test'
component.myValue = testValue // Should be the correct way to
test ngModel
tick();
fixture.detectChanges();
// Assertion error: Expected '' to equal 'Component member test'
// Why wasn't the value set in the textbox?
expect(fixture.debugElement.query(By.css('input'))
.nativeElement.value).toEqual(testValue)
//Yeah, the bananas are working. We have out-binding
expect(fixture.debugElement
.query(By.css('div'))
.nativeElement
.textContent
).toEqual(testValue);
}))
it('should test two-way binding by setting value directly on the native
element. But that just tests the out-binding', fakeAsync(() => {
const testValue = 'NativeElement test'
let element =
fixture.debugElement.query(By.css('input')).nativeElement;
element.value = testValue // this tests only the out-binding
element.dispatchEvent(new Event('input'));
tick();
fixture.detectChanges();
//of course this is Ok, we just set it directly
expect(fixture.debugElement.query(By.css('input'))
.nativeElement.value).toEqual(testValue)
//Yeah, the bananas are working. We have out-binding
expect(fixture.debugElement
.query(By.css('div'))
.nativeElement
.textContent
).toEqual(testValue);
}))
})
答案 1 :(得分:1)
尝试以下代码从表(ListObject
)获取A列和B列的最后一行:
Option Explicit
Sub LastRowTable()
Dim Tbl As ListObject
Dim LastRow2 As Long, LastRow3 As Long
' modify "Table1" to your table's Name
Set Tbl = ThisWorkbook.Worksheets("DataÖnskemål").ListObjects("Table1")
LastRow3 = Tbl.ListColumns(1).Range.Rows.Count '<-- last row in Column A in your Table
LastRow2 = Tbl.ListColumns(2).Range(LastRow3, 1).End(xlUp).Row '<-- last row with data in Column B in your Table
End Sub
答案 2 :(得分:1)
查找表的最后几行有点繁琐,特别是因为您经常需要满足用户过滤数据的情况。具有多个检查的循环可能更适合您,因为您可以根据自己对表中数据的需要进行调整。
您也没有提到您是否可以确定最后一行确实是一张表。
鉴于这些观点,也许.Find
函数适合你,因为无论是否在表中都会找到任何非空单元格,无论是否隐藏(尽管它不能应对已过滤表)。 (说“任何非空单元格”并不完全正确,因为例如,空字符串不会被拾取,但是这些异常可能不会给您带来麻烦)。无论如何你的代码可能是:
With Sheet1
lastRow1 = .Columns(1).Find(What:="*", _
After:=.Columns(1).Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
lastRow2 = .Columns(2).Find(What:="*", _
After:=.Columns(2).Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End With
答案 3 :(得分:1)
我一直在寻找类似的答案,最终想出了一个替代解决方案。我发现它在测试用例中很有效。试试这个案例:
Dim loTable As ListObject
Dim lngRowLast1 As Long
Dim lngRowLast2 As Long
'Set reference to your specific table
Set loTable = ThisWorkbook.Sheets(1).ListObjects("Table1")
With loTable
'I use the following as a catch in case the table is empty
If .DataBodyRange Is Nothing Then
.ListRows.Add
lngRowLast1 = 1
lngRowLast2 = 1
Else
lngRowLast1 = .DataBodyRange.Cells(.ListRows.Count, 1).End(xlUp).Row
lngRowLast2 = .DataBodyRange.Cells(.ListRows.Count, 2).End(xlUp).Row
End If
End With
我认为正在发生的是工作表中的 End 命令将始终停在表格正文的最后一行,无论该行是否有内容。相反,通过在 DataBodyRange 中使用 End(xlUp) 命令,它的行为就像您期望找到最后使用的行一样。
我还将您的引用从 Range 交换到 Cell,因为我发现为列引用传递变量更容易。
答案 4 :(得分:0)
试试这个
Dim DataRange As Range
Set DataRange = Range("A1:M" & LastRow)