首页 > 解决方案 > 在变量中查找日期和存储范围

问题描述

我试图在工作表中找到 A 列中有日期的“块”。如图所示,一个块由线条分隔。整个文件充满了这些块,但我只需要在 A 列中有日期的块。为了清楚起见,我不仅需要包含日期的行,还需要包含日期的完整块。

例如,我的文件中的一个块是 Range A172:G192。文件图片:

[![在此处输入图片描述][1]][1]

选择第一个块后我应该如何继续?我可能应该使用从第 184 行开始的 Find 函数或 ResultDown 变量行在“A”列上向下移动工作表。但是该行需要是动态的,所以我可以将它用于下一个块。我也不知道会有多少块,所以我也想听听你如何解决这个问题。

我想将所有块保存为不同的变量,然后隐藏工作表中的所有块,然后取消隐藏我存储在变量中的块。

我最大的问题是最后一行。

Result2 = Range(Cells(StartcellRow, 1), Cells(1000, 1)).Find(What:="**/**/****", After:=Range(Cells(StartcellRow, 1))).Select

我不断收到运行时错误 1004

 Public Sub FB_MAKRO()

    Dim FBwb As Workbook
    Dim FBsht As Worksheet
    Dim ACol As Range



'Set variables for workbook and sheet

    Set FBwb = Workbooks.Open(Filename:="C:\Users\l000xxx\Desktop\Makrot\FORCED BALANCE MAKRO\FB HARJOITUS.xls")
    Set FBsht = FBwb.Sheets("Forced Balance")
    Set ACol = FBsht.Range("A1:A1000")

'I want ACol variable to be the entire A-Column. Any ideas?
'For some reason the range function is not working here?





'This locates the date in A'column, so I can select the correct block

    Result = Range("A3:A1000").Find(What:="**/**/****", After:=Range("A3")).Address


'This is the top left corner of the Block1 selection

    ResultUp = Range(Result).End(xlUp).Offset(-1, 0).Address
    Range(ResultUp).End(xlDown).Select
    Range(ActiveCell, ActiveCell).End(xlDown).Select

'The ResultsDownLastRow variable is for Block2 find function
'ResultDown is the bottom right corner of the Block1

    ResultsDownLastRow = Range(ActiveCell, ActiveCell).End(xlDown).Address
    ResultDown = Range(ActiveCell, ActiveCell).End(xlDown).Offset(-2, 6).Address

'First Block assigned. I plan to use this in the end when I hide everything and then unhide these blocks

    Block1 = Range(ResultUp, ResultDown).Select


' NEXT BLOCK STARTS HERE

'StartCellRow is the cell that the find function should start looking for Block2
'Result2 is the find function for Block2


    StartcellRow = Range(ResultsDownLastRow).Row


    Result2 = Range(Cells(StartcellRow, 1), Cells(1000, 1)).Find(What:="**/**/****", After:=Range(Cells(StartcellRow, 1))).Select



End Sub

'这将返回值 194

StartcellRow = Range(ResultsDownLastRow).Row MsgBox StartcellRow 

'这应该有效,但没有。我收到语法错误

Range(Cells(StartcellRow &","& 1),Cells(1000 & "," & 1)).Find(What:="**/**/****", After:=Range(Cells(StartcellRow& ","& 1)).Select 

这也不起作用

'StarcellRow 给出的值为 194

StartcellRow = Range(ResultsDownLastRow).Row

Result2 = Range("A&:StartcellRow:A648").Find(What:="**/**/****", After:=Range("A&:StartcellRow")).Select

这不会给我语法错误,但它不起作用

标签: vbaexcel

解决方案


我会搜索所有货币标题并将它们的行号存储到一个数组中。对于数组中的每个行号,我会查看下面的单元格(行号 + 1)。当单元格中有日期时,我将按以下方式设置范围:

set rangeWithDate = Range(Cells(actualRowNumberInArray - 1, 1), Cells(nextRowNumberInArray - 2, 7))

大批:

Dim array1() As long    

Redim array1(5)        
For i = 1 To 5        
    array(i) = i
Next i

Redim array1(10)        ' changes the "length" of the array, but deletes old entries 
For i = 1 To 10        
    Feld1(i) = i    
Next i  

Redim Preserve array1(15)    ' changes the "length" of the array without deleting old entries

推荐阅读