在变量

时间:2018-06-07 10:15:27

标签: excel vba excel-vba

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

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

[![在此处输入图像说明] [1]] [1]

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

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

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

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

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

2 个答案:

答案 0 :(得分:0)

我会搜索所有货币标题并将他们的rownumber存储到数组中。对于数组中的每个rownumber,我将查看下面的单元格(rownumber + 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

答案 1 :(得分:0)

也许这可以帮到你:

请注意:我希望第一行中的标题

Sub Test()

Dim LR As Long, RW1 As Long, RW2 As Long, X As Long, Y As Long
Dim CL As Range, MyArray() As Range

'Store all the blocks in an array (seperated by lines as in question)
LR = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row 'Or use a manual LR if the last row hasn't got values in column A.
For Each CL In Sheets(1).Range(Cells(2, 1), Cells(LR, 1))
    If CL.Borders(xlEdgeTop).LineStyle <> xlLineStyleNone Then RW1 = CL.Row
    If CL.Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
        RW2 = CL.Row
        ReDim Preserve MyArray(Y)
        Set MyArray(Y) = Sheets(1).Range(Cells(RW1, 1), Cells(RW2, 1))
        Y = Y + 1
    End If
Next CL

'Loop through all the blocks in the array
For X = LBound(MyArray) To UBound(MyArray)
    'Now you could loop through the rows of the variable
    For Each Row In MyArray(X).Rows
        If IsDate(Sheets(1).Cells(Row, 1)) = True Then
            MsgBox "YEY"
        End If
    Next Row

    'Or you could hide the whole range
    MyArray(X).Rows.Hidden = True

    'Check the address
    Debug.Print MyArray(X).Address

    'Or do whatever you please
Next X

End Sub

希望你会发现一些有用的代码。