search in cell below previously found cell in VBA

LSTR_94

New Member
Joined
Apr 13, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all,

i'm pretty new to coding in VBA that's why i need your help.
the macro i want to make is like this:
i have multiple cells in column A from sheet2 that i need to find in column A from sheet1,
if found i want the macro to look at the cell below it and search for a amount of spaces in that cell, if this is the case then the full value of that cell should be copy and pasted in a new row under the cell from the first search.
so far i have programmed this but it gets stuck at the second find function:

Option Explicit
Sub search_for_SUB()
Dim i As Integer
Dim NumRows As Integer
Dim MG As String
Dim rng_MG As Range
Dim rng_SUBMG As Range
Dim SPACES As String
Dim submg As String

SPACES = " "

Application.ScreenUpdating = False
NumRows = Range("A10", Range("A10").End(xlDown)).Rows.Count
Range("A10").Select
For i = 10 To NumRows
Worksheets("sheet2").Activate

MG = ActiveCell.Value

Set rng_MG = Worksheets("sheet1").Cells.Find(what:=MG, _
LookIn:=xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)


If Not rng_MG Is Nothing Then
Set rng_SUBMG = Cells.Offset(1, 0).Find(what:=SPACES, _
LookIn:=xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not rng_SUBMG Is Nothing Then

Else
GoTo skip
End If

Else
GoTo skip
End If
skip:
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
End Sub

i hope somebody could help me with this
thanks in advance
 

Attachments

  • search for sub.png
    search for sub.png
    50.8 KB · Views: 17

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try:

VBA Code:
Option Explicit

Sub search_for_SUB()
    Dim i As Integer
    Dim NumRows As Integer
    Dim MG As String
    Dim rng_MG As Range
    Dim rng_SUBMG As Range
    Dim SPACES As String
    Dim submg As String
    Dim newRow As Long
    
    SPACES = " "
    
    Application.ScreenUpdating = False
    NumRows = Worksheets("sheet2").Range("A10", Range("A10").End(xlDown)).Rows.Count
    
    For i = 10 To NumRows
        Worksheets("sheet2").Activate
        MG = ActiveCell.Value
        
        Set rng_MG = Worksheets("sheet1").Cells.Find(what:=MG, _
            LookIn:=xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
        If Not rng_MG Is Nothing Then
            Set rng_SUBMG = Worksheets("sheet2").Cells.Offset(1, 0).Find(what:=SPACES, _
                LookIn:=xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            If Not rng_SUBMG Is Nothing Then
                newRow = rng_MG.Row + 1
                rng_SUBMG.Offset(0, 1).Copy Destination:=Worksheets("sheet1").Cells(newRow, 1)
            End If
        End If
        
        ActiveCell.Offset(1, 0).Select
    Next
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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