Sub testing()
'start searching for address
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
IncNum = Sheets("Sheet2").UsedRange.Columns.Count
ExcNum = Sheets("Sheet1").UsedRange.Columns.Count
InrNum = Sheets("Sheet2").UsedRange.Rows.Count
Exrnum = Sheets("Sheet1").UsedRange.Rows.Count
Set sheet1Table = Sheets("Sheet1").UsedRange
Set sheet2Table = Sheets("Sheet2").UsedRange
'skip header
For InrCounter = 2 To InrNum
For ExrCounter = 2 To Exrnum
If sheet1Table.Cells(InrCounter, 1) = sheet2Table.Cells(ExrCounter, 1) And sheet1Table.Cells(InrCounter, 2) = sheet2Table.Cells(ExrCounter, 2) And sheet1Table.Cells(InrCounter, 3) = sheet2Table.Cells(ExrCounter, 3) Then
If IncNum = ExcNum Then
Exit For
Else
Dim LastCofRowCounter, lastCofthisR As Integer
lastCofthisR = sheet1Table.Cells(ExrCounter, Columns.Count).End(xlToLeft).Column
For LastCofRowCounter = lastCofthisR + 1 To IncNum
Sheets("Sheet1").Cells(ExrCounter, LastCofRowCounter) = Sheets("Sheet2").Cells(InrCounter, LastCofRowCounter)
Next LastCofRowCounter
End If
ElseIf ExrCounter = Exrnum Then
'fid the last row of mastersheet
lrowEx = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For counterCofLastR = 1 To IncNum
Sheets("Sheet1").Cells(lrowEx + 1, counterCofLastR) = Sheets("Sheet2").UsedRange.Columns(1).Cells(InrCounter, counterCofLastR)
Next counterCofLastR
End If
Next ExrCounter
Next InrCounter
End Sub
the table looks like
h1 h2 h3 h4
x x x x
ExcNum = Sheets("Sheet1").UsedRange.Columns.Count 行给了我 9 而不是 4,我不知道为什么在这种情况下...我尝试了另一个具有同一张表的工作表,它工作正常。
我试着打电话
Sub ResetUsedRng()
Application.ActiveSheet.UsedRange
End Sub
在执行所有代码之前,但这不起作用。我还阅读了你们链接的相关帖子,其中包括
Private Sub Workbook_BeforeSave _
(ByVal SaveAsUI As Boolean, Cancel As Boolean)
For Each Sh In ThisWorkbook.Worksheets
x = Sh.UsedRange.Rows.Count
Next Sh
End Sub
我调用了 Call Workbook_BeforeSave(True, False),它也不起作用。想法?