Overflow fault 6 VBA While-loop - vba

First of all thanks for helping me.
I programmed a Sub and I always get an Overflow fault 6. Its in the line where it says While x<160. I cannot do more then 160. E.g. If I do 170 Ill get the fault. But I would like to do While x<1000
The goal is to sum up duration times. Eg. On 1/2/2017 we had 2 downtime for electrical reason, first was 2 minutes, second was 10 minutes.
This macro is making a summary for the 1/2/17 saying that we had 12min downtime because electrical reasons.
I don't do that in a professional way, but this would make my work so much easier! Its working. I only get this annoying Overflow fault if I want to write in large numbers.
Sub sumuptheduration()
'Then sum up all the duration times
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim x As Long
Dim summe As Variant
Dim valuecells As Range
a = 1
b = 2
c = 0
x = 1
Set valuecells = Worksheets(1).Range("P2")
Do While Worksheets(1).Range("C" & b).Value = Worksheets(1).Range("C" & b+a).Value And Worksheets(1).Range("J" & b).Value = Worksheets(1).Range("J" & b + a).Value
Set valuecells = Worksheets(1).Range("P" & b & ":P" & a + b)
a = a + 1
Loop
Worksheets(1).Range("S" & a + (b - 1)).Value = Application.WorksheetFunction.Sum(valuecells)
b = a + 2
c = a + 2
a = 1
Dim y As Integer
y = 0
While x < 160
Do While Worksheets(1).Range("C" & b).Value = Worksheets(1).Range("C" & b + a).Value And Worksheets(1).Range("J" & b).Value = Worksheets(1).Range("J" & b + a).Value
Set valuecells = Worksheets(1).Range("P" & b & ":P" & c + a)
a = a + 1
Loop
Worksheets(1).Range("S" & c + (a - 1)).Value = Application.WorksheetFunction.Sum(valuecells) 'Writes the Sum into the correct Cell (last one of combination)
'This if clause checks if there are "Single combinations" because single combinations do not go through the do while at the top
If Worksheets(1).Range("J" & b).Value <> Worksheets(1).Range("J" & b + a).Value And Worksheets(1).Range("J" & b).Value <> Worksheets(1).Range("J" & b + (a - 2)).Value Or Worksheets(1).Range("C" & b).Value <> Worksheets(1).Range("C" & b + a).Value And Worksheets(1).Range("C" & b).Value <> Worksheets(1).Range("C" & b + (a - 2)).Value Then
Worksheets(1).Range("S" & b).Value = Worksheets(1).Range("P" & b).Value
Else
End If
'Change the variables into correct way for the next passing of the do while clause
x = x + 1 'causes that do while is not just one time passes
b = c + a
c = c + a 'temporary variable to not forget the old b during the next passing of the do while
a = 1 'at the end of every passing a tells you how much times you needed to pass the do while, so you have to reset it every time
Wend
End Sub
here

Change the declarations as follows:
Dim a As Long
Dim b As Long
Dim c As Long
Do not declare these variables as Integer

Related

Creating a module that Randomizes non-numeric values in Excel-VBA 2016

Background: I have a table with rows of Staff IDs and columns of shifts (two a day: AM and PM). Staff indicate whether they can attend each shift. I then run a module that generates lists of all IDs who can attend each shift.
Question: Is there a module that can take that list of potential attendees for each shift and generate four random IDs for each AM shift and three random IDs for each PM shift?
The PM shift should not have the same IDs as the AM shift for each day.
Image 1: enter link description here
Image 2: enter link description here
This will do it - It's flexible on the number of employees available (can be 10, can be 50!) but it does assume 3 things:
The list of employees starts on row 3 with no blanks in this list
The Monday - Friday column C to column L
The 1st row will indicate whether it's an AM or PM shift
Option Explicit
Sub Work_timetables()
Dim c As Integer, R As Long, iEmployees As Long, ID As String, iField As Integer, bAM As Boolean, lRandomNumber As Long, iNumbersNeeded As Integer, iPicked As Integer
Application.ScreenUpdating = False
c = 3
R = 2
iField = 1
iEmployees = Range("B3:B" & Range("B3").End(xlDown).Row).Rows.Count
Do Until c > 12
Range("C2:L" & iEmployees + 2).AutoFilter Field:=iField, Criteria1:="Yes"
Range("B3:B" & Range("B3").End(xlDown).Row).Copy
Cells(iEmployees + 4, c).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("C2:L" & iEmployees).AutoFilter
c = c + 1
iField = iField + 1
Loop
c = 3
Start:
Do Until c > 12
If c Mod 2 = 0 Then
bAM = False
iNumbersNeeded = 3
End If
If Not c Mod 2 = 0 Then
bAM = True
iNumbersNeeded = 4
End If
If (Cells(1048563, c).End(xlUp).Row - (iEmployees + 3)) < iNumbersNeeded Then
MsgBox "There isn't enough emplooyees available for the " & Cells(2, c).Value & " (" & Cells(1, c).Value & ") shift" & vbNewLine & vbNewLine & "Moving to next shift", vbOKOnly, "Short staffed!"
c = c + 1
GoTo Start
End If
Do Until iPicked = iNumbersNeeded
goLoop:
lRandomNumber = WorksheetFunction.RandBetween(iEmployees + 4, Cells(iEmployees + 4, c).End(xlDown).Row)
If Trim(Range("B" & lRandomNumber).Value) = "" Then
Range("B" & lRandomNumber).Value = "Picked"
Cells(Range("C" & iEmployees + 4).CurrentRegion.Rows.Count + iEmployees + 6 + iPicked, c).Value = Cells(lRandomNumber, c)
iPicked = iPicked + 1
Else
GoTo goLoop
End If
Loop
Range("B" & iEmployees + 4 & ":B" & Range("C" & iEmployees + 4).CurrentRegion.Rows.Count + iEmployees + 4).ClearContents
c = c + 1
iPicked = 0
Loop
Application.ScreenUpdating = True
End Sub

Excel VBA Copy Paste Values with conditions

I'm trying to copy values from one small sheet "MD with ID" to A Larger sheet "D with ID" if 2 fields are identical (consider those two as keys that identify each record).
Here is my first try:
Sub CopyIDCells()
Set i = Sheets("MD with ID")
Set e = Sheets("D with ID")
Dim d
Dim j
d = 1
j = 2
Do Until IsEmpty(e.Range("B" & j))
d = 2
Do Until IsEmpty(i.Range("A" & d))
If e.Range("C" & j).Value = i.Range("D" & d).Value Then
If e.Range("M" & j).Value = i.Range("J" & d).Value Then
e.Range("A" & j).Value = i.Range("B" & d).Value
End If
End If
d = d + 1
Loop
j = j + 1
Loop
End Sub
Here is my second try:
Sub CopyIDCells2()
Set i = Sheets("MD with ID")
Set e = Sheets("D with ID")
Dim d
Dim j
d = 1
j = 2
Do Until j = 20886
d = 2
Do Until d = 1742
If e.Cells(j, 3).Value = i.Cells(d, 4).Value Then
If e.Cells(j, 13).Value = i.Cells(d, 10).Value Then
e.Cells(j, 1).Value = i.Cells(d, 2).Value
End If
End If
d = d + 1
Loop
j = j + 1
Loop
End Sub
Nothing changes in the excel sheet when this code runs, although it takes few minutes to run -_-".
.. sample was removed
So looking at your first CopyIdCells method, there is only one fix I would make to this - make variable d=2. This has headers at the top of your sample data and you need to start on row 2 just like the other sheet.
Sub CopyIDCells()
Set i = Sheets("MD with ID")
Set e = Sheets("D with ID")
Dim d
Dim j
d = 2
j = 2
Do Until IsEmpty(e.Range("B" & j))
d = 2
Do Until IsEmpty(i.Range("A" & d))
If e.Range("C" & j).Value = i.Range("D" & d).Value Then
If e.Range("M" & j).Value = i.Range("J" & d).Value Then
e.Range("A" & j).Value = i.Range("B" & d).Value
End If
End If
d = d + 1
Loop
j = j + 1
Loop
End Sub
Other than that your formulas look good, you just do not have any data that meets your requirements. Add this column to the bottom of "MD with ID" and you will see your code match.
mouse 10 08 11267 A/J M 823 1/11/2008 1 SC-807 LONG 10/10/2005
Since you are matching on "Case Number" AND "Other ID" there are no items in both sheets that meet this criteria. When you add the row above to "MD with ID", you will see the appropriate ID added to your second sheet on several rows.

Find Copy and Paste in VBA macro

I am trying to write a macro which search data from one sheet and copy's to another.
But now I have a problem because I want to copy data between two searches and paste the whole data from multiple cells into one single cell.
For example in the above picture my macro:
SEARCH for "--------------" and "*****END OF RECORD"
COPIES everything in between , here example data in row 29 and 30 and from column A,B,C
PASTE all the data from multiple cells A29,B29,C29 and then A30,B30,C30 to single cell in sheet 2 say cell E2.
This pattern is reoccurring in the column A so I want to search for the next occurrence and do all the steps 1,2,3 and this time I will paste it in Sheet2 , cell E3.
Below is the code:
I am able to search my pattern but hard time in giving references to the cells in between those searched patterns and then copying all the data to ONE cell.
x = 2: y = 2: Z = 7000: m = 0: n = 0
Do
x = x + 1
If ThisWorkbook.Sheets("lic").Range("A" & x) = "---------------------" Then m = x
If ThisWorkbook.Sheets("lic").Range("A" & x) = "****** END OF RECORD" Then n = x
If (n > 0) Then
Do
For i = m To n
ThisWorkbook.Sheets("lic").Range("A" & i + 1).Copy
ThisWorkbook.Sheets("lic").Range("B" & i + 1).Copy
ThisWorkbook.Sheets("lic").Range("C" & i + 1).Copy
'If (n > 0) Then ThisWorkbook.Sheets("Sheet1").Range("E" & y) = ThisWorkbook.Sheets("lic").Range("A" & m + 1, "C" & n - 1): y = y + 1
'If (n > 0) Then ThisWorkbook.Sheets("Sheet1").Range("E" & y).Resize(CopyFrom.Rows.Count).Value = CopyFrom.Value: y = y + 1
Loop While Not x > Z
'Driver's Licence #:Driver's Licence #:Driver's Licence #:
x = 2: y = 2: Z = 7000: counter = 1
Do
x = x + 1
If ThisWorkbook.Sheets("lic").Range("A" & x) = "Driver's Licence #:" Then counter = counter + 1
If (counter = 2) Then ThisWorkbook.Sheets("Sheet1").Range("B" & y) = ThisWorkbook.Sheets("lic").Range("C" & x): y = y + 1: counter = 0
If x = Z Then Exit Sub
Loop
End Sub
Considering that the search is working correctly, about the copy thing you just need to do:
Sheet2.Range("E2").value = ThisWorkbook.Sheets("lic").Range("A" & i + 1).value & ";" & ThisWorkbook.Sheets("lic").Range("B" & i + 1).value & ";" & ThisWorkbook.Sheets("lic").Range("C" & i + 1).value
The result will be something like: AIR COO; L DAT; A
--------UPDATE---------
It was hard to understand your code, so I'm write a new one. Basically it's copy what it found on sheet1 to sheet2.
Sub Copy()
Dim count As Integer 'Counter of loops to the for
Dim Z As Integer 'Limit of (?)
Dim h As Integer 'Count the filled cells on sheet2
Dim y As Integer 'Counter the columns to be copied
Z = 7000
h = 1
'Assuming that the "----" will always be on the top, the code will start searching on the second row
'if it's not true, will be needed to validate this to.
For count = 2 To Z
If Sheet1.Cells(count, 1).Value <> "****** END OF RECORD" Then
If Sheet1.Cells(count, 1).Value <> "" Then
For y = 1 To 3 'In case you need to copy more columns just adjust this for.
Sheet2.Cells(h, 1).Value = Sheet2.Cells(h, 1).Value & Sheet1.Cells(count, y).Value
Next y
h = h + 1
End If
Else
MsgBox "END OF RECORD REACHED"
Exit Sub
End If
Next count
End Sub
Maybe I don't get the full idea but this might work for you.
I'm not at all sure what you want to see in the final output, so this is an educated guess:
Sub DenseCopyPasteFill ()
Dim wsFrom, wsTo As Worksheet
Dim ur As Range
Dim row, newRow As Integer
Dim dataOn As Boolean
Dim currentVal As String
dataOn = False
newRow = 3
Set wsFrom = Sheets("Sheet1")
Set wsTo = Sheets("Sheet2")
Set ur = wsFrom.UsedRange
For row = 1 To ur.Rows.Count
If wsFrom.Cells(row, 1).Value2 = "--------------" Then
dataOn = True
ElseIf wsFrom.Cells(row, 1).Value2 = "***** END OF RECORD" Then
newRow = newRow + 1
dataOn = False
ElseIf dataOn Then
currentVal = wsTo.Cells(newRow, 5).Value2
wsTo.Cells(newRow, 5).Value2 = currentVal & _
wsFrom.Cells(row, 1) & wsFrom.Cells(row, 2) & _
wsFrom.Cells(row, 3)
End If
Next row
End Sub
If you can get away without using the Windows clipboard, I would. Instead of copy/paste, here I demonstrated how you can simply add or append a value.
Add this sub:
Sub copy_range(rng As Range)
Dim str As String
str = rng.Cells(1).Value & rng.Cells(2).Value & rng.Cells(3).Value
Range("E" & Range("E" & Rows.Count).End(xlUp).Row + 1).Value = str
End Sub
Then your for loop should look like this:
For i = m To n
copy_range ThisWorkbook.Sheets("lic").Range("A" & i + 1 & ":C" & i + 1)
Next i

Create combinations of text and transforming these to binary in VBA

I'm working on a project where I need to combine 6 keywords to the maximum number of combinations. After this is done, I have to transform these combinations to a form of binary.
example:
Word1 = 100000
Word2 = 010000
Word3 = 001000
Word4 = 000100
Word5 = 000010
Word6 = 000001
And a combination could be: (Word1 Word2 Word5) = 110010
My 6 keywords are in column A2:A7.
If this could be done in VBA, it would make my job a lot easier.
Acording to basic math, 6 entries would combine to 64 combinations. I don't need the "blank" combination, why it'll total 63 combinations.
I'm fairly new to coding and have only worked in VBA for a couple of weeks, so I'm hoping that there might be a specialist out there that could help me with this problem.
Update!
This is what I have written so far. It only combines the words once:
Sub combinations()
Dim i As Long, j As Long, k As Long, l As Long, n As Long, m As Long, lr As Long
With ActiveSheet
lr = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
For j = i + 1 To lr
For k = j + 1 To lr
For l = k + 1 To lr
For n = l + 1 To lr
For m = n + 1 To lr
.Cells(Rows.Count, 6).End(xlUp).Offset(1, 0) = .Cells(i, 1).Value & " " & .Cells(j, 1).Value & " " & .Cells(k, 1).Value & " " & .Cells(l, 1).Value & " " & .Cells(n, 1).Value & " " & .Cells(m, 1).Value
Next m
Next n
Next l
Next k
Next j
Next i
End With
End Sub
Regards,
Emil
It would be easier to begin with the bitmap representation (BTW it would be best to avoid calling those "binary")
Dim a, b, c, d, e, f, i
i = 1
For a = 0 To 1
For b = 0 To 1
For c = 0 To 1
For d = 0 To 1
For e = 0 To 1
For f = 0 To 1
Cells(i, 1).Value = "'" & a & b & c & d & e & f
i = i + 1
Next f
Next e
Next d
Next c
Next b
Next a

Extract values from cell and copy entire row with each individual value

I have a column which consists of 3 digit codes referring to a branch. If a person is working in multiple departments, then he/she will have multiple codes referred in that column. Below is what it would look like.
Name | Branch
ABC | 423
MNO | 367325
XYZ | 414426429
I want it to look like this.
Name | Branch
ABC | 423
MNO | 367
MNO | 325
XYZ | 414
XYZ | 426
XYZ | 429
I want to extract the value of the cell, suppose let's say the string length is 9, then that person works for 3 branches. I want to extract those 3 values and duplicate the entire row with each row containing one branch number.
A few pointers : No person can work for more than 3 branches(so maximum string length will be 9). There are about 20 columns. The column which contains the branch codes is always the same i.e. column G. The column also has empty cells and other string values like 'BIKCJHGT'. The entire column is formatted as text.
Can any one please give me the VBA code to accomplish this?
Here is the code I've used. It hasn't thrown any errors, but it's not working either.
Option Explicit
Sub MultiRecords()
Dim b As Workbook
Set b = Workbooks.Open("C:\Users\uspola00\Desktop\Headcount_Final.xlsx")
ActiveWorkbook.Sheets("Headcount").Activate
ActiveSheet.Range("G1").Select
Dim ws As Worksheet
Set ws = Sheets("Headcount")
Dim intInsertRows As Integer
Dim i As Long
i = 1
Application.ScreenUpdating = False
Do Until i > ws.Range("G" & Rows.Count).End(xlUp).Row
Dim str As String
str = LTrim(RTrim(ws.Range("G" & i)))
If Len("G" & i) = 9 Then
intInsertRows = 2
Range("G" & i + 1 & ":G" & i + intInsertRows).EntireRow.Insert
Range("A" & i & ":N" & (i + intInsertRows)).FillDown
Range("G" & (i + intInsertRows)).Value = Right(str, 3)
Range("G" & i + 1).Value = Mid(str, 4, 3)
Range("G" & i).Value = Left(str, 3)
i = i + intInsertRows
ElseIf Len("G" & i) = 6 Then
intInsertRows = 1
Range("G" & i & ":G" & i + intInsertRows).EntireRow.Insert
Range("A" & i & ":N" & (i + intInsertRows)).FillDown
Range("G" & i + intInsertRows).Value = Right(str, 3)
Range("G" & i).Value = Left(str, 3)
i = i + intInsertRows
ElseIf Len("G" & i) = 3 Then
intInsertRows = 0
i = i + intInsertRows
ElseIf IsEmpty(Range("G" & i)) Then
i = i + 0
End If
i = i + 1
Loop
End Sub
Try this:
Sub MultiRecords()
Dim b As Workbook
Dim ws As Worksheet
Dim c As Range, v, i As Long
Set b = Workbooks.Open("C:\Users\uspola00\Desktop\Headcount_Final.xlsx")
Set ws = b.Sheets("Headcount")
Set c = ws.Cells(Rows.Count, "G").End(xlUp)
Do
v = Trim(c.Value)
If v Like "######" Or v Like "#########" Then
i = Len(v) / 3
c.Offset(1, 0).Resize(i - 1).EntireRow.Insert
c.Resize(i).EntireRow.FillDown
c.Value = Left(v, 3)
c.Offset(1, 0).Value = Mid(v, 4, 3)
If Len(v) = 9 Then c.Offset(2, 0).Value = Right(v, 3)
End If
If c.Row = 1 Then Exit Do
Set c = c.Offset(-1, 0)
Loop
End Sub