Define a cell containing specific text as range

Rainmanne

Board Regular
Joined
Mar 10, 2016
Messages
120
Office Version
  1. 2013
Platform
  1. Windows
I've got two slightly different templates where I need to fill right a formula based on the template selected in the userform. I am thinking to simplify it and instead using If Else structure to use specific text to define a start cell. Currently it looks like this (it's a part of the code):
VBA Code:
Dim finstart As Range
Dim endcell As Range, startcell As Range
Dim yearsno As Range
Dim numrowsadj As Integer
Dim cfyearsno As Range
Dim numrows As Integer
Dim numrowscf As Integer

If UserForm1.radioift.Value = True Then
    With ThisWorkbook.Sheets("FS")
        Set startcell = .Range("D1")
        Set endcell = Cells(Range("D" & Rows.Count).End(xlUp).Row, 3 + numrows + numrowsadj)
        Set finstart = .Range(startcell.Address & ":" & endcell.Address)
        finstart.FillRight
    End With
Else
    With ThisWorkbook.Sheets("FS")
    Set startcell = .Range("F1")
    Set endcell = Cells(Range("F150").End(xlUp).Row, 5 + numrows + numrowsadj)
    Set finstart = .Range(startcell.Address & ":" & endcell.Address)
    finstart.FillRight
End With
End If

Basically I want to set startcell to the cell after the cell containing a word "Item". Then define the endcell as the last non-blank cell in the column and then select and fill right by a number of years. The cell containing "Item" if either C1 or E1. I just cannot figure out how to set startcell using the text in the cell. Could anyone help?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
@Rainmanne Maybe similar to below?
Have used LIKE in case cell contains string containing "Item, not just the string "Item" ??
Not tested and please forgive if I have misunderstood.

VBA Code:
With ThisWorkbook.Sheets("FS")

    If Range("C1") Like ("*" & ("Item") & "*") Then
    
            Set startcell = .Range("D1")
            Set endcell = Cells(Range("D" & Rows.Count).End(xlUp).Row, 3 + numrows + numrowsadj)
            Set finstart = .Range(startcell.Address & ":" & endcell.Address)
            finstart.FillRight
        
    ElseIf Range("E1") Like ("*" & ("Item") & "*") Then
    
            Set startcell = .Range("F1")
            Set endcell = Cells(Range("F150").End(xlUp).Row, 5 + numrows + numrowsadj)
            Set finstart = .Range(startcell.Address & ":" & endcell.Address)
            finstart.FillRight
    
    End If
End With
 
Upvote 0
@Rainmanne Maybe similar to below?
Have used LIKE in case cell contains string containing "Item, not just the string "Item" ??
Not tested and please forgive if I have misunderstood.

VBA Code:
With ThisWorkbook.Sheets("FS")

    If Range("C1") Like ("*" & ("Item") & "*") Then
   
            Set startcell = .Range("D1")
            Set endcell = Cells(Range("D" & Rows.Count).End(xlUp).Row, 3 + numrows + numrowsadj)
            Set finstart = .Range(startcell.Address & ":" & endcell.Address)
            finstart.FillRight
       
    ElseIf Range("E1") Like ("*" & ("Item") & "*") Then
   
            Set startcell = .Range("F1")
            Set endcell = Cells(Range("F150").End(xlUp).Row, 5 + numrows + numrowsadj)
            Set finstart = .Range(startcell.Address & ":" & endcell.Address)
            finstart.FillRight
   
    End If
End With

Hi, thanks a lot for that. I had something slightly different in mind. Something like
VBA Code:
With ThisWorkbook.Sheets("FS")
            Set startcell = (a cell with text "Item")
            Set endcell = Cells(Range("D" & Rows.Count).End(xlUp).Row, 3 + numrows + numrowsadj)
            Set finstart = .Range(startcell.Address & ":" & endcell.Address)
            finstart.FillRight
End With

So I do not know how to set the start cell.
The start cell contains the string Item, there is no other text there.
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,900
Members
449,194
Latest member
JayEggleton

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