简体   繁体   English

用于测试和更改用户范围输入的行号和工作表索引号的 VBA 函数

[英]VBA Function to test and change user range input's row number and sheet index no

I'm trying to perform quite a simple task with a custom function in VBA.我正在尝试使用 VBA 中的自定义函数执行一项非常简单的任务。

I have a macro which iterates through large Excel data files which are split into sheets of 50,000 rows.我有一个宏,它遍历被分成 50,000 行的大 Excel 数据文件。

Unfortunately I can't stitch these all together and loop down the columns because this often exceeds the 1,048,576 row limit for each sheet in excel.不幸的是,我无法将这些全部拼接在一起并向下循环列,因为这通常超过 excel 中每张纸的 1,048,576 行限制。

My macro will loop down through the data until a condition is met, and record its row number for future reference.我的宏将遍历数据直到满足条件,并记录其行号以供将来参考。 Obviously when it reaches any row >50,000 then it needs to move onto the next sheet and carry on the iteration process.显然,当它到达任何行 >50,000 时,它需要移动到下一张纸并继续迭代过程。

However I'm having trouble dealing with values either side of the 50,000 limit eg: sheets(3).Cells(50021,2) .但是,我无法处理 50,000 限制两侧的值,例如: sheets(3).Cells(50021,2)

What I need is something like this:我需要的是这样的:

Input:输入:

customfunction(sheets(3).Cells(50021,2))

Output:输出:

sheets(4).Cells(21,2)
Public Type SheetPositions
  SheetIndex As Long
  RowNumber As Long
End Type

Public Function TranslateRange(ByVal c As Range) As Range
  With TranslatePositions(c.Worksheet.Index, c.Row)
    Set TranslateRange = c.Worksheet.Parent.Worksheets(.SheetIndex).Cells(.RowNumber, c.Column)
  End With
End Function

Public Function TranslatePositions(ByVal SheetIndex As Long, ByVal RowNumber As Long) As SheetPositions
  Const ROWS_PER_SHEET As Long = 50000

  If RowNumber <= ROWS_PER_SHEET Then
    TranslatePositions.SheetIndex = SheetIndex
    TranslatePositions.RowNumber = RowNumber
  Else
    TranslatePositions.SheetIndex = SheetIndex + RowNumber \ ROWS_PER_SHEET
    TranslatePositions.RowNumber = RowNumber Mod ROWS_PER_SHEET
  End If
End Function
Dim r As Range
Set r = Worksheets(2).Cells(50021, 1)
Set r = TranslateRange(r)
With TranslatePositions(3, 50021)
  MsgBox "Sheet " & .SheetIndex & " row " & .RowNumber
End With

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM