Excel VBA - Nested Do While Loop Not Incrementing - excel

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.

Related

Grouping records in an Excel sheet which have the same values in one column but only one unique record in other columns

Dummy data of a tournament
Above is the example of the dummy data. My goal is to use VBA to group the data so that there is only one name displayed and the 3 Games populated with the Results so there would only be one line for the name as well as the 3 Games' results in the same line.
Example of the output data
Well, this is not as easy as first appears, however, this works:
So, the country is returned with classic index & match. The results are built by finding the result against each player and round. This expects blanks in the other cells for each player.
Try this:
Sub mSummarise()
'
' Macro1 Macro
'
'
Dim lData, lSummary, lFilter As String
Dim lRow1, lRow2, lRow3, lCol1, lCount As Long
lData = ActiveSheet.Name
Range("A1").Select
Selection.End(xlToRight).Select
lCol1 = ActiveCell.Column
Range("A1").Select
Selection.End(xlDown).Select
lRow1 = ActiveCell.Row
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Summary"
Sheets(lData).Activate
Range("A1:B" & lRow1).Select
Selection.Copy
Sheets("Summary").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$B$" & lRow1).RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlNo
Range("A1").Select
Selection.End(xlDown).Select
lRow2 = ActiveCell.Row
Sheets(lData).Select
Range(Cells(1, 3), Cells(1, lCol1)).Select
Selection.Copy
Sheets("Summary").Select
Range("C1").Select
ActiveSheet.Paste
Sheets(lData).Select
For lCount = 3 To lCol1
Range(Cells(1, 1), Cells(lRow1, lCol1)).Select
Selection.AutoFilter
ActiveSheet.Range(Cells(1, 1), Cells(lRow1, lCol1)).AutoFilter Field:=lCount, Criteria1:="<>", Operator:=xlAnd
Range(Cells(1, 1), Cells(lRow1, lCount)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
lFilter = ActiveSheet.Name
ActiveSheet.Paste
Range("A1").Select
Selection.End(xlDown).Select
lRow3 = ActiveCell.Row
Sheets("Summary").Select
Application.CutCopyMode = False
Cells(2, lCount).Select
ActiveCell.Formula = "=VLOOKUP(A2," & lFilter & "!$A$2:" & Cells(lRow3, lCount).Address & "," & lCount & ",0)"
Range(Cells(2, lCount), Cells(2, lCount)).Copy
Range(Cells(2, lCount), Cells(lRow3, lCount)).Select
ActiveSheet.Paste
Range(Cells(2, lCount), Cells(lRow3, lCount)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(lFilter).Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets(lData).Select
Next
Selection.AutoFilter
Range("A1").Select
Sheets("Summary").Select
Range("A1").Select
End Sub

VBA changes date format

I have a table with data in it and run a macro to neaten things up and then adds a hyperlink to column G but the issue is when the macro has run, the date changes from:
https://websitenamehere.com//agentView/agentname#company/2021-11-08
to
https://websitenamehere.com//agentView/agentname#company/44508
In my table, I created in column H the column for "today" and then in column G is where it puts it all together but messing up the date part.
Here is my code which i am using. Any help would be appreciated.
Sub CleanFollowUps()
Dim Lrow As Integer
Dim lCol As Integer
Dim C As Range
Lrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(Lrow, lCol)), , xlYes).Name = "FollowUps"
Range("FollowUps[#All]").Select
ActiveSheet.ListObjects("FollowUps").TableStyle = ""
Range("F1").Select
ActiveCell.FormulaR1C1 = "Helper"
Range("F2").Select
ActiveCell.FormulaR1C1 = "=IF(SUM([#[Due today]]+[#Late])>0,""Yes"","""")"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Schedule"
Range("H1").Select
ActiveCell.FormulaR1C1 = "day"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Columns("H:H").Select
Selection.NumberFormat = "yyyy-mm-dd"
Range("H2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=""https://websitenamehere.com/agentView/""&[#Username]&""#company/""&[#day]"
Range("G2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
With Sheets("FU")
For Each C In .Range("G2:G" & .Range("G" & .Rows.Count).End(xlUp).Row)
.Hyperlinks.Add Anchor:=C, Address:=C.Value, SubAddress:=C.Value
Next C
End With
Application.DisplayAlerts = True
End Sub

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

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

Perform calculation on cells if they have data

I am a bit new to the macro's in excel and I am trying to find a way to adjust one of the macros I currently have in an excel file. I have a calculation that takes the columns D and E then subtracts D from E and adds it to the value of column B. here is the current code and also the sheet being used.
Sub InvAdj()
'
' InvAdj Macro
'
' Keyboard Shortcut: Ctrl+Shift+I
'
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "Quality"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]-RC[2]+RC[3]"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C33")
Range("C2:C33").Select
Columns("C:C").Select
Selection.Copy
Columns("B:B").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("D2:E33").Select
Selection.ClearContents
Range("F1").Select
End Sub
Not sure if this is what you are trying?
Sub InvAdj()
Dim ws As Worksheet
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Range("B1").Value = "Quality"
For i = 2 To 33
'~~> Check if all cells have data
If Len(Trim(.Range("B" & i).Value)) <> 0 And _
en(Trim(.Range("D" & i).Value)) <> 0 And _
en(Trim(.Range("E" & i).Value)) <> 0 Then
'B = B + (E - D)
.Range("B" & i).Value = .Range("B" & i).Value + _
(.Range("E" & i).Value - .Range("D" & i).Value)
End If
Next i
End With
End Sub

Resources