简体   繁体   中英

Working with Arrays VBA Excel

I am developing a macro to eliminate blank rows from a worksheet which is used for entering customized orders. Lets say rows 7,8,9 and 12 have contents. I want to move the contents of row 12 to row 10.

So far I've located the last occupied row in column c then identified whether the cell in the row in column e is blank or not.

Now I want to put a value into an array either 0 (blank) or 1 (occupied). I am getting an error (object required) on the line of code that sets the value of stones (1) to 1 or 0.

What is going wrong?

  Dim ws As Worksheet
Dim rng1 As Range
Set ws = Sheets("Belmont")
Set rng1 = ws.Columns("c").Find("*", ws.[c1], xlValues, , xlByRows, xlPrevious)
Dim zen As String
zen = rng1.Address(0, 0)
Range(zen).Select
Set ruby = Window.ActiveCell.Row - 11
Dim stones() As Boolean
ReDim stones(1 To ruby)
If IsEmpty(ActiveCell.Offset(2, 0)) Then
Set stones(1) = 0
Else
Set stones(1) = 1
End If
msg55 = MsgBox(stones(1), vbDefaultButton1, "Gekko")

My assumption is that you are doing this for purposes of learning rather than practicality:

You could google VBA arrays and get a plethora of material on the subject. I would start here: http://www.cpearson.com/excel/vbaarrays.htm

You would declare your array like so:

Dim stones(1 To 10) As Double

You're going to have to iterate through each cell in your range. You can Google how to do that as well:

Loop through each cell in a range of cells when given a Range object

You can set the value of the 5th element in the array to the value of 10 like so:

stones(5) = 10

It really seems like you need to do some basic VBA programming tutorials. You could start here:

http://www.mrexcel.com/forum/excel-questions/667818-tutorials-excel-macros-visual-basic-applications.html

If you're trying to get rid of blank cells in sheet 'Belmont' column C, then this should work for you:

Sub tgr()

    Dim rngBlanks As Range

    With Sheets("Belmont").Range("C1", Sheets("Belmont").Cells(Rows.Count, "C").End(xlUp))
        On Error Resume Next
        Set rngBlanks = .SpecialCells(xlCellTypeBlanks)
        On Error GoTo 0
        If Not rngBlanks Is Nothing Then rngBlanks.EntireRow.Delete
    End With

    Set rngBlanks = Nothing

End Sub

If you want to delete all rows in which column C is blank, then:

Sub dural()
    Dim r As Range
    Set r = Range("C:C").Cells.SpecialCells(xlCellTypeBlanks).EntireRow
    r.Delete
End Sub

will accomplish this without looping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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