Help with VBA coding please :)

jedibrown

Board Regular
Joined
Oct 17, 2011
Messages
136
Good morning,

I wondered if somebody could help me.

I have the following code but I need to work out how to put each variable on another line - I can't figure it out. So each "or/and" combination I want on a seperate line.

Private Sub CommandButton1_Click()
Dim LR As Long, i As Long
Sheets("Sheet2").UsedRange.ClearContents
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Not IsError(Range("A" & i)) Then
If LCase(Range("C" & i).Value) <> "complete" And _
LCase(Range("C" & i).Value) <> "rejected-nonissue" And _
(InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And InStr(LCase(Range("M" & i).Value), "kh") > 0 Or InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And InStr(LCase(Range("M" & i).Value), "gd") > 0 Or InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And InStr(LCase(Range("M" & i).Value), "tb") > 0 Or InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And InStr(LCase(Range("M" & i).Value), "kc") > 0 Or InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And InStr(LCase(Range("M" & i).Value), "tg") > 0 Or InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And InStr(LCase(Range("M" & i).Value), "gr") > 0 Or InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And InStr(LCase(Range("M" & i).Value), "js") > 0 Or InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And InStr(LCase(Range("M" & i).Value), "sc") > 0) _
Then Rows(i).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Is this what you mean?

Code:
Private Sub CommandButton1_Click()Dim LR As Long, i As Long


    Sheets("Sheet2").UsedRange.ClearContents
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR

        If Not IsError(Range("A" & i)) Then

            If LCase(Range("C" & i).Value) <> "complete" And _
                LCase(Range("C" & i).Value) <> "rejected-nonissue" And _
                InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And _
                (InStr(LCase(Range("M" & i).Value), "kh") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "gd") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "tb") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "kc") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "tg") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "gr") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "js") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "sc") > 0) Then
                
                Rows(i).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
        End If
    Next i
End Sub
 
Upvote 0
Perfect - simple as that! Thank you very much.

I don't suppose you know much about dates and VBA do you? I wanted to add another variable so that it only pulls through lines with a date between 1st January 2012 and 31st January 2012 from column K

Thanks again for your help - much appreciated.
 
Upvote 0
Code:
Private Sub CommandButton1_Click()
Dim LR As Long, i As Long


    Sheets("Sheet2").UsedRange.ClearContents
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
    
        If Not IsError(Range("A" & i)) Then
        
            If LCase(Range("C" & i).Value) <> "complete" And _
                LCase(Range("C" & i).Value) <> "rejected-nonissue" And _
                Year(Range("K" & i).Value) = 2012 And Month(Range("K" & i).Value) = 1 And _
                InStr(LCase(Range("F" & i).Value), "compensation payment") > 0 And _
                (InStr(LCase(Range("M" & i).Value), "kh") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "gd") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "tb") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "kc") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "tg") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "gr") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "js") > 0 Or _
                InStr(LCase(Range("M" & i).Value), "sc") > 0) Then
                
                Rows(i).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
        End If
    Next i
End Sub
 
Upvote 0
That suggests that you have text in one of the date cells. The most likely candidate is the first row, which is probably a header. Change the loop to go from 2 To LR rather than 1 To LR.
 
Upvote 0
Apologies - another question if that would be OK please?

I want this to pull the data from a sheet called "raw data" as opposed to the sheet the button is in. Do you know how I work write this?

Many thanks again
 
Upvote 0
Rich (BB code):
Worksheets("raw data").
Rows(i).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)

That is all one line, the forum is throwing the line.
 
Upvote 0
Sorry - you may have realised I'm quite new at this.

I thought I would have to put coding at the start to change the range for the "raw data" sheet rather than the active sheet that the button is in?

Thanks - sorry!
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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