Defining Variables and VBA Conditional on Cell Value

-Wayner-

Board Regular
Joined
Feb 8, 2008
Messages
84
Good afternoon,

I am trying to write a piece of VBA code which will perform an action (clearing the contents of certain cells) but will only act in a range of cells based on the contents of another cell.

The basic code i am trying to use is as follows (as an extract):

Code:
Sheets("User Interface").Select

If Range("HRTSelect") = "Supergroup" Then k = 7
If Range("HRTSelect") = "Group" Then k = 10
If Range("HRTSelect") = "Upcs" Then k = 13

For j = 4 To 205
If Cells(j, k + 1) = Range("HRTSpecify") And Cells(j + 1, k) = "" Then
Cells(j, k).ClearContents

If Range("HRTSelect") = "Upcs" Then
Range(Cells(j, k + 2), Cells(j, k + 8)).ClearContents

End If
End If
Next j
This row keeps coming up as a run time error '1004' Application-defined or object defined error.

Code:
If Cells(j, k + 1) = Range("HRTSpecify") And Cells(j + 1, k) = "" Then
For what it's worth i have DIM j & k as integers at the beginning of the whole process.

I'm not sure if it's that line itself which is causing the issue, or the way i have tried to specify 'k' in the initial part of my code.

If anyone could give me a little bit of a steer as to what might be causing the issue i would be eternally grateful!

Thanks in advance!
Wayner
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi,

Try changing the range part like so...

Code:
Sheets("MySheet").Range("HRTSpecify") .Value

Change the sheet name accordingly.
 
Upvote 0
End ifs are in the wrong place:

Code:
For j = 4 To 205
If Cells(j, k + 1) = Range("HRTSpecify") And Cells(j + 1, k) = "" Then
      Cells(j, k).ClearContents
End If


If Range("HRTSelect") = "Upcs" Then
    Range(Cells(j, k + 2), Cells(j, k + 8)).ClearContents
End If

see if that helps
 
Upvote 0
Maybe, depends on the order the Op wants it to work in, it may be ok.

With some indentation it becomes:

Code:
    Sheets("User Interface").Select

    If Range("HRTSelect") = "Supergroup" Then k = 7
    If Range("HRTSelect") = "Group" Then k = 10
    If Range("HRTSelect") = "Upcs" Then k = 13
    
    For j = 4 To 205
        If Cells(j, k + 1) = Range("HRTSpecify") And Cells(j + 1, k) = "" Then
            Cells(j, k).ClearContents
            If Range("HRTSelect") = "Upcs" Then
                Range(Cells(j, k + 2), Cells(j, k + 8)).ClearContents
            End If
        End If
    Next j
 
Upvote 0
Thanks for the feedback guys, much appreciated.

On the End If's i think they are where they need to be, as if the first statement is false then the second statement doesn't need to be evaluated (it only need to happen if the first is true).

I'm a little confused by which part you mean to add the sheets statement to, could you elaborate please.

Cheers!
 
Upvote 0
IF it helps i'm sure it must be something to do with how i have defined the variable k, as i have a very similar bit of code as part of the same procedure (occurring before this part without any issue):

Code:
For i = 1 To 2040
If Cells(i, 2) = Range("HRTSpecify") And Cells(i + 1, 2) = "" Then
    Range(Cells(i, 3), Cells(i, 54)).ClearContents
End If
Next i

This works, but doesn't have the 'k' variable. Is the way i have defined 'k' proper procedure (as in based on the contents of the cell i have called HRTselect)?
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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