简体   繁体   English

在Excel中的单独工作表上找不到最后一行

[英]Can not find last row on a separate worksheet in excel

I am currently working on a macro and I had it working 100% like I wanted, but when I went to move the control group to a different sheet, I've started getting all sorts of problems. 我目前正在处理一个宏,并且可以按照我的要求100%地运行它,但是当我将控制组移到另一个工作表时,我开始遇到各种各样的问题。 Here is the code: 这是代码:

Sub Duplicate_Count()

'Diclare Variables
Dim LastRow As Long
Dim value1 As String
Dim value2 As String
Dim counter As Long
Dim startRange As Long
Dim endRange As Long
Dim inColumn As String
Dim outColumn As String
Dim color As Long
counter = 0
Dim sht As Worksheet
Dim controlSht As Worksheet
Set sht = Worksheets("Sheet1")
Set controlSht = Worksheets("Duplicate Check")

'Find the last used row in column L
LastRow = sht.Cells(Rows.Count, "L").End(xlUp).Row

'set default ranges
startRange = 2
endRange = LastRow - 1
inColumn = "L"
outColumn = "N"

'check for user inputs
If controlSht.Cells(8, "B") <> "" Then
    startRange = controlSht.Cells(8, "B")
End If
If controlSht.Cells(8, "C") <> "" Then
    endRange = controlSht.Cells(8, "C")
End If
If controlSht.Cells(11, "C") <> "" Then
    Column = controlSht.Cells(11, "C")
End If
If controlSht.Cells(14, "C") <> "" Then
    Column = controlSht.Cells(14, "C")
End If
color = controlSht.Cells(17, "C").Interior.color

'Search down row for duplicates
Dim i As Long

For i = startRange To endRange

    'Sets value1 and value2 to be compared
    value1 = sht.Cells(i, inColumn).Value
    value2 = sht.Cells(i + 1, inColumn).Value

    'If values are not diferent then counter will not increment
    counter = 1

    Do While value1 = value2
        sht.Cells(i, inColumn).Interior.color = color
        sht.Cells(i + counter, inColumn).Interior.color = color
        counter = counter + 1
        value2 = sht.Cells(i + counter, inColumn).Value
    Loop

    'Ouput the number of duplicates on last duplicates row
    If counter <> 1 Then
        sht.Cells(i + counter - 1, outColumn) = counter
        i = i + counter - 1
    End If

Next i

End Sub

This is my first program so I apologize for all the mistakes. 这是我的第一个程序,对于所有错误我深表歉意。 This code does exactly what I want except for finding the last row if there is no user input. 除了没有用户输入时查找最后一行之外,这段代码完全符合我的要求。 It always says the last row is 1, when it should be 110460. I'm not sure if it's grabbing from the wrong sheet or if there is an error in my logic. 它总是说最后一行是1,应该是110460。我不确定它是从错误的表中获取还是逻辑上有错误。

This should be easy to fix by just Activating the sheet first. 仅通过先激活工作表即可轻松解决此问题。 I can't recall the exact syntax but since you tagged this macros just record a macro, then select a sheet and click on it somewhere. 我记不清确切的语法,但是由于您标记了此宏,所以只记录了一个宏,然后选择一个工作表并在某个位置单击它。 Then open up the macro it will say something like Sheets("sheet name".Activate. Or Sheets("sheet name").Select. Repeat that for each worksheet you want to run the macro on. To clarify, each time your macro finds the last row on 1 sheet, then you Activate or Select the next worksheet and find the last row again. Suppose this is being called in a loop through list of worksheet names. 然后打开宏,它会显示类似Sheets(“ sheet name” .Activate。或Sheets(“ sheet name”)。Select的内容。对要在其上运行宏的每个工作表重复此操作。要澄清,每次您的宏在一张纸上找到最后一行,然后激活或选择下一个工作表,然后再次找到最后一行,假设这是通过工作表名称列表循环调用的。

I changed the "L" to an 11, and it all seems to work now. 我将“ L”更改为11,现在看来一切正常。 Why it wants it this way i have no clue, but it works. 为什么以这种方式想要它,我不知道,但是它有效。

I always do it like this. 我总是这样。

Sub FindingLastRow()

'PURPOSE: Different ways to find the last row number of a range
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ThisWorkbook.Worksheets("Sheet1")

'Ctrl + Shift + End
  LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

'Using UsedRange
  sht.UsedRange 'Refresh UsedRange
  LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row

'Using Table Range
  LastRow = sht.ListObjects("Table1").Range.Rows.Count

'Using Named Range
  LastRow = sht.Range("MyNamedRange").Rows.Count

'Ctrl + Shift + Down (Range should be first cell in data set)
  LastRow = sht.Range("A1").CurrentRegion.Rows.Count

End Sub

https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Outlook VBA在Excel工作表中查找最后一行 - Outlook VBA find last Row in Excel Worksheet 访问 2007 vba 以查找 Excel 2007 工作表中的最后一行 - Access 2007 vba to find last row in Excel 2007 worksheet 优秀 在不同的工作表中查找行 - excel | find row in different worksheet 一个Excel宏,可以使用两列中的数据将新行添加并填充到单独的工作表中 - An Excel macro that can add and populate a new row to a separate worksheet using data from two columns 查找Excel工作簿的工作表名称和行号 - Find Worksheet Name and Row Number for an Excel Workbook 编辑Excel VBA模块以仅通过电子邮件发送工作表的最后一行 - Edit Excel VBA Module to only email the last row of the worksheet AddIn工作表的最后一行 - Last row of AddIn worksheet EXCEL VBA 尝试将数据从一个工作表上的非连续单元格复制到单独工作表上的下一个可用行 - EXCEL VBA Trying to copy data from non consecutive cells on one worksheet to the next available row on separate worksheet 编写一个for循环以查找从活动行到最后一个工作表末尾的值 - Write a for loop to find a value from the active row to the end of last worksheet 如何使用excel vba自动复制同一工作表中最后一行的数据并将其复制到同一表的最后一行? - How to copy the data in last row and copy to the last row of a table in the same worksheet automatically using excel vba?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM