Getting VBA to output formatted data in different excel worksheet

ILMWT

New Member
Joined
Dec 9, 2014
Messages
35
Hey all! So I have some VBA that formats some data that I enter. But it formats it in the current tab. Then it messes up the tab and I cannot re-run the vba (linked to a button) without closing the spreadsheet, not saving it, then re-opening. I would like the data I put in to immediately be pasted into a new excel doc, then formatted with the following VBA:

Sub PushTheButton()
Filtername
Tidy
ActiveRangeBorders
InsertRows
Mergetoptwo
ColorCellsWhite
ColorTopRowBlue
End Sub

Sub Filtername()
Dim lr As Long
Dim ws As Worksheet

Set ws = Sheets("WeeklyWS")

lr = ws.Cells(Rows.Count, 9).End(xlUp).Row

For i = 2 To lr
If InStr(ws.Cells(i, 9).Value, "|") > 0 Then
ws.Cells(i, 9).Value = Left(ws.Cells(i, 9).Value, InStr(ws.Cells(i, 9).Value, "|") - 1)
End If
Next i

End Sub

Sub Tidy()
Dim lastcol As Integer
Dim c As Range

Application.ScreenUpdating = False

'Step 1: Clear first name
On Error Resume Next
For Each c In Range("E1:E100")
c.Value = Left(c.Value, InStr(1, c.Value, ",") - 1)
Next
On Error GoTo 0

'Step 2: insert new column at the end
Application.ActiveSheet.UsedRange
lastcol = Selection.SpecialCells(xlCellTypeLastCell).Column + 1
Cells(1, lastcol).Value = "Comment"
Cells(1, lastcol).Font.Color = vbWhite

'Step 4: check column 'I' for specific string
For Each c In Range("I3:I100")
If InStr(1, LCase(c.Value), "HN") > 0 Then
With Cells(c.Row, lastcol)
.Value = "HN"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "BS") > 0 Then
With Cells(c.Row, lastcol)
.Value = "BS"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "RE") > 0 Then
With Cells(c.Row, lastcol)
.Value = "RE"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "PE") > 0 Then
With Cells(c.Row, lastcol)
.Value = "PE"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "MA") > 0 Then
With Cells(c.Row, lastcol)
.Value = "MA"
.Font.Bold = True
End With
ElseIf InStr(1, LCase(c.Value), "CN") > 0 Then
With Cells(c.Row, lastcol)
.Value = "CN"
.Font.Bold = True
End With
Else
With Cells(c.Row, lastcol)
.Font.Bold = False
.ClearContents
End With
End If
Next c
ActiveSheet.Columns("H").Delete
ActiveSheet.Columns("D").Delete
ActiveSheet.Columns("C").Delete

Application.ScreenUpdating = True
End Sub

Sub ActiveRangeBorders()
' Puts borders around only populated cells in a range

Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long

lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count

For Each rngCell In Range("A1:A" & lngLstRow)
If rngCell.Value > "" Then
r = rngCell.Row
c = rngCell.Column
Range(Cells(r, c), Cells(r, "I")).Select 'lngLstCol was where "H" is now. Skipped two columns to the right for some reason.
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
Next

Application.ScreenUpdating = True

End Sub


Sub InsertRows()
Application.ScreenUpdating = False
'Insert 2 Rows Above Row 1
Rows("1:2").Insert Shift:=xlDown, _
CopyOrigin:=xlFormatFromLeftOrAbove
Application.ScreenUpdating = True
End Sub

Sub Mergetoptwo()
Application.ScreenUpdating = False
Range("A1:H1,A2:H2").Select
Range("H2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Application.ScreenUpdating = True
End Sub

Sub ColorCellsWhite()
Application.ScreenUpdating = False

Dim Data As Range
Dim cell As Range
Set currentsheet = ActiveWorkbook.Sheets("WEEKLY WS")
Set Data = currentsheet.Range("A1:Z300")

For Each cell In Data
cell.Interior.ColorIndex = 2
Next

Application.ScreenUpdating = True
End Sub

Sub ColorTopRowBlue()
Application.ScreenUpdating = False

Dim Data As Range
Dim cell As Range
Set currentsheet = ActiveWorkbook.Sheets("WEEKLY WS")
Set Data = currentsheet.Range("A3:I3")

For Each cell In Data
If cell.Value <> "" Then
cell.Interior.ColorIndex = 33
End If
Next

Application.ScreenUpdating = True

End Sub



How would I do this?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
For starters ILMWT, you should post your code in between code tags (see example in red below in my tag-line). With 35 posts to your name you are supposed to understand this. A lot of people won't even bother to reply to your post, if you don't want to put in the effort.

I will look at your code and come back.
 
Upvote 0
OK, your code basically fails because you are not clear on which ranges and sheets the code is to act on.

Take for instance a piece of code from ActiveRangeBorders()
Rich (BB code):
For Each rngCell In Range("A1:A" & lngLstRow)
If rngCell.Value > "" Then
r = rngCell.Row
c = rngCell.Column
Range(Cells(r, c), Cells(r, "I")).Select

Excel will execute this code on whatever sheet is active at the moment, but you have nowhere specified which sheet this should be.

You always have to code meticouisly which sheet the ranges are in etc, etc else you get strange results. So we could rewrite this piece of code as:
Rich (BB code):
        For Each rngCell In wsWStoFormat.Range("A1:A" & lngLstRow)
            If rngCell.Value > "" Then
                r = rngCell.Row
                c = rngCell.Column
                 'lngLstCol was where "H" is now. Skipped two columns to the right for some reason.
                With wsWStoFormat.Range(wsWStoFormat.Cells(r, c), wsWStoFormat.Cells(r, "I")).Borders

which we can shorten to
Rich (BB code):
    With wsWStoFormat
        For Each rngCell In .Range("A1:A" & lngLstRow)
            If rngCell.Value > "" Then
                r = rngCell.Row
                c = rngCell.Column
                 'lngLstCol was where "H" is now. Skipped two columns to the right for some reason.
                With .Range(.Cells(r, c), .Cells(r, "I")).Borders

To make it all a lot more clear, call each sub with the sheet that is to be acted on. Also I note you have two sheets with near identical names, not very user-friendly (WeeklyWS and WEEKLY WS)


So my rehash of your cose is as follows.(note that I do not know in every case which sheet was t be acted on, so modify in PushTheButton as required)

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> PushTheButton()<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <br>    Filtername Sheets("WeeklyWS")<br>    Tidy Sheets("WeeklyWS")<br>    ActiveRangeBorders Sheets("WeeklyWS")<br>    InsertRows Sheets("WeeklyWS")<br>    Mergetoptwo<br>    ColorCellsWhite Sheets("WEEKLY WS")<br>    ColorTopRowBlue Sheets("WEEKLY WS")<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> Filtername(wsWStoFormat <SPAN style="color:#00007F">As</SPAN> Worksheet)<br><SPAN style="color:#007F00">' get rid of any text to right of and incl.  '|' character</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wsWS <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> wsWS = wsWStoFormat<br>    <br>    <SPAN style="color:#00007F">With</SPAN> wsWS<br>        lr = .Cells(Rows.Count, 9).End(xlUp).Row<br>        <br>        <SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> lr<br>            <SPAN style="color:#00007F">If</SPAN> InStr(.Cells(i, 9).Value, "|") > 0 <SPAN style="color:#00007F">Then</SPAN><br>                .Cells(i, 9).Value = Left(.Cells(i, 9).Value, InStr(.Cells(i, 9).Value, "|") - 1)<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> i<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsWS = <SPAN style="color:#00007F">Nothing</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> Tidy(wsWStoFormat <SPAN style="color:#00007F">As</SPAN> Worksheet)<br>    <SPAN style="color:#00007F">Dim</SPAN> LastCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">With</SPAN> wsWStoFormat<br>    <br>        <SPAN style="color:#007F00">'Step 1: Clear first name</SPAN><br>        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> .Range("E1:E100")<br>            c.Value = Left(c.Value, InStr(1, c.Value, ",") - 1)<br>        <SPAN style="color:#00007F">Next</SPAN><br>        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>        <br>        <SPAN style="color:#007F00">'Step 2: insert new column at the end</SPAN><br>        Application.wsWStoFormat.UsedRange   <SPAN style="color:#007F00">' reset used range</SPAN><br>        LastCol = Selection.SpecialCells(xlCellTypeLastCell).Column + 1<br>        .Cells(1, LastCol).Value = "Comment"<br>        .Cells(1, LastCol).Font.Color = vbWhite<br>        <br>        <SPAN style="color:#007F00">'Step 4: check column 'I' for specific string</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> .Range("I3:I100")<br>            <SPAN style="color:#00007F">With</SPAN> .Cells(c.Row, LastCol)<br>                <SPAN style="color:#00007F">If</SPAN> InStr(1, LCase(c.Value), "HN") > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    .Value = "HN"<br>                    .Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">ElseIf</SPAN> InStr(1, LCase(c.Value), "BS") > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    .Value = "BS"<br>                    .Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">ElseIf</SPAN> InStr(1, LCase(c.Value), "RE") > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    .Value = "RE"<br>                    .Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">ElseIf</SPAN> InStr(1, LCase(c.Value), "PE") > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    .Value = "PE"<br>                    .Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">ElseIf</SPAN> InStr(1, LCase(c.Value), "MA") > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    .Value = "MA"<br>                    .Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">ElseIf</SPAN> InStr(1, LCase(c.Value), "CN") > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    .Value = "CN"<br>                    .Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">Else</SPAN><br>                    .Font.Bold = <SPAN style="color:#00007F">False</SPAN><br>                    .ClearContents<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> c<br>        .Columns("H").Delete<br>        .Columns("D").Delete<br>        .Columns("C").Delete<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> ActiveRangeBorders(wsWStoFormat <SPAN style="color:#00007F">As</SPAN> Worksheet)<br><SPAN style="color:#007F00">' Puts borders around only populated cells in a range</SPAN><br><br>    <SPAN style="color:#00007F">Dim</SPAN> lngLstCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lngLstRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    lngLstRow = wsWStoFormat.UsedRange.Rows.Count<br>    lngLstCol = wsWStoFormat.UsedRange.Columns.Count<br>    <br>    <SPAN style="color:#00007F">With</SPAN> wsWStoFormat<br>        For <SPAN style="color:#00007F">Each</SPAN> rngCell <SPAN style="color:#00007F">In</SPAN> .Range("A1:A" & lngLstRow)<br>            <SPAN style="color:#00007F">If</SPAN> rngCell.Value > "" <SPAN style="color:#00007F">Then</SPAN><br>                r = rngCell.Row<br>                c = rngCell.Column<br>                 <SPAN style="color:#007F00">'lngLstCol was where "H" is now. Skipped two columns to the right for some reason.</SPAN><br>                <SPAN style="color:#00007F">With</SPAN> .Range(.Cells(r, c), .Cells(r, "I")).Borders<br>                    .LineStyle = xlContinuous<br>                    .Weight = xlThin<br>                    .ColorIndex = xlAutomatic<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Sub</SPAN> InsertRows(wsWStoFormat <SPAN style="color:#00007F">As</SPAN> Worksheet)<br>    <SPAN style="color:#007F00">'Insert 2 Rows Above Row 1</SPAN><br>    <br>    wsWStoFormat.Rows("1:2").Insert Shift:=xlDown, _<br>    CopyOrigin:=xlFormatFromLeftOrAbove<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> Mergetoptwo(wsWStoFormat <SPAN style="color:#00007F">As</SPAN> Worksheet)<br>    <br>    <SPAN style="color:#00007F">With</SPAN> wsWStoFormat.Range("A1:H1,A2:H2").Activate<br>        .HorizontalAlignment = xlCenter<br>        .VerticalAlignment = xlBottom<br>        .WrapText = <SPAN style="color:#00007F">False</SPAN><br>        .Orientation = 0<br>        .AddIndent = <SPAN style="color:#00007F">False</SPAN><br>        .IndentLevel = 0<br>        .ShrinkToFit = <SPAN style="color:#00007F">False</SPAN><br>        .ReadingOrder = xlContext<br>        .MergeCells = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Selection.Merge<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> ColorCellsWhite(wsWStoFormat <SPAN style="color:#00007F">As</SPAN> Worksheet)<br>    <SPAN style="color:#00007F">Dim</SPAN> Data <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> cell <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> Data = wsWStoFormat.Range("A1:Z300")<br>    <br>    For <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> Data<br>        cell.Interior.Color<SPAN style="color:#00007F">In</SPAN>dex = 2<br>    <SPAN style="color:#00007F">Next</SPAN><br>    <br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> ColorTopRowBlue(wsWStoFormat <SPAN style="color:#00007F">As</SPAN> Worksheet)<br>    <br>    <SPAN style="color:#00007F">Dim</SPAN> Data <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> cell <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> Data = wsWStoFormat.Range("A3:I3")<br>    <br>    For <SPAN style="color:#00007F">Each</SPAN> cell In Data<br>        <SPAN style="color:#00007F">If</SPAN> Len(cell.Value) <SPAN style="color:#00007F">Then</SPAN>     <SPAN style="color:#007F00">' faster than if x <> "" Then</SPAN><br>            cell.Interior.ColorIndex = 33<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN><br>    <br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,216,175
Messages
6,129,300
Members
449,499
Latest member
HockeyBoi

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