VBA code - Copy / paste first visible cell after filter applied

Radek1

New Member
Joined
Apr 17, 2019
Messages
11
Dears,

I would like to kindly ask you for your help with vba code for copying first visible cells after filtering.
I have vba which copy / paste everytime value from one fixed cell (A9)....now as need to update file and more variables appearing, I need to copy first visible cell under header (A8)

Old vba code...
Worksheets("Cover sheet").Range("M17").Value = Worksheets("Kamil Deml").Range("A9").Value

I believe, for you no any problem :)

Thanks in advance

Rregads

Radek
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try to change this to suit what you need:

Code:
crit = 2 'change

With Sheets("Sheet1") 'change
    If .AutoFilterMode Then .AutoFilterMode = False
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    If lr < 9 Then Exit Sub
    With .Range("A8:A" & lr)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:=crit
        If .SpecialCells(xlCellTypeVisible).Count > 1 Then
            .Offset(1, 0).Resize(lr - 1).SpecialCells(xlCellTypeVisible).Cells(1).Copy
        End If
    End With
    .AutoFilterMode = False
End With
 
Upvote 0
Hi,

Thank you for your advice. I'm real beginner in VBA and tried to fit to whole code chain but no success...I'm pretty sure that your code works, but it's by my hands :)
Whole code chain is linked to click button. I just need to copy data from one sheet to another for these (in red color)...rest is working (I did not create that code at all, just request from my boss to update for mentioned problem...

Sub Button2_Click()


Dim rng As Range, cel As Range
On Error Resume Next
Set rng = Range("B9:B" & Cells(Rows.Count, "B").End(3).Row).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then
For Each cel In rng
If cel <> rng(1) Or cel(1, 2) <> rng(1, 2) Then
MsgBox " More then one supplier / initiative selected " & vbCrLf & " See cell(s) " & cel.Address(0, 0) & " and/or " & cel(1, 2).Address(0, 0)
cel.Select
Exit Sub
End If
Next


Worksheets("Kamil Deml").Range("E9:E10000").Copy
Worksheets("Cover sheet").Cells(27, 2).PasteSpecial xlPasteValues
Application.CutCopyMode = False


Worksheets("Kamil Deml").Range("F9:F10000").Copy
Worksheets("Cover sheet").Cells(27, 6).PasteSpecial xlPasteValues
Application.CutCopyMode = False


Worksheets("Kamil Deml").Range("G9:G10000").Copy
Worksheets("Cover sheet").Cells(27, 7).PasteSpecial xlPasteValues
Application.CutCopyMode = False


Worksheets("Kamil Deml").Range("Q9:Q10000").Copy
Worksheets("Cover sheet").Cells(27, 15).PasteSpecial xlPasteValues
Application.CutCopyMode = False


Worksheets("Kamil Deml").Range("N9:N10000").Copy
Worksheets("Cover sheet").Cells(27, 19).PasteSpecial xlPasteValues
Application.CutCopyMode = False




Worksheets("Cover sheet").Range("M17").Value = Worksheets("Kamil Deml").Range("A9").Value
Worksheets("Cover sheet").Range("G8").Value = Worksheets("Kamil Deml").Range("C9").Value
Worksheets("Cover sheet").Range("G10").Value = Worksheets("Kamil Deml").Range("B9").Value
Worksheets("Cover sheet").Range("G12").Value = Worksheets("Kamil Deml").Range("J9").Value


Worksheets("Cover sheet").Select
Worksheets("Cover sheet").Range("A1:A1").Select


End If






LastRow = Sheets("Kamil Deml").Cells(Sheets("Kamil Deml").Rows.Count, "A").End(xlUp).Row
Filter_count = Sheets("Kamil Deml").Range("A9", Sheets("Kamil Deml").Cells(LastRow, 1)).Rows.SpecialCells(xlCellTypeVisible).Count


If Filter_count > 40 Then

For i = 1 To Filter_count - 40
Worksheets("Cover sheet").Cells(28, 2).EntireRow.Insert


Next i
End If
End Sub



Try to change this to suit what you need:

Code:
crit = 2 'change

With Sheets("Sheet1") 'change
    If .AutoFilterMode Then .AutoFilterMode = False
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    If lr < 9 Then Exit Sub
    With .Range("A8:A" & lr)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:=crit
        If .SpecialCells(xlCellTypeVisible).Count > 1 Then
            .Offset(1, 0).Resize(lr - 1).SpecialCells(xlCellTypeVisible).Cells(1).Copy
        End If
    End With
    .AutoFilterMode = False
End With
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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