I have a raw data sheet and am trying to process nps scores from the emergency department (ANE) versus all others.
I am counting the values and transferring the number of promoters, detractors and passives onto a sheet which I can then calculate the net promoter score for that month.
I have four institutes which are being surveyed and it's run as such.
I receive
Compile error next without for.
Which End If is causing the error, or am I missing more End Ifs?
Dim UNITAOutpatientANEPromoter As Integer
Dim UNITBOutpatientANEPromoter As Integer
Dim UNITCOutpatientANEPromoter As Integer
Dim UNITDOutpatientANEPromoter As Integer
Dim ALLOutpatientANEPromoter As Integer
Dim UNITAOutpatientANEDetractor As Integer
Dim UNITBOutpatientANEDetractor As Integer
Dim UNITCOutpatientANEDetractor As Integer
Dim UNITDOutpatientANEDetractor As Integer
Dim ALLOutpatientANEDetractor As Integer
Dim UNITAOutpatientANEPassive As Integer
Dim UNITBOutpatientANEPassive As Integer
Dim UNITCOutpatientANEPassive As Integer
Dim UNITDOutpatientANEPassive As Integer
Dim ALLOutpatientANEPassive As Integer
Dim UNITAOutpatientOtherPromoter As Integer
Dim UNITBOutpatientOtherPromoter As Integer
Dim UNITCOutpatientOtherPromoter As Integer
Dim UNITDOutpatientOtherPromoter As Integer
Dim ALLOutpatientOtherPromoter As Integer
Dim UNITAOutpatientOtherDetractor As Integer
Dim UNITBOutpatientOtherDetractor As Integer
Dim UNITCOutpatientOtherDetractor As Integer
Dim UNITDOutpatientOtherDetractor As Integer
Dim ALLOutpatientOtherDetractor As Integer
Dim UNITAOutpatientOtherPassive As Integer
Dim UNITBOutpatientOtherPassive As Integer
Dim UNITCOutpatientOtherPassive As Integer
Dim UNITDOutpatientOtherPassive As Integer
Dim ALLOutpatientOtherPassive As Integer
UNITAOutpatientANEPromoter = 0
UNITBOutpatientANEPromoter = 0
UNITCOutpatientANEPromoter = 0
UNITDOutpatientANEPromoter = 0
ALLOutpatientANEPromoter = 0
UNITAOutpatientANEDetractor = 0
UNITBOutpatientANEDetractor = 0
UNITCOutpatientANEDetractor = 0
UNITDOutpatientANEDetractor = 0
ALLOutpatientANEDetractor = 0
UNITAOutpatientANEPassive = 0
UNITBOutpatientANEPassive = 0
UNITCOutpatientANEPassive = 0
UNITDOutpatientANEPassive = 0
ALLOutpatientANEPassive = 0
UNITAOutpatientOtherPromoter = 0
UNITBOutpatientOtherPromoter = 0
UNITCOutpatientOtherPromoter = 0
UNITDOutpatientOtherPromoter = 0
ALLOutpatientOtherPromoter = 0
UNITAOutpatientOtherDetractor = 0
UNITBOutpatientOtherDetractor = 0
UNITCOutpatientOtherDetractor = 0
UNITDOutpatientOtherDetractor = 0
ALLOutpatientOtherDetractor = 0
UNITAOutpatientOtherPassive = 0
UNITBOutpatientOtherPassive = 0
UNITCOutpatientOtherPassive = 0
UNITDOutpatientOtherPassive = 0
ALLOutpatientOtherPassive = 0
Dim mycount As Integer
mycount = Worksheets("1. Raw").Range("A1", Worksheets("1. Raw").Range("A1").End(xlDown)).Rows.Count - 1
'MsgBox (mycount)
If (mycount = 0 Or mycount = 1) Then
MsgBox ("Need raw data first")
Else 'mycount > 1
Dim i As Integer
'MsgBox (mycount)
For i = 1 To mycount
If (Worksheets("1. Raw").Cells(i + 1, 1)) = "OUTPATIENT" Then
'UNITA
If (Worksheets("1. Raw").Cells(i + 1, 2)) = "UNITA" Then
If (Worksheets("1. Raw").Cells(i + 1, 6)) = "A&E-A" Then
If (Worksheets("1. Raw").Cells(i + 1, 22)) = "Promoter" Then
UNITAOutpatientANEPromoter = UNITAOutpatientANEPromoter + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Detractor" Then
UNITAOutpatientANEDetractor = UNITAOutpatientANEDetractor + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Passive" Then
UNITAOutpatientANEPassive = UNITAOutpatientANEPassive + 1
End If
Else
If (Worksheets("1. Raw").Cells(i + 1, 22)) = "Promoter" Then
UNITAOutpatientOtherPromoter = UNITAOutpatientOtherPromoter + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Detractor" Then
UNITAOutpatientOtherDetractor = UNITAOutpatientOtherDetractor + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Passive" Then
UNITAOutpatientOtherPassive = UNITAOutpatientOtherPassive + 1
End If
End If
'UNITB
ElseIf (Worksheets("1. Raw").Cells(i + 1, 1)) = "UNITB" Then
If (Worksheets("1. Raw").Cells(i + 1, 6)) = "A&E-B" Then
If (Worksheets("1. Raw").Cells(i + 1, 22)) = "Promoter" Then
UNITBOutpatientANEPromoter = UNITBOutpatientANEPromoter + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Detractor" Then
UNITBOutpatientANEDetractor = UNITBOutpatientANEDetractor + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Passive" Then
UNITBOutpatientANEPassive = UNITBOutpatientANEPassive + 1
End If
Else
If (Worksheets("1. Raw").Cells(i + 1, 22)) = "Promoter" Then
UNITBOutpatientOtherPromoter = UNITBOutpatientOtherPromoter + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Detractor" Then
UNITBOutpatientOtherDetractor = UNITBOutpatientOtherDetractor + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Passive" Then
UNITBOutpatientOtherPassive = UNITBOutpatientOtherPassive + 1
End If
End If
'UNITC
ElseIf (Worksheets("1. Raw").Cells(i + 1, 1)) = "UNITC" Then
If (Worksheets("1. Raw").Cells(i + 1, 6)) = "A&E-C" Then
If (Worksheets("1. Raw").Cells(i + 1, 22)) = "Promoter" Then
UNITCOutpatientANEPromoter = UNITCOutpatientANEPromoter + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Detractor" Then
UNITCOutpatientANEDetractor = UNITCOutpatientANEDetractor + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Passive" Then
UNITCOutpatientANEPassive = UNITCOutpatientANEPassive + 1
End If
Else
If (Worksheets("1. Raw").Cells(i + 1, 22)) = "Promoter" Then
UNITCOutpatientOtherPromoter = UNITCOutpatientOtherPromoter + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Detractor" Then
UNITCOutpatientOtherDetractor = UNITCOutpatientOtherDetractor + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Passive" Then
UNITCOutpatientOtherPassive = UNITCOutpatientOtherPassive + 1
End If
End If
'UNITD
Else
If (Worksheets("1. Raw").Cells(i + 1, 1)) = "UNITD" Then
If (Worksheets("1. Raw").Cells(i + 1, 6)) = "A&E-D" Then
If (Worksheets("1. Raw").Cells(i + 1, 22)) = "Promoter" Then
UNITDOutpatientANEPromoter = UNITDOutpatientANEPromoter + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Detractor" Then
UNITDOutpatientANEDetractor = UNITDOutpatientANEDetractor + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Passive" Then
UNITDOutpatientANEPassive = UNITDOutpatientANEPassive + 1
End If
Else
If (Worksheets("1. Raw").Cells(i + 1, 22)) = "Promoter" Then
UNITDOutpatientOtherPromoter = UNITDOutpatientOtherPromoter + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Detractor" Then
UNITDOutpatientOtherDetractor = UNITDOutpatientOtherDetractor + 1
ElseIf (Worksheets("1. Raw").Cells(i + 1, 22)) = "Passive" Then
UNITDOutpatientOtherPassive = UNITDOutpatientOtherPassive + 1
End If
End If
End If
Next i
End If
All of those variables will give you a headache sooner or later: it would be easier to maintain a more-flexible approach such as below, using a Dictionary to collect the counts, keyed using a combination of the values you're interested in.
Sub Tester()
Dim wsRaw As Worksheet
Dim i As Integer, valUnit, valType, AandE As String, dict As Object, k, lastRow As Long
Set dict = CreateObject("scripting.dictionary")
Set wsRaw = Worksheets("1. Raw")
lastRow = wsRaw.Cells(Rows.Count, "A").End(xlUp).Row
'MsgBox (mycount)
If lastRow < 2 Then
MsgBox ("Need raw data first")
Exit Sub
End If
For i = 2 To lastRow
If wsRaw.Cells(i, "A") = "OUTPATIENT" Then
valUnit = Trim(wsRaw.Cells(i, "B").Value)
valType = Trim(wsRaw.Cells(i, 22).Value)
AandE = IIf(Trim(wsRaw.Cells(i, 6).Value) = "A&E-A", "A&E", "NonA&E") 'is this A&E ?
Select Case valUnit
Case "UNITA", "UNITB", "UNITC", "UNITD" 'counting this unit?
Select Case valType
Case "Promoter", "Detractor", "Passive" 'counting this type?
k = valUnit & "-" & valType & "-" & AandE 'create key for dictionary
dict(k) = dict(k) + 1 'increment count for key
End Select
End Select
End If 'outpatient
Next i
'output all the collected keys and counts
For Each k In dict
Debug.Print k, dict(k)
Next k
End Sub
Related
I would like to know if it would be possible to use the IFERROR, INDEX, MATCH function on below scenario.
D2:=INDEX($A$2:$A$16, MATCH(0, COUNTIF($D$1:$D1, $A$2:$A$16), 0))
E2: =IFERROR(INDEX($B$2:$B$16, MATCH(0, COUNTIF($D2:D2,$B$2:$B$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
H2: =IFERROR(INDEX($C$2:$C$16, MATCH(0, COUNTIF($D2:D2,$C$2:$C$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
I2: =IFERROR(INDEX($C$2:$C$16, MATCH(0, COUNTIF($D2:H2,$C$2:$C$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), "")
Based on the data of Countries and Cities filled in yellow on the left, by using the IFERROR, INDEX, MATCH formula I managed to get all the data I need. Now if there are more than 3 City, I want for the excel to continue the list of cities by creating another row under it as example of row filled in red.
I hope it makes sence. Let me know if it's possible.
You did tag vba as well as excel-formula so give this a try
Sub condense()
Dim src, dest(), ws As Worksheet, srcRange As Range, i As Long, j As Long, countryCount As Long, rowNum As Long
Set ws = ActiveSheet
Set srcRange = ws.Cells(1, 1).Resize(ws.Cells(ws.Rows.Count, 1).End(xlUp).Row, 3)
src = srcRange.Value2
ReDim dest(1 To UBound(src, 1) - 1, 1 To 7)
rowNum = 1
i = 2
Do While i <= UBound(src, 1)
countryCount = Application.CountIf(srcRange.Columns(1), src(i, 1))
For j = 1 To countryCount
dest(rowNum + Int((j - 1) / 3), 1) = src(i + j - 1, 1)
dest(rowNum + Int((j - 1) / 3), 2 + ((j - 1) Mod 3)) = src(i + j - 1, 2)
dest(rowNum + Int((j - 1) / 3), 5 + ((j - 1) Mod 3)) = src(i + j - 1, 3)
Next j
i = i + countryCount
rowNum = rowNum + 1 + Int((countryCount - 1) / 3)
Loop
ws.Cells(2, 4).Resize(rowNum, 7).Value2 = dest
With ws.Cells(1, 4).Resize(1, 7)
.Value2 = Strings.Split("Country,City1,City2,City3,Image1,Image2,Image3", ",")
.EntireColumn.AutoFit
End With
End Sub
EDIT 17-Jul-2022 (per comment from OP)
Sub condenseInto4cols()
Dim src, dest(), ws As Worksheet, srcRange As Range, i As Long, j As Long, countryCount As Long, rowNum As Long
Set ws = ActiveSheet
Set srcRange = ws.Cells(1, 1).Resize(ws.Cells(ws.Rows.Count, 1).End(xlUp).Row, 3)
srcRange.Sort key1:=ws.Cells(2, 1), order1:=xlAscending, Header:=xlYes
src = srcRange.Value2
ReDim dest(1 To UBound(src, 1) - 1, 1 To 9)
rowNum = 1
i = 2
Do While i <= UBound(src, 1)
countryCount = Application.CountIf(srcRange.Columns(1), src(i, 1))
For j = 1 To countryCount
dest(rowNum + Int((j - 1) / 4), 1) = src(i + j - 1, 1)
dest(rowNum + Int((j - 1) / 4), 2 + ((j - 1) Mod 4)) = src(i + j - 1, 2)
dest(rowNum + Int((j - 1) / 4), 6 + ((j - 1) Mod 4)) = src(i + j - 1, 3)
Next j
i = i + countryCount
rowNum = rowNum + 1 + Int((countryCount - 1) / 4)
Loop
ws.Cells(2, 4).Resize(rowNum, 9).Value2 = dest
With ws.Cells(1, 4).Resize(1, 9)
.Value2 = Strings.Split("Country,City1,City2,City3,City4,Image1,Image2,Image3,Image4", ",")
.EntireColumn.AutoFit
End With
srcRange.Sort key1:=ws.Cells(2, 2), order1:=xlAscending, Header:=xlYes
End Sub
I am working on a module that is running calculations of demand for a panelboard. The function created exports a value for demand. I am trying to pull values that were defined in the calculation. For example the value that is assigned to totalrecep how would I be able to call that in another sub?
I want to be able to call some of the values that are defined in the demand function.
Function Demand(myRange As Range)
Application.Volatile ' this causes the sheet to automatically update
Dim LoadtypeA, LoadtypeB, LoadtypeC, Load1 As Excel.Range 'defines the ranges
Dim newdemand As Double
totalrecep = 0
totaldemand = 0
motordemanda = 0
recepdemanda = 0
Contdemanda = 0
otherdemanda = 0
motordemandb = 0
recepdemandb = 0
Contdemandb = 0
otherdemandb = 0
motordemandc = 0
recepdemandc = 0
Contdemandc = 0
otherdemandc = 0
motorMAX = 0
cont25 = 0
motor25 = 0
recepdeduct = 0
Poles = 84
'===========================================================================================
'This is for the first section of the panel
'===========================================================================================
'Set Load1 = Worksheets(ActiveSheet.Name).Range("F7:F48") 'gets the range
Set Load1 = myRange
'This is for the left side of the panel
For x = 1 To (Poles / 2)
LoadtypeA = Load1(x)
LoadtypeB = Load1(x + 1)
LoadtypeC = Load1(x + 2)
'MsgBox Loadtype
'----------------------------------------------------------------
Select Case LoadtypeA
Case "R"
'MsgBox "R"
recepdemanda = recepdemanda + Load1(x, 2)
Case "M"
'MsgBox "m"
motordemanda = motordemanda + Load1(x, 2)
If Load1(x, 2) > motorMAXa Then
motorMAXa = Load1(x, 2)
End If
Case "C"
'MsgBox "C"
Contdemanda = Contdemanda + Load1(x, 2)
Case "N"
'MsgBox "N"
otherdemanda = otherdemanda + Load1(x, 2)
End Select
'----------------------------------------------------------------
Select Case LoadtypeB
Case "R"
'MsgBox "R"
recepdemandb = recepdemandb + Load1(x + 1, 2)
Case "M"
'MsgBox "M"
motordemandb = motordemandb + Load1(x + 1, 2)
If Load1(x + 1, 2) > motorMAXb Then
motorMAXb = Load1(x + 1, 2)
End If
Case "C"
'MsgBox "C"
Contdemandb = Contdemandb + Load1(x + 1, 2)
Case "N"
'MsgBox "N"
otherdemandb = otherdemandb + Load1(x + 1, 2)
End Select
'----------------------------------------------------------------
Select Case LoadtypeC
Case "R"
'MsgBox "R"
recepdemandc = recepdemandc + Load1(x + 2, 2)
Case "M"
'MsgBox "M"
motordemandc = motordemandc + Load1(x + 2, 2)
If Load1(x + 2, 2) > motorMAXc Then
motorMAXc = Load1(x + 2, 2)
End If
Case "C"
'MsgBox "C"
Contdemandc = Contdemandc + Load1(x + 2, 2)
Case "N"
'MsgBox "N"
otherdemandc = otherdemandc + Load1(x + 2, 2)
End Select
'----------------------------------------------------------------
x = x + 2
Next x
'----------------------------------------------------------------
' End of Left side of panel
'----------------------------------------------------------------
' Begin right side of panel
For x = 1 To (Poles / 2)
LoadtypeA = Load1(x, 11)
LoadtypeB = Load1(x + 1, 11)
LoadtypeC = Load1(x + 2, 11)
'MsgBox Loadtype
'----------------------------------------------------------------
Select Case LoadtypeA
Case "R"
'MsgBox "R"
recepdemanda = recepdemanda + Load1(x, 10)
Case "M"
'MsgBox "M"
motordemanda = motordemanda + Load1(x, 10)
If Load1(x, 10) > motorMAXa Then
motorMAXa = Load1(x, 10)
End If
Case "C"
'MsgBox "C"
Contdemanda = Contdemanda + Load1(x, 10)
Case "N"
'MsgBox "N"
otherdemanda = otherdemanda + Load1(x, 10)
End Select
'----------------------------------------------------------------
Select Case LoadtypeB
Case "R"
'MsgBox "R"
recepdemandb = recepdemandb + Load1(x + 1, 10)
Case "M"
'MsgBox "M"
motordemandb = motordemandb + Load1(x + 1, 10)
If Load1(x + 1, 10) > motorMAXb Then
motorMAXb = Load1(x + 1, 10)
End If
Case "C"
'MsgBox "C"
Contdemandb = Contdemandb + Load1(x + 1, 10)
Case "N"
'MsgBox "N"
otherdemandb = otherdemandb + Load1(x + 1, 10)
End Select
'----------------------------------------------------------------
Select Case LoadtypeC
Case "R"
'MsgBox "R"
recepdemandc = recepdemandc + Load1(x + 2, 10)
Case "M"
'MsgBox "M"
motordemandc = motordemandc + Load1(x + 2, 10)
If Load1(x + 2, 10) > motorMAXc Then
motorMAXc = Load1(x + 2, 10)
End If
Case "C"
'MsgBox "C"
Contdemandc = Contdemandc + Load1(x + 2, 10)
Case "N"
'MsgBox "N"
otherdemandc = otherdemandc + Load1(x + 2, 10)
End Select
'----------------------------------------------------------------
x = x + 2
Next x
'----------------------------------------------------------------
' End of Right side of panel
' Begin Demand Calcs
' Total Panel Calcs
'----------------------------------------------------------------
cont25 = (Contdemanda + Contdemandb + Contdemandc) * 0.25
motor25 = (motorMAXa + motorMAXb + motorMAXc) * 0.25
contdemand = Contdemanda + Contdemandb + Contdemandc
motordemand = motordemanda + motordemandb + motordemandc
totalrecep = recepdemanda + recepdemandb + recepdemandc
otherdemand = otherdemanda + otherdemandb + otherdemandc
If totalrecep > 10 Then
recepdemand = ((totalrecep - 10) / 2 + 10)
End If
If totalrecep < 10 Then
recepdemand = totalrecep
End If
newdemand = recepdemand + contdemand + motordemand + otherdemand + cont25 + motor25
'newdemand = Load1(1)
Demand = newdemand
'----------------------------------------------------------------
End Function
Create a public variable outside function and inside function
assigned your a public variable to local function variable.
then you can call a public variable from any place.
Option Explicit
Public x As String
Function myFun()
Dim name As String
name = "Hearno"
x = name
End Function
Sub Test()
Debug.Print x ' Hearno
End Sub
I'm working with userform to add data in a Table "t_database". For each checkbox = true, add a ROW.
When i add some data, appear "Run-time error '9' Subscript out of range..
To create this code, i used a post founded here and i completed with my requeriments.
Option Explicit
Private Sub cmdAddproject_Click()
Dim chkCnt As Integer
Dim ctl As MSForms.Control, i As Integer, lr As Long
Dim cb As MSForms.CheckBox
With Me
chkCnt = .Tool1.Value + .Tool2.Value + .Tool3.Value + .Tool4.Value + .Tool5.Value + .Tool6.Value + .Tool7.Value + .Tool8.Value + .Tool9.Value + .Tool10.Value + .Tool11.Value + .Tool12.Value + .Tool13.Value + .Tool14.Value + .Tool15.Value + .Tool16.Value + .Tool7.Value + .Tool18.Value + .Tool19.Value + .Tool20.Value + .Tool21.Value + .Tool22.Value + .Tool23.Value + .Tool24.Value + .Tool25.Value + .Tool26.Value + .Tool27.Value + .Tool28.Value + .Tool29.Value + .Tool30.Value
chkCnt = Abs(chkCnt)
If chkCnt <> 0 Then
ReDim mval(1 To chkCnt, 1 To 17)
i = 1
For Each ctl In .Controls
If TypeOf ctl Is MSForms.CheckBox Then
Set cb = ctl
If cb Then
mval(i, 1) = .txtProyecto.Value
mval(i, 2) = .txtAno.Value
mval(i, 3) = .txtEmpresa.Value
mval(i, 4) = .SectorEmpresa.Value
mval(i, 5) = .TipoEmpresa.Value
mval(i, 6) = .txtDireccion.Value
mval(i, 7) = .txtCiudad.Value
mval(i, 8) = .txtCodigoPostal.Value
mval(i, 9) = .txtPais.Value
mval(i, 10) = .txtDescripcion.Value
mval(i, 11) = .txtIndicador1.Value
mval(i, 12) = .metrica1.Value
mval(i, 13) = .txtIndicador2.Value
mval(i, 14) = .metrica2.Value
mval(i, 15) = cb.Caption
mval(i, 16) = .txtAhorrosPrevistos.Value
mval(i, 17) = .txtAhorrosObtenidos.Value
i = i + 1
End If
End If
Next
End If
End With
With Sheets("Database")
lr = .Range("A" & .Rows.Count).End(xlUp).Row + 1
.Range("A" & lr).Resize(UBound(mval, 1), 17) = mval
End With
End Sub
I did a vb code which is reading multiple text files from a folder and then parsing specific data from it. In the code I have hard coded a folderpath strPath = "C:\Users\smim\Desktop\Mim\Excel\". Now I would like to be able to choose the folder and files manually instead of hard coding the folder path. Here is my code :
Sub Parse()
Dim ws As Worksheet
Dim MyData As String, strData() As String
Dim WriteToRow As Long, i As Long
Dim strCurrentTxtFile As String
Dim count As Variant, yellow As Variant, red As Variant,
Dim YellowC As Variant,RedC As Variant, filecounter As Variant
Dim strPath As String
Application.ScreenUpdating = False
count = 0
red = 0
yellow = 0
YellowC = 0
RedC = 0
strPath = "C:\Users\smim\Desktop\Mim\Excel\"
'Set Book3 = Sheets("Sheet1")
Set ws = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
MsgBox ("Started")
'~~> Start from Row 1
'WriteToRow = 1
Cells(3, 1) = "Error"
Cells(3, 1).Interior.ColorIndex = 3
Cells(3, 2) = "Warnings"
Cells(3, 2).Interior.ColorIndex = 6
Cells(1, 3) = "Error"
Cells(1, 3).Interior.ColorIndex = 3
Cells(2, 3) = "Warnings"
Cells(2, 3).Interior.ColorIndex = 6
strCurrentTxtFile = Dir(strPath & "test_*.txt")
' MsgBox (strCurrentTxtFile)
'~~> Looping through all text files in a folder
Do While strCurrentTxtFile <> ""
Dim list() As String
'~~> Open the file in 1 go to read it into an array
Open strPath & strCurrentTxtFile For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbLf)
LineCount = UBound(strData)
' MsgBox (LineCount)
'Assigning length of the list array
ReDim Preserve list(LineCount + 1)
For x = 0 To (LineCount - 1)
'For x = LBound(strData) To UBound(strData)
'Parsing each line to get the result only ( after = sign)
s = Split(strData(x), "=")
b = UBound(s)
'MsgBox (s(1))
'Assigning Values to the list array
list(x) = s(1)
Next
'MsgBox ("This is list" & list(2))
'Active Cell 2
Range("A2").Activate
'Get row number
dblRowNo = ActiveCell.Row
'Get col number
dblColNo = ActiveCell.Column
'MsgBox (dblColNo)
' ReDim Preserve list(LineCount)
For i = 0 To (LineCount - 1)
Cells(3, 3 + i + 1).Value = i
'Looping and assigning Values to the Cell
'For i = LBound(strData) To UBound(strData)
tempParsing = Split(list(i), ":")
' MsgBox (tempParsing(0))
If tempParsing(0) > 0 And tempParsing(0) < 10 Then
Cells(dblRowNo + count + 2, dblColNo + i + 3).Interior.ColorIndex = 6
yellow = yellow + 1
ElseIf tempParsing(0) >= 10 Then
Cells(dblRowNo + count + 2, dblColNo + i + 3).Interior.ColorIndex = 3
red = red + 1
ElseIf tempParsing(0) = 0 Then
Cells(dblRowNo + count + 2, dblColNo + i + 3).Interior.ColorIndex = 0
End If
'Looping and assigning Values to the Cell
' For i = LBound(strData) To UBound(strData)
Cells(dblRowNo + count + 2, dblColNo + 1) = yellow
Cells(dblRowNo + count + 2, dblColNo) = red
Cells(dblRowNo + count + 2, dblColNo + i + 3).Value = list(i)
Next
Cells(3 + count + 1, 3).Value = count
count = count + 1
yellow = 0
red = 0
strCurrentTxtFile = Dir
Loop
For t = 4 To 175
If Cells(t, 1).Value > 0 Then
Cells(t, 1).Interior.ColorIndex = 3
End If
If Cells(t, 2).Value > 0 Then
Cells(t, 2).Interior.ColorIndex = 6
End If
Next
'Cells(9, 1) = "linecount = "
'Cells(9, 2) = LineCount
MsgBox "Done"
For f = 4 To 175
If Cells(f, 4).Interior.ColorIndex = 6 Then
YellowC = YellowC + 1
ElseIf Cells(f, 4).Interior.ColorIndex = 3 Then
RedC = RedC + 1
End If
Next
For g = 4 To 175
If Cells(g, 7).Interior.ColorIndex = 6 Then
YellowC = YellowC + 1
ElseIf Cells(g, 7).Interior.ColorIndex = 3 Then
RedC = RedC + 1
End If
Next
For u = 0 To (LineCount - 1)
Cells(dblRowNo, dblColNo + u + 3) = YellowC
Cells(1, dblColNo + u + 3) = RedC
Next
YellowC = 0
RedC = 0
Application.ScreenUpdating = True
End Sub
I've been trying to run the following code without luck. Nothing happens:
dim Dim OutputDataBordyRange As Range
Dim OutputCell As Range
For Each OutputCell In OutputDataBordyRange.Cells
If OutputCell Is Nothing Then
OutputCell.Value = 0
End If
Next OutputCell
Any suggestions?
Edit. I've tried below suggestions without luck. therefore, as requested, find below full code:
Sub Output_SBTB()
Dim InputDataRange As Range
Dim InputCountryRange As Range
Dim InputSiteRange As Range
Dim InputServiceLineRange As Range
Dim InputCalcHourlySalaryRateRange As Range
Dim InputRegionRange As Range
Dim OutputDataBodyRange As Range
Dim OutputHearderRowRange As Range
Dim OutputArrayColumns As Long
Dim OutputArrayRows As Long
Dim OutputArray() As Variant
Dim OutputArrayCounter As Long
Dim InputRowCounter As Long
Dim MatchRegion As Long
Dim InputCurrentSiteRowsCount As Long
Dim i As Long
Dim OutputCell As Range
Set InputDataRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").DataBodyRange
Set InputCountryRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").PivotFields("Country").DataRange
Set InputSiteRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").PivotFields("Site").DataRange
Set InputServiceLineRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").PivotFields("Serviceline").DataRange
Set InputCalcHourlySalaryRateRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").PivotFields("CalcHourlySalaryRate").DataRange
Set InputRegionRange = ThisWorkbook.Worksheets(Sheet4.Name).PivotTables("PivotTableRegion").PivotFields("Country").DataRange
Set OutputDataBodyRange = ThisWorkbook.Worksheets(Sheet2.Name).ListObjects("TableOutput").DataBodyRange
Set OutputHearderRowRange = ThisWorkbook.Worksheets(Sheet2.Name).ListObjects("TableOutput").HeaderRowRange
OutputArrayColumns = InputDataRange.Rows.Count
OutputArrayRows = OutputHearderRowRange.Columns.Count
ReDim Preserve OutputArray(OutputArrayRows, 1)
OutputArrayCounter = 0
If Not OutputDataBodyRange Is Nothing Then
OutputDataBodyRange.Delete
End If
For InputRowCounter = 1 To InputDataRange.Rows.Count
If InputSiteRange(InputRowCounter) <> InputSiteRange(InputRowCounter - 1) Then
OutputArrayCounter = OutputArrayCounter + 1
ReDim Preserve OutputArray(OutputArrayRows, OutputArrayCounter)
MatchRegion = Application.WorksheetFunction.Match(InputCountryRange(InputRowCounter), InputRegionRange, 0)
OutputArray(1, OutputArrayCounter) = InputRegionRange(MatchRegion).Offset(0, -1)
OutputArray(2, OutputArrayCounter) = InputCountryRange(InputRowCounter)
OutputArray(3, OutputArrayCounter) = InputSiteRange(InputRowCounter)
InputCurrentSiteRowsCount = Application.WorksheetFunction.CountIf(InputSiteRange, OutputArray(3, OutputArrayCounter)) - 1
For i = 0 To InputCurrentSiteRowsCount
' *** Landscaping & Irrigation System ***
If InputServiceLineRange(InputRowCounter + i) = "3.2.3-3.2.4 Landscaping & Irrigation System" Or InputServiceLineRange(InputRowCounter + i) = "Landscaping & Irrigation System - SBTB" Then
If InputCalcHourlySalaryRateRange(InputRowCounter + i) = "(blank)" Then
OutputArray(4, OutputArrayCounter) = OutputArray(4, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1)
OutputArray(5, OutputArrayCounter) = OutputArray(5, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2)
OutputArray(6, OutputArrayCounter) = OutputArray(6, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3)
Else
OutputArray(4, OutputArrayCounter) = OutputArray(4, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 4)
OutputArray(5, OutputArrayCounter) = OutputArray(4, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 5)
OutputArray(6, OutputArrayCounter) = OutputArray(4, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 6)
End If
End If
' *** Interior Plant and Tree Maintenance ***
If InputServiceLineRange(InputRowCounter + i) = "3.2.11 Interior Plant and Tree Maintenance" Or InputServiceLineRange(InputRowCounter + i) = "Interior Plant and Tree Maintenance - SBTB" Then
If InputCalcHourlySalaryRateRange(InputRowCounter + i) = "(blank)" Then
OutputArray(7, OutputArrayCounter) = OutputArray(7, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1)
OutputArray(8, OutputArrayCounter) = OutputArray(8, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2)
OutputArray(9, OutputArrayCounter) = OutputArray(9, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3)
Else
OutputArray(7, OutputArrayCounter) = OutputArray(7, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 4)
OutputArray(8, OutputArrayCounter) = OutputArray(8, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 5)
OutputArray(9, OutputArrayCounter) = OutputArray(9, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 6)
End If
End If
' *** Interior Pest Control ***
If InputServiceLineRange(InputRowCounter + i) = "3.3.10 Interior Pest Control" Or InputServiceLineRange(InputRowCounter + i) = "Pest Control - SBTB" Then
If InputCalcHourlySalaryRateRange(InputRowCounter + i) = "(blank)" Then
OutputArray(10, OutputArrayCounter) = OutputArray(10, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1)
OutputArray(11, OutputArrayCounter) = OutputArray(11, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2)
OutputArray(12, OutputArrayCounter) = OutputArray(12, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3)
Else
OutputArray(10, OutputArrayCounter) = OutputArray(10, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 4)
OutputArray(11, OutputArrayCounter) = OutputArray(11, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 5)
OutputArray(12, OutputArrayCounter) = OutputArray(12, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 6)
End If
End If
Next i
End If
Next InputRowCounter
ThisWorkbook.Worksheets(Sheet2.Name).Range("A3:L" & OutputArrayCounter) = Application.WorksheetFunction.Transpose(OutputArray)
For Each OutputCell In OutputDataBodyRange.Cells
If OutputCell.Value = vbNullString Then
OutputCell.Value = 0
End If
Next OutputCell
End Sub
If anything needs to be specified, please let me know.
If the cells are actually empty, you could skip the loop and just use:
On Error Resume Next
OutputDataBordyRange.SpecialCells(xlcelltypeblanks).Value2 = 0
On Error Goto 0
The Nothing keyword is not used to see if a cell is empty, it is used to see if a variable holds the default value for it's declared data type, or assign the default value to it (see this for more information).
Try the following:
For Each OutputCell In OutputDataBordyRange.Cells
If OutputCell.Value = vbNullString Then
OutputCell.Value = 0
End If
Next OutputCell