Populate All Rows with Values with Formula - excel

I was hoping to get help with one last tweak to this code. It works just fine with two extra manual steps, but I would love to make it all automatic with the Macros. In the last paragraph, there is a formula that I would like to be included in as many rows as there are rows with values in them, not just until Row 244.
Sub GLMacro2()
' Shortcut Ctrl+Shift+H
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
Rows(ActiveCell.Row).Delete
Range("N1").Select
ActiveCell.FormulaR1C1 = "Balance"
Columns("A:N").Select
Columns("A:N").EntireColumn.AutoFit
Range("A1").Select
Columns("B:B").Select
Selection.ColumnWidth = 12
Columns("C:C").Select
Selection.ColumnWidth = 12
Columns("H:H").Select
Selection.ColumnWidth = 42.57
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("G2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Selection.Subtotal GroupBy:=7, Function:=xlSum, TotalList:=Array(12, 13), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
With ActiveSheet.Outline
.AutomaticStyles = False
.SummaryRow = xlBelow
.SummaryColumn = xlLeft
End With
Selection.ApplyOutlineStyles
Columns("L:N").Select
Selection.Style = "Comma"
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.ColorIndex = 49
.Pattern = xlSolid
End With
Selection.Font.ColorIndex = 2
Selection.Font.Bold = True
Range("A1").Select
' Balance
ActiveCell.Offset(1, 13).Range("A1").Select
ActiveCell.FormulaR1C1 = "=IF(ISBLANK(C[-3]),RC[-2]-RC[-1],"""")"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A244"), Type:= _
xlFillDefault
ActiveCell.Range("A1:A2").Select
ActiveCell.Columns("A:A").EntireColumn.EntireColumn.AutoFit
Selection.Style = "Comma"
End Sub

Use a code similar to this:
Dim RowCount as Long
RowCount = Cells(Rows.Count,2).End(xlUp).Row
'Will get the row of the last row. Replace 2 with what ever column you want it to be bassed off. Ex: B = 2
Then in your auto-fill, what you want to do it this:
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & RowCount), Type:= _
xlFillDefault

Related

Setting the default value based on the adjacent cell in VBA

Sub Print_New()
'
' Print_New Macro
'
'
ActiveSheet.Unprotect
ActiveSheet.Range("$B$7:$G$24").AutoFilter Field:=1, Criteria1:="<>"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
ActiveSheet.Range("$B$7:$G$24").AutoFilter Field:=1
ActiveSheet.Protect
Sheets("Bill (1)").Copy Before:=Sheets(5)
ActiveSheet.Unprotect
Range("C8:C17,D20,E20:F20").Select
Range("E20").Activate
Selection.ClearContents
Range("G20").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
Range("F8").Select
Range("F8").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F9").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F10").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F11").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F12").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F13").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F14").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F15").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F16").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F17").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("C8").Select
ActiveSheet.Protect
ActiveWorkbook.Save
End Sub
Need a proper code instead of any "IF" formula.
When I write something in any cell in the range C8:C17, the default value 1 should be equal to the same cell in the range F8:F17. Which can be changed. And when C8:C17 is empty then F8:F17 should also be empty.
Please don't do the constant Select and ActiveCell: you might replace:
Range("G20").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
by:
Range("G20").FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
And, instead of using RC, you might do the following:
Range("G20").Formula = "=IF(Offset(-2;0)="""","""",5%)"
In top of this, you can use the whole range of F8:F17:
Range("F8:F17").Formula = "IF(Offset(-3;0)>0,1,"""")"
This is already a big decrease of obsolete code.

VBA sort on similar sheets in a workbook without using named sheet based on months that will change based on current month

I need to copy a list from Column C that has blanks throughout the column to Column 0 sorted to remove the blanks. I need to do this on Multiple sheets represent the month (Jan, Feb, Mar, Apr....). The issue I run into is it uses: ActiveWorkbook.Worksheets("Jan") so if I do a do loop to get the other months (Feb, Mar....) then it won't work.
Essentially what I'm trying to get is a master list of all the names in column C from each month for a summary tab listing all the names from the various months. Depending on the month I run this the file will only have sheets for the months that have occurred.
Below is my code:
'First Tab
Columns("C:C").Select
Selection.Copy
Columns("O:O").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
With ActiveWorkbook.Worksheets("Jan").Sort
.SetRange Range("O1:O1590")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
Range("o:o").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort key1:=Range("o:o", Range("o:o").End(xlDown)), _
order1:=xlAscending, Header:=xlNo
'Add the managers to the next sheet
Range("O1").Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("p1").Select
ActiveSheet.Paste
End With
ActiveSheet.Next.Select
'''''''''''''''''''''
'''''''''''''''''''''
Do
Columns("C:C").Select
Selection.Copy
Columns("O:O").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
With ActiveWorkbook.Worksheets("Jan").Sort
.SetRange Range("O1:O1590")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
Range("o:o").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort key1:=Range("o:o", Range("o:o").End(xlDown)), _
order1:=xlAscending, Header:=xlNo
'Add the names to the next sheet
Range("O1").Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("p1").Select
' Selection.End(xlDown).Select
' Selection.End(xlUp).Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
Range("O1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
Range("O1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1).Select
ActiveSheet.Paste
Selection.End(xlDown).Select
Selection.End(xlDown).Select
End With
If ActiveSheet.Next.Name = "Summary" Then
Exit Do
ElseIf ActiveSheet.Index <> Sheets.Count Then
ActiveSheet.Next.Select
Else
Exit Do
End If
Loop
ActiveSheet.Next.Select
Range("A1").Select
Sheets("Summary").Select
ActiveSheet.Previous.Select
Columns("O:O").Select
Selection.Copy
ActiveSheet.Next.Select
ActiveWindow.ScrollColumn = 2
Columns("AC:AC").Select
ActiveSheet.Paste
Range("AC2").Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.ScrollColumn = 1
Range("A3").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'ActiveSheet.Range("$A$2:$A$43").RemoveDuplicates Columns:=1, Header:=xlYesActiveSheet.Range.Cells("a1").Select
Sheets("Guide").Select
End Sub
You can use Format() to get the worksheet names.
Below is an example of using it for your purpose. You will need to modify your code to work with inputting either the Worksheet name as String or the Worksheet Object itself. sName is what you are stuck on from this post. Example here uses the Worksheet Object Reference.
Option Explicit
Sub ProcessAllMonthsWorksheet()
Dim iMonth As Integer, iYear As Integer, sName As String
Dim oWS As Worksheet
iYear = Year(Date)
On Error Resume Next
For iMonth = 1 To 12
sName = Format(DateSerial(iYear, iMonth, 1), "mmm")
Debug.Print "sName: " & sName
Set oWS = ThisWorkbook.Worksheets(sName)
If Not oWS Is Nothing Then ProcessMonthWorksheet oWS
Set oWS = Nothing
Next
End Sub
Private Sub ProcessMonthWorksheet(ByRef WorksheetObject As Worksheet)
Debug.Print "Processing worksheet """ & WorksheetObject.Name & """"
With WorksheetObject
' do your stuff with the worksheet
End With
End Sub

Excel VBA - vlookup for a variable number of rows within a macro

I've created a macro to organize a data set and compile into another sheet in a way that makes more sense for doing analyses. The set originally is comprised of columns for user, timestamp and 3 possible events. The user could appear on multiple rows but I wanted to look at this data set by user and have a separate column for each timestamp. The macros I've made can successfully clean, filter by event type, and separate by event type into separate worksheets (no matter how many rows of data) but I'm having trouble with compiling data into one sheet using vlookup AND accounting for a variable number of rows. I have looked at other answers to this question and tried this:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R" & LastRow0 & "C3,2,FALSE)"
... but it keeps giving me errors.
What I have below (Vlookup_events2) works but just not for the entire variable number of rows.Please help me adjust the code for the vlookup so it will work no matter how many rows.
Here is the code below for separating data (just for reference), then the problem macro - compiling it with vlookup. I would really appreciate some help, I know there's an amazing VBA expert out there!
Sheets.Add
Sheets("Sheet1").Name = "Email Sent"
ActiveSheet.Next.Select
Selection.AutoFilter
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Email Sent"
ActiveCell.Offset(0, -2).Range("A1:D2355").Select
ActiveCell.Activate
Selection.Copy
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveSheet.Next.Select
Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Email Opened"
Sheets.Add
Sheets("Sheet2").Name = "Email Opened"
ActiveSheet.Next.Select
ActiveCell.Range("A1:D1000000").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(0, 3).Range("A1").Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveCell.Columns("A:A").EntireColumn.Select
Selection.Delete Shift:=xlToLeft
Sheets.Add
Sheets("Sheet3").Name = "Clicked Link"
ActiveSheet.Next.Select
ActiveSheet.Next.Select
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Clicked Link"
ActiveCell.Offset(0, -2).Range("A1:D1000000").Select
ActiveCell.Activate
Selection.Copy
ActiveSheet.Previous.Select
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(0, 3).Columns("A:A").EntireColumn.Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
Sub Vlookup_events2()
' Vlookup_events2 Macro
ActiveSheet.Previous.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
ActiveSheet.Next.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
ActiveSheet.Next.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
Sheets.Add
Sheets("Sheet4").Name = "Compiled Events"
ActiveSheet.Previous.Select
ActiveSheet.Previous.Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveSheet.Next.Select
ActiveSheet.Next.Select
Range("A1").Select
ActiveSheet.Paste
Columns("C:D").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("B1").Select
ActiveCell.FormulaR1C1 = "Email Sent Time"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Email Opened Time"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Clicked Link Time"
Range("A1").Select
Application.Goto Reference:="R2C3"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R601C3,2,FALSE)"
Range("C3").Select
Range(Selection, Selection.End(xlUp)).Select
Columns("C:C").Select
Selection.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R601C3,2,FALSE)"
Columns("D:D").Select
Selection.FormulaR1C1 = "=VLOOKUP(RC[-3],'Clicked Link'!R1C1:R56C3,2,FALSE)"
Columns("C:C").Select
Selection.NumberFormat = "m/d/yyyy h:mm"
Columns("D:D").Select
Selection.NumberFormat = "m/d/yyyy h:mm"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Email Opened Time"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Clicked Link Time"
Range("C2").Select
End Sub

autofilter to exclude dates with "or" operator coming up with incomplete result

Sub DOBdateRange()
Dim Bfordate As Date
Dim Afterdate As Date
Bfordate = Worksheets("error").Range("i5").Value
Afterdate = Worksheets("error").Range("j5").Value
Application.ScreenUpdating = False
'çhange data formate to Date
Worksheets("data").Select
Worksheets("data").Range("a2", Range("a" & Rows.count).End(xlUp)).Offset(, 3).Select
Selection.Name = "DOB"
Selection.NumberFormat = "d/mm/yyyy"
'filter and copy how many records match (exclude) date criteria,
Worksheets("Data").Select
Range("bq1").Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.AutoFilter
ActiveSheet.Range(Selection, Selection.End(xlDown)).AutoFilter _
**Field:=4, _
Criteria1:="<" & Bfordate, _
Operator:=xlOr, _
Criteria2:=">" & Afterdate**
Range("a1").Select
Range("bq1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy Set NewSheet = Sheets.Add(After:=Worksheets("error"))
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
On Error GoTo duplicatesheet3
NewSheet.Name = "DOBdateRange"
On Error GoTo 0
NewSheet.Range("A1").Select
Application.CutCopyMode = False
Worksheets("dobdaterange").Range("d:d").Select
Selection.NumberFormat = "d/mm/yyyy"
Application.CutCopyMode = False
Worksheets("dobdaterange").Range("a1").Select
Range(Selection, Selection.End(xlToRight)).EntireColumn.AutoFit
With ActiveSheet.UsedRange.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Worksheets("Data").Select
Worksheets("Data").Range("A1").Select
Selection.AutoFilter 'Remove auto filter
Sheets("DOBdateRange").Select
Exit Sub
Previously I was using range. Value to refer to date criteria since it wasn't working I thought its not picking up date format so I declared the dates as date, still its not filtering all the data, its generating incomplete result, after much searching and trying different things i decided to post it for direction. any help pointing to right direction will be highly appreciated. Thank you

Excel VBA - Nested Do While Loop Not Incrementing

I have nested "Do While" loops near the bottom of the below code that are not incrementing. I have stepped through the code, and confirmed that once a non-zero value is found in cell E37 of the "Outages" tab, the code continuously finds a solution for that value instead of incrementing the company code. The company and trading partner numbers are in a matrix from B2:AE31. This is an accounting application to figure out which intercompany accounts do not balance by company and trading partner. Basically, this macro needs to loop through all combination of values for company code and trading partner (1:27 for each). Any help you can give would be appreciated.
'4 - Identify outages in table (loop through)
Dim i As Integer
Dim j As Integer
Dim CO As String
Dim TP As String
Dim MO As Integer
Dim SolverValue As Double
i = 1 'Company code
j = 1 'Trading partner
MO = Sheets("Inputs").Range("B1").Value2
Do While i < 28
Range("E34").Value2 = i
j = 1
Do While j < 28
Range("E35").Value2 = j
Sheets("Outages").Select
If Range("E37").Value2 <> 0 Then
CO = Range("E34").Value2
TP = Range("E35").Value2
'4a - Run solver for companies if an outage is found
ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Solver"
Sheets("Transactions").Select
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$R$10000").AutoFilter Field:=2, Criteria1:=MO
ActiveSheet.Range("$A$1:$R$10000").AutoFilter Field:=9, Criteria1:=CO, _
Operator:=xlOr, Criteria2:=TP
ActiveSheet.Range("$A$1:$R$10000").AutoFilter Field:=11, Criteria1:=CO, _
Operator:=xlOr, Criteria2:=TP
ActiveSheet.Range("$A$1:$R$10000").AutoFilter Field:=18, Criteria1:="1"
Sheets("Transactions").Select
Rows("1:10000").Select
Selection.Copy
Sheets("Solver").Select
Rows("1:1").Select
ActiveSheet.Paste
Columns("A:A").EntireColumn.AutoFit
Cells.Select
Cells.EntireColumn.AutoFit
Range("Q1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=+SUM(R[1]C:R[201]C)"
Range("Q2").Select
ActiveWindow.SmallScroll Down:=-18
ActiveCell.FormulaR1C1 = "=+RC[-3]*RC[-1]"
Range("Q2").Select
Selection.Copy
Range("Q3:Q203").Select
ActiveSheet.Paste
Range("P2").Select
Application.CutCopyMode = False
Selection.Copy
Range("P3:P203").Select
ActiveSheet.Paste
Range("R1").Select
ActiveWindow.SmallScroll ToRight:=4
Sheets("Outages").Select
Range("E37").Select
Selection.Copy
Sheets("Solver").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Copy
Selection.Style = "Comma"
SolverReset
SolverValue = Sheets("Outages").Range("E37")
SolverOk SetCell:="$Q$1", MaxMinVal:=3, ValueOf:=SolverValue, ByChange:= _
"$P$2:$P$201", Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:="$P$2:$P$201", Relation:=5, FormulaText:="binary"
SolverSolve True
Columns("P:R").Select
Columns("P:R").EntireColumn.AutoFit
'4b - Copy entries causing outages to a list
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$W$201").AutoFilter Field:=16, Criteria1:="1.00"
Range("A2:Q1000").Select
Selection.Copy
Sheets("Transactions Causing Outages").Select
Range("A2").Select
ActiveSheet.Paste
Columns("N:Q").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Cells.Select
Cells.EntireColumn.AutoFit
'4c - Delete Solver tab
Application.DisplayAlerts = False
Worksheets("Solver").Delete
Application.DisplayAlerts = True
Worksheets("Transactions").ShowAllData 'Unfilter the transactions tab
End If
j = j + 1
Loop
i = i + 1
Loop
Sheets("Outages").Select was out of place.

Resources