Macros-count the last row & use the results

lkcred

New Member
Joined
Feb 19, 2014
Messages
23
I want to use the value of the last row found into a range using For Each...In [A:A]...in other words I want the range to stop at the last cell w/content in it...

'***Find last row
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

'***Modify Data In Cells
For Each nb In [A:A] or LastRow.Rows
If nb.Value Like "*description *" Then
nb.Cells.Font.Bold = True
End If
Next nb

I want the loop to stop at the last row it found...then it moves to the next part of the code...
 
after my macros finishes through deleting "unwanted" cells & modifying "wanted" cells...column A has a list of cells w/the wanted data...any cell w/FE* has a corresponding "next" cell below...I want to merge the content of the two cells in to one cell...
Maybe if you see the whole picture...here is my full script:
Dim Filter As String, Title As String, FilterIndex As Integer, Filename As Variant, LastRow As Long
Dim rng As Range, c As Range, nb As Range, x As String, varLastRow, rw As Long
'***************************************************Function To Get Information From A File
Sub GetData4()
'***File types
Filter = "Excel Files (*.xls),*.xls," & _
"Text Files (*.txt),*.txt," & _
"All Files (*.*),*.*"
FilterIndex = 3 'Default Filter to *.*
Title = "SELECT FILE TO OPEN:" 'Caption
'*************************************Start Drive & Path
ChDir ("C:\")
With Application
'***Name selected File
Filename = .GetOpenFilename(Filter, FilterIndex, Title, ActiveWorkbook.Name)
End With
'***Exit With No Selection
If Filename = False Then
MsgBox "NO FILE WAS SELECTED!", vbExclamation
Exit Sub
End If
'***Open File & Extract Data
Workbooks.Open Filename
MsgBox Filename, vbInformation, "DATA EXTRACTED FROM:"
'************************************Modify Data In Cells
'***replace wanted content in cells
Application.ReplaceFormat.Clear
Application.ReplaceFormat.Font.Bold = True
Range("A1", Cells(Rows.Count, "A").End(xlUp)).Replace "description ", " ", xlPart, , False, , False, True
Range("A1", Cells(Rows.Count, "A").End(xlUp)).Replace "interface ", " ", xlPart, , False, , False, True
Range("A1", Cells(Rows.Count, "A").End(xlUp)).Replace "*ip address", " ", xlPart, , False, , False, True
Range("A1", Cells(Rows.Count, "A").End(xlUp)).Replace "GigabitEthernet", "GE", xlPart, , False, , False, True
Range("A1", Cells(Rows.Count, "A").End(xlUp)).Replace "FastEthernet", "FE", xlPart, , False, , False, True
Application.ReplaceFormat.Clear
'***bold wanted content in cells
For Each nb In Range("A1:A999")
If ActiveCell.Characters(10, 1).Font.Bold = False Then
ActiveCell.EntireRow.Delete
Else: ActiveCell.Offset(1, 0).Select
End If
Next nb
'***combine wanted content from an found data in each cell & merge the content to the next cell below
For Each nb In [A1:A555]
If nb.Value Like "FE*" Or nb.Value Like "GE*" Or nb.Value Like "Vlan*" Then
rw = rw + 1
Range("B" & rw).Value = nb.Value & " " & nb.Offset(1).Value
End If
Next nb
Columns("A:Z").EntireColumn.AutoFit
End Sub
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
In Message #10, your code showed this statement...

Code:
If i.Value Like "FE*" Or i.Value Like "Vlan*" Then
As structured, that is looking for FE or Vlan at the beginning of the text which is what hiker95 and I assumed when we wrote our code solutions. But in looking at the code you just posted, I am not so sure that this text is always (ever?) going to be the first characters in the text. So we have some idea how to proceed, please clarify this for us... is the FE or Vlan always going to be the first characters in the text or not?
 
Upvote 0
Yes...FE* GE* Vlan* will always be at the beginning of a cell im looking for...

Then I am at a loss why neither hiker95 nor my code worked for you. Is there any chance you can post a copy of your workbook so we can see first hand what is happening with our codes as it interacts with your actual data? If so, you can post a copy of your workbook to a SkyDrive or Box account if you have one, else use one of the free online file sharing websites on the internet. In addition, I would ask you to send a copy of your workbook directly to me as my computer is having problems downloading files from websites. My email address is...

rick DOT news AT verizon DOT net
 
Upvote 0
I tried to get an image of the output....didnt work out...so if Port-chennel1 is at A1 then the code should skip cell A1...when the code looks at A2 it should merge the content of A2 with A3 into B2's new value of "FE1/0/1 ES752-1" & removing the contents of A2 & A3...the code continues until it reaches the end...I hope this helps.
[TABLE="width: 184"]
<tbody>[TR]
[TD] Port-channel1[/TD]
[/TR]
[TR]
[TD] FE1/0/1[/TD]
[/TR]
[TR]
[TD] ES752A-1[/TD]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M-lan[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD] Vlan6[/TD]
[/TR]
[TR]
[TD] DIS-DMT-LAN[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD] Vlan99[/TD]
[/TR]
[TR]
[TD] BITBUCKET[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
I found the issue...seems that I need to do "*FE*" there was a space in the front of the content...final script...sorry everyone...
rw = 1
For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
If c.Value Like "*FE*" Or c.Value Like "*GE*" Or c.Value Like "*Vlan*" Then
Range("B" & rw).Value = c.Value & " " & c.Offset(1).Value
rw = rw + 1
End If
Next c
 
Upvote 0
I put in a "*" (probably after an edit ) that should be an "&"

I.Offset(0, 1) = I.Value & " " * I.Offset(1, 0).Value
should be
I.Offset(0, 1) = I.Value & " " & I.Offset(1, 0).Value
 
Upvote 0
I found the issue...seems that I need to do "*FE*" there was a space in the front of the content...final script...sorry everyone...
rw = 1
For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
If c.Value Like "*FE*" Or c.Value Like "*GE*" Or c.Value Like "*Vlan*" Then
Range("B" & rw).Value = c.Value & " " & c.Offset(1).Value
rw = rw + 1
End If
Next c
Here is the non-looping code I posted in Message #14 modified to work around that leading space (I also added the test for GE at the beginning which your above code shows you want as well)..

Code:
Sub CombineFEorVlanCellsInColumnAintoColumnB()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp)).Offset(, 1)
    .Formula = "=IF(OR(LEFT(TRIM(RC1),2)=""FE"",LEFT(TRIM(RC1),2)=""GE""LEFT(TRIM(RC1),4)=""Vlan""),RC1&"" ""&R[1]C1,"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0
When I run the code there is a run-time error 1004, application-defined or object-defined error at .Formula = "=IF(OR(LEFT(TRIM(RC1),2)=""FE"",LEFT(TRIM(RC1),2)=""GE""LEFT(TRIM(RC1),4)=""Vlan""),RC1&"" ""&R[1]C1,"""")"
 
Upvote 0
When I run the code there is a run-time error 1004, application-defined or object-defined error at .Formula = "=IF(OR(LEFT(TRIM(RC1),2)=""FE"",LEFT(TRIM(RC1),2)=""GE"",LEFT(TRIM(RC1),4)=""Vlan""),RC1&"" ""&R[1]C1,"""")"

Sorry, a comma (shown in red above) got dropped somehow when I copy/paste the code into my response. Here is the corrected code for you to try...

Code:
Sub CombineFEorVlanCellsInColumnAintoColumnB()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp)).Offset(, 1)
    .FormulaR1C1 = "=IF(OR(LEFT(TRIM(RC1),2)=""FE"",LEFT(TRIM(RC1),2)=""GE"",LEFT(TRIM(RC1),4)=""Vlan""),RC1&"" ""&R[1]C1,"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

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