Cut,clear and replace.

topi1

Board Regular
Joined
Aug 6, 2014
Messages
248
Office Version
  1. 2010
Hi,
I am hoping to get a vba for the following.
In sheet2, column R, loop through cells and look for cells which start with "GHT:" (not case sensitive).
If it finds two cells that begin with "GHT:", cut and clear the bottom one and paste replace the top one.

Not critical but it will be nice if GHT can be all CAP after cell has been pasted, regardless of how the bottom cell originally was.

The number of preceding, intervening and subsequent rows vary.
"GHT:" could be in the first row although unlikely.
No blanks.
Thank you.

Here is the example.

BEFORE
Only necessary VBA 11.xlsm
R
11
22
33
4GHT: 1
54
65
76
8Ght: 2
97
Sheet2


AFTER
Only necessary VBA 11.xlsm
R
11
22
33
4GHT: 2
54
65
76
8
97
Sheet2
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
What if there are more than to Ght cells?
Do you only want the last Ght moved to the first?
 
Upvote 0
I'm sure there are more elegant ways. but this seems to work with the constraints you have given:
(it probably will not work if you have that string in cells in adjacent columns, but you can update the vba)

VBA Code:
Sub PutLastGhtInFirst()
Dim c As Integer
Dim r As Range
Dim f As Range
Dim s As Range

Set r = ActiveCell.CurrentRegion.Cells

c = 0

For Each cell In r
    If InStr(1, cell, "ght", vbTextCompare) Then c = c + 1
    If c = 1 Then Set f = cell
    If c = 2 Then
        Set s = cell
        f.Value = UCase(s.Value)
        s.Clear
        End If
    Next cell
c = 0 
End Sub
 
Last edited:
Upvote 0
I'm sure there are more elegant ways. but this seems to work with the constraints you have given:
(it probably will not work if you have that string in cells in adjacent columns, but you can update the vba)

VBA Code:
Sub PutLastGhtInFirst()
Dim c As Integer
Dim r As Range
Dim f As Range
Dim s As Range

Set r = ActiveCell.CurrentRegion.Cells

c = 0

For Each cell In r
    If InStr(1, cell, "ght", vbTextCompare) Then c = c + 1
    If c = 1 Then Set f = cell
    If c = 2 Then
        Set s = cell
        f.Value = UCase(s.Value)
        s.Clear
        End If
    Next cell
c = 0
End Sub
Did not work for me. Changed "ght" to "ght:", that did not help. Made vba and cells cASE specific, that did not help either. Nothing happens when I run the code. TY.
 
Upvote 0
Sorry, try this:

VBA Code:
Sub PutLastGhtInFirst()
Dim c As Integer
Dim ff As Boolean
Dim fs As Boolean
Dim r As Range
Dim f As Range
Dim s As Range

Set r = ActiveCell.CurrentRegion.Cells

c = 0
ff = False
fs = False
For Each cell In r
    If InStr(1, cell, "ght", vbTextCompare) Then c = c + 1
    If c = 1 And ff = False Then
        Set f = cell
        ff = True
        End If
    If c = 2 And fs = False Then
        Set s = cell
        f.Value = UCase(s.Value)
        s.Clear
        fs = True
        End If
    Next cell
   
c = 0
ff = False
fs = False
   
End Sub
 
Upvote 0
Solution
Sorry, try this:

VBA Code:
Sub PutLastGhtInFirst()
Dim c As Integer
Dim ff As Boolean
Dim fs As Boolean
Dim r As Range
Dim f As Range
Dim s As Range

Set r = ActiveCell.CurrentRegion.Cells

c = 0
ff = False
fs = False
For Each cell In r
    If InStr(1, cell, "ght", vbTextCompare) Then c = c + 1
    If c = 1 And ff = False Then
        Set f = cell
        ff = True
        End If
    If c = 2 And fs = False Then
        Set s = cell
        f.Value = UCase(s.Value)
        s.Clear
        fs = True
        End If
    Next cell
  
c = 0
ff = False
fs = False
  
End Sub
I am running the code on the following file. But nothing happens.

Only necessary VBA 11.xlsm
R
11
22
33
4GHT: 1
54
65
76
8GHT: 2
97
Sheet2
 
Upvote 0
it is working for me.
Your cursor must be in the range of cells you want to transform when you run the macro

before:
Book1
AB
11
22
33
4ght: 1
54
65
76
8Ght: 2
97
10
Sheet1


after:
Book1
AB
11
22
33
4GHT: 2
54
65
76
8
97
10
Sheet1



Another:

Before:
Book1
AB
11
22
33
4GHT: 1
54
65
76
8GHT: 2
97
10
Sheet1


After:
Book1
AB
11
22
33
4GHT: 2
54
65
76
8
97
10
Sheet1
 
Upvote 0
it is working for me.
Your cursor must be in the range of cells you want to transform when you run the macro

before:
Book1
AB
11
22
33
4GHT: 1
54
65
76
8GHT: 2
97
10
Sheet1


after:
Book1
AB
11
22
33
4GHT: 2
54
65
76
8
97
10
Sheet1
Let me rerun. I move my cursor to go to view>macro etc so I know the cursor is not in the range.
 
Upvote 0
@awoohaw I think I have found the issue. Your code works when the data is in the column A but not when it is in the column R. My data is in the column R though. I think the following modified version works. Thank you for all your help.


VBA Code:
    Worksheets("Sheet2").Activate
    Set r = Worksheets("Sheet2").Range("R:R")
    
    c = 0
    ff = False
    fs = False
    For Each cell In r.Cells
        If InStr(1, cell.Value, "GHT", vbTextCompare) Then c = c + 1
        If c = 1 And ff = False Then
            Set f = cell
            ff = True
        End If
        If c = 2 And fs = False Then
            Set s = cell
            f.Value = UCase(s.Value)
            s.ClearContents
            fs = True
        End If
    Next cell
    
    c = 0
    ff = False
    fs = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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