Help with Array Range and For Each If Then Statement VBA. It works for the first line.

pnwAnalyst

New Member
Joined
Oct 30, 2017
Messages
9
I'm still really new to VBA. And this is the last portion of a long list of macros for completing data cleanup and analysis for a report. Maybe this isn't the best way to do this? I'm still new to this, so I'm open to other suggestions. But it needs to be a macro. This is basically what it looks like (the * fields are filled with a vlookup, which is why I have two different arrays because they're not contiguous):

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]mfg #[/TD]
[TD]mfg # alt[/TD]
[TD]desc[/TD]
[TD]brand[/TD]
[TD]uom[/TD]
[TD]qtyuom[/TD]
[TD]mfgname[/TD]
[TD]mfgcode[/TD]
[TD]mfgtype[/TD]
[TD]contract[/TD]
[TD]contractnum[/TD]
[TD]supcode[/TD]
[TD]effdate[/TD]
[TD]expdate[/TD]
[TD]$date[/TD]
[TD]$exp[/TD]
[TD]status[/TD]
[/TR]
[TR]
[TD]*[/TD]
[TD]*[/TD]
[TD]*[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]*[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[/TR]
[TR]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[TD]aaa[/TD]
[/TR]
</tbody>[/TABLE]

The number of rows varies depending on the report. Sometimes its 4000 rows, sometimes its more, sometimes its less. But I've made sure that every column would be the same. We're trying to automate as much of it as we can so that we might be able to get some less-technical people to be able to run through the entire process. The first time I went through the process it took me 6 hours (although I was taking notes too). :mad: For the senior person here it takes about 2 hours for each one, depending. We have somewhere around 300 of these to do before the end of the year.

Anyways, this code works, but only for the first row. Which makes sense because that's what I've told it to do in setting the ranges. But when I tried to get it to fill down it filled every single row in the entire worksheet and crashed excel. Any help would be appreciated.

Code:
Sub AutomateAllTheThings6()
    Dim arr3() As String
    Dim arr11() As String
    Dim rng3 As Range
    Dim rng11 As Range
    Dim sourcerng As Range


    Set rng3 = ActiveSheet.Range("BH2:BJ2")
    Set rng11 = ActiveSheet.Range("BL2:BV2")
    Set sourcerng = ActiveSheet.Range("BE2:BF2")
    'Set sourcerng = ActiveSheet.Range("BE:BF" & Range("BD" & Rows.count).End(xlUp).Row)
    arr3() = Split("UNKNOWN,UNKNOWN,UNKNOWN", ",")
    arr11() = Split("UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, 00/00/0000, 00/00/0000, 00/00/0000, 00/00/0000, NEEDS REVIEW", ",")
        Call OptimizeCode_Begin
            For Each cell In sourcerng
                If IsEmpty(cell) Then
                    rng3.Value = arr3
                    rng11.Value = arr11
                End If
            Next
        Call OptimizeCode_End
End Sub
 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Ahh right, I knew I forgot to clarify something.

When both cells from the BE and BF column are blank, that's when I need the arrays to get inserted.
 
Upvote 0
Well this code works now, but fills every row with the array's, not just the rows where columns BE and BF are blank. Must be my For Each and If statement.

Code:
Sub AutomateAllTheThings6()
    Dim arr3() As String
    Dim arr11() As String
    Dim rng3 As Range
    Dim rng11 As Range
    Dim sourcerng As Range
    Dim lastRow As Long
        Call OptimizeCode_Begin
            lastRow = Range("D1:D" & Range("D1").End(xlDown).Row).Rows.Count
            Set rng3 = ActiveSheet.Range("BH2:BJ2" & ":BH" & lastRow)
            Set rng11 = ActiveSheet.Range("BL2:BV2" & ":BL" & lastRow)
            Set sourcerng = ActiveSheet.Range("BE2:BF2" & ":BE" & lastRow)
            arr3() = Split("UNKNOWN,UNKNOWN,UNKNOWN", ",")
            arr11() = Split("UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, 00/00/0000, 00/00/0000, 00/00/0000, 00/00/0000, NEEDS REVIEW", ",")
                For Each cell In sourcerng
                    If IsEmpty(cell) Then
                        rng3.Value = arr3
                        rng11.Value = arr11
                    End If
                Next
        Call OptimizeCode_End
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top