Excel 第一张工作表已满时,如何将数据写入下一张工作表?
我正在使用函数从数据库中选择数据。结果最多可以添加200万行,因为Excel限制在14000000行左右,所以它只剪切数据。我需要多余的数据移动到新的工作表。 我尝试添加带有if条件的工作表Excel 第一张工作表已满时,如何将数据写入下一张工作表?,excel,vba,Excel,Vba,我正在使用函数从数据库中选择数据。结果最多可以添加200万行,因为Excel限制在14000000行左右,所以它只剪切数据。我需要多余的数据移动到新的工作表。 我尝试添加带有if条件的工作表 If ActiveWorkbook.ActiveSheet.Cells.Rows.Count > 250 Then ActiveWorkbook.Worksheets.Add End If 它只是创建了一个空表。我把这个条件放在哪里? 函数本身不起作用,在宏主体中也不起作用。
If ActiveWorkbook.ActiveSheet.Cells.Rows.Count > 250 Then
ActiveWorkbook.Worksheets.Add
End If
它只是创建了一个空表。我把这个条件放在哪里?
函数本身不起作用,在宏主体中也不起作用。您希望查找包含数据的最后一行,而不是绝对的最后一行(这是count返回的) 试着这样做:
Sub test()
Dim lLastRow As Long
lLastRow = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
If lLastRow > 250 Then ActiveWorkbook.Worksheets.Add
End Sub
Dim sheetToPaste As Worksheet
Dim row As Long, column As Long
Set sheetToPaste = ThisWorkbook.Sheets("Name_of_the_sheet") ' refere to the sheet where you're starting
' this is where you tell Excel to work with this sheet
column = 1 'define column to paste
row = 1 ' define starting row
For Each item In itemsCollection ' this is a dummy code, just to show that you are going through some container
' and depends on container type you use
If row = 250 Then 'this is the condition to create new sheet and paste data there, you may use "row=sheetToPaste.Rows.Count" to check whehter you're at the bottom of the sheet
Set sheetToPaste = ThisWorkbook.Sheets.Add 'add new sheet and tell Excel to work with it
row = 1 ' re-define starting row
Else
sheetToPaste.Cells(row, column) = item ' if row value is less then limit - paste item data to this row
row = row + 1 ' step into next row
End If
Next
请记住,如果您只是通过将整个记录集复制到工作表来添加数据,则不会有任何效果。您必须编写一个循环,遍历数据集的每一行,将其逐个添加到工作表中,并在到达最大行时进行检测,这意味着您真正需要的只是一个计数器
另一种选择是优化数据库查询以确保它不会返回太多记录,然后发出另一个查询以获取下一个数据子集
但事实上,如果要插入那么多数据,您可能首先应该重新思考为什么需要Excel中的所有数据,因为您在尝试处理这些数据时,将来会遇到更多问题,而且速度会很慢。如果您无法采纳@braX的建议,您可以按照以下步骤操作: 如果 结果最多可以添加200万行 首先,您必须将其放入一个容器(如数组或集合)中,而不仅仅是粘贴到工作表中 那么要避免 。。。擅长只是剪切数据 您必须循环使用此容器并逐个粘贴到工作表 第三件事——修改每页250行的限制(如果我理解正确的话),因为将2M记录划分为每页250行将导致大约8k张。你可能会熟悉办公室的限制 第四步与循环和图纸创建相连接 它只是创建了一个空表 为了使Excel将数据放入某个工作表,您必须告诉Excel将数据放入哪个工作表。在您的例子中,代码
ActiveWorkbook.Worksheets.Add
只是告诉Excel创建一个新的工作表,没有其他内容
因此,在循环中,您可以使用如下内容:
Sub test()
Dim lLastRow As Long
lLastRow = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
If lLastRow > 250 Then ActiveWorkbook.Worksheets.Add
End Sub
Dim sheetToPaste As Worksheet
Dim row As Long, column As Long
Set sheetToPaste = ThisWorkbook.Sheets("Name_of_the_sheet") ' refere to the sheet where you're starting
' this is where you tell Excel to work with this sheet
column = 1 'define column to paste
row = 1 ' define starting row
For Each item In itemsCollection ' this is a dummy code, just to show that you are going through some container
' and depends on container type you use
If row = 250 Then 'this is the condition to create new sheet and paste data there, you may use "row=sheetToPaste.Rows.Count" to check whehter you're at the bottom of the sheet
Set sheetToPaste = ThisWorkbook.Sheets.Add 'add new sheet and tell Excel to work with it
row = 1 ' re-define starting row
Else
sheetToPaste.Cells(row, column) = item ' if row value is less then limit - paste item data to this row
row = row + 1 ' step into next row
End If
Next
这种方法肯定会为您提供更多的机会,以过滤您在其他条件下粘贴到工作表中的数据。只需将它们添加到For…Next
语句之间
对此,
我把这个条件放在哪里?函数本身不起作用,
无论是在宏主体中
没有人会回答这个问题,除非你至少展示一下它在你的文件中是如何工作的。如果不知道数据是如何写入工作表的,这段代码就毫无意义。写入数据的函数是什么?