VBA Excel Loop for Multiple occurrence of a Value

KAHHAR

New Member
Joined
Feb 23, 2021
Messages
5
Office Version
  1. 2010
Platform
  1. Windows
Create Loop for Copy data for Multiple occurrence of Sheets("Result").Range("D1").Text

VBA Code:
Private Sub CommandButton1_Click()
Dim celldesav As Range
Dim rdesav As Range
Dim searchdesav As String

Set rdesav = Sheets("List").Columns("A:A")
searchdesav = Sheets("Result").Range("D1").Text
Set celldesav = rdesav.Find(What:=searchdesav, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)

Sheets("List").Range(celldesav, celldesav.Offset(-4, 0)).Copy
Sheets("Result").Range("A1").PasteSpecial Paste:=xlPasteValues
Sheets("List").Range(celldesav, celldesav.Offset(1, 0)).Copy
Sheets("Result").Range("A1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End Sub

Need a quick help please
 

Attachments

  • Sheet- List.png
    Sheet- List.png
    74 KB · Views: 26
  • Sheet- Result.png
    Sheet- Result.png
    71.8 KB · Views: 25

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi Kahhar,

Welcome to the board,

I would suggest using findnext in vba for searching multiple occurrences of a string, in a data set.
Complete how to is available here.

hth...
 
Upvote 0
Hi Kahhar,

Welcome to the board,

I would suggest using findnext in vba for searching multiple occurrences of a string, in a data set.
Complete how to is available here.

hth...
I'M new in codding. could you please describe me about looping my code.
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()
Dim celldesav As Range
Dim rdesav As Range
Dim searchdesav As String

Set rdesav = Sheets("List").Columns("A:A")
searchdesav = Sheets("Result").Range("D1").Text
Set celldesav = rdesav.Find(What:=searchdesav, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)

If Not celldesav Is Nothing Then
    FirstAddress = celldesav.Address
    Do
        Sheets("List").Range(celldesav.Offset(1, 0), celldesav.Offset(-4, 0)).Copy
        Sheets("Result").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
        Set celldesav = rdesav.FindNext(celldesav)
        if celldesav Is Nothing Then Goto DoneFinding
    Loop While celldesav.Address <> FirstAddress
DoneFinding:
End IF
End Sub
 
Upvote 0
Solution
VBA Code:
Private Sub CommandButton1_Click()
Dim celldesav As Range
Dim rdesav As Range
Dim searchdesav As String

Set rdesav = Sheets("List").Columns("A:A")
searchdesav = Sheets("Result").Range("D1").Text
Set celldesav = rdesav.Find(What:=searchdesav, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)

If Not celldesav Is Nothing Then
    FirstAddress = celldesav.Address
    Do
        Sheets("List").Range(celldesav.Offset(1, 0), celldesav.Offset(-4, 0)).Copy
        Sheets("Result").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
        Set celldesav = rdesav.FindNext(celldesav)
        if celldesav Is Nothing Then Goto DoneFinding
    Loop While celldesav.Address <> FirstAddress
DoneFinding:
End IF
End Sub
Can i use Greater then zero value for search instead of "Sheets("Result").Range("D1").Text"
 
Upvote 0
What do you mean? Greater than zero is almost everything.
Can you give an example what you want?
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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