How to call a variable defined in a function? - excel

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

Related

Sorting responses into categories

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

how can i see the all possibilities using parameters

I have 3 workers.
I need to make assembly line balancing.
There are 10 operations of model.
You can see the time of operations for all workers in the chart below. They have different abilities.
So I need to share all operations between 3 workers.
so what I need:
Worker and operations of model is changeable.
20 worker-25 operations
18 worker-40 operations
19 worker-75 operations
...
So I need to define parameters for all i. Maybe need to use a function?
Sub rapor_calistir()
Range("q1") = Now()
Sheets("Rapor").Range("A2:Z1048576").ClearContents
a = 2: worker1 = 0: worker2 = 0: worker3 = 0
For i1 = 1 To 3
For i2 = 1 To 3
For i3 = 1 To 3
For i4 = 1 To 3
For i5 = 1 To 3
For i6 = 1 To 3
For i7 = 1 To 3
For i8 = 1 To 3
For i9 = 1 To 3
Sheets("Rapor").Cells(a, 1) = a - 1
Sheets("Rapor").Cells(a, 2) = i1
Sheets("Rapor").Cells(a, 3) = i2
Sheets("Rapor").Cells(a, 4) = i3
Sheets("Rapor").Cells(a, 5) = i4
Sheets("Rapor").Cells(a, 6) = i5
Sheets("Rapor").Cells(a, 7) = i6
Sheets("Rapor").Cells(a, 8) = i7
Sheets("Rapor").Cells(a, 9) = i8
Sheets("Rapor").Cells(a, 10) = i9
Sheets("Rapor").Cells(a, 11) = i10
For i = 1 To 10
ara_toplam = ara_toplam + WorksheetFunction.VLookup(i, Sheets("Data").Columns("A:D"), Sheets("Rapor").Cells(a, i + 1) + 1, False)
If Sheets("Rapor").Cells(a, i + 1) = 1 Then
worker1 = worker1 + WorksheetFunction.VLookup(i, Sheets("Data").Columns("A:D"), Sheets("Rapor").Cells(a, i + 1) + 1, False)
ElseIf Sheets("Rapor").Cells(a, i + 1) = 2 Then
worker2 = worker2 + WorksheetFunction.VLookup(i, Sheets("Data").Columns("A:D"), Sheets("Rapor").Cells(a, i + 1) + 1, False)
ElseIf Sheets("Rapor").Cells(a, i + 1) = 3 Then
worker3 = worker3 + WorksheetFunction.VLookup(i, Sheets("Data").Columns("A:D"), Sheets("Rapor").Cells(a, i + 1) + 1, False)
End If
Next i
Sheets("Rapor").Cells(a, 12) = ara_toplam
Sheets("Rapor").Cells(a, 13) = worker1
Sheets("Rapor").Cells(a, 14) = worker2
Sheets("Rapor").Cells(a, 15) = worker3
ara_toplam = 0: worker1 = 0: worker2 = 0: worker3 = 0
a = a + 1
Next i10
Next i9
Next i8
Next i7
Next i6
Next i5
Next i4
Next i3
Next i2
Next i1
End Sub
This sounds like a combination problem (order doesn't matter).
Option Explicit
Sub main()
Call for_each_in_others(rDATA:=Worksheets("Sheet1").Range("A2"), bHDR:=True)
End Sub
Sub for_each_in_others(rDATA As Range, Optional bHDR As Boolean = False)
Dim v As Long, w As Long
Dim iINCROWS As Long, iMAXROWS As Long, sErrorRng As String
Dim vVALs As Variant, vTMPs As Variant, vCOLs As Variant
On Error GoTo bm_Safe_Exit
appTGGL bTGGL:=False
With rDATA.Parent
With rDATA(1).CurrentRegion
'Debug.Print rDATA(1).Row - .Cells(1).Row
With .Resize(.Rows.Count - (rDATA(1).Row - .Cells(1).Row), .Columns.Count).Offset(2, 0)
sErrorRng = .Address(0, 0)
vTMPs = .Value2
ReDim vCOLs(LBound(vTMPs, 2) To UBound(vTMPs, 2))
iMAXROWS = 1
'On Error GoTo bm_Output_Exceeded
For w = LBound(vTMPs, 2) To UBound(vTMPs, 2)
vCOLs(w) = Application.CountA(.Columns(w))
iMAXROWS = iMAXROWS * vCOLs(w)
Next w
'control excessive or no rows of output
If iMAXROWS > Rows.Count Then
GoTo bm_Output_Exceeded
ElseIf .Columns.Count = 1 Or iMAXROWS = 0 Then
GoTo bm_Nothing_To_Do
End If
On Error GoTo bm_Safe_Exit
ReDim vVALs(LBound(vTMPs, 1) To iMAXROWS, LBound(vTMPs, 2) To UBound(vTMPs, 2))
iINCROWS = 1
For w = LBound(vVALs, 2) To UBound(vVALs, 2)
iINCROWS = iINCROWS * vCOLs(w)
For v = LBound(vVALs, 1) To UBound(vVALs, 1)
vVALs(v, w) = vTMPs((Int(iINCROWS * ((v - 1) / UBound(vVALs, 1))) Mod vCOLs(w)) + 1, w)
Next v
Next w
End With
End With
.Cells(2, UBound(vVALs, 2) + 2).Resize(1, UBound(vVALs, 2) + 2).EntireColumn.Delete
If bHDR Then
rDATA.Cells(1, 1).Offset(-1, 0).Resize(1, UBound(vVALs, 2)).Copy _
Destination:=rDATA.Cells(1, UBound(vVALs, 2) + 2).Offset(-1, 0)
End If
rDATA.Cells(1, UBound(vVALs, 2) + 2).Resize(UBound(vVALs, 1), UBound(vVALs, 2)) = vVALs
End With
GoTo bm_Safe_Exit
bm_Nothing_To_Do:
MsgBox "There is not enough data in " & sErrorRng & " to perform expansion." & Chr(10) & _
"This could be due to a single column of values or one or more blank column(s) of values." & _
Chr(10) & Chr(10) & "There is nothing to expand.", vbInformation, _
"Single or No Column of Raw Data"
GoTo bm_Safe_Exit
bm_Output_Exceeded:
MsgBox "The number of expanded values created from " & sErrorRng & _
" (" & Format(iMAXROWS, "\> #, ##0") & " rows × " & UBound(vTMPs, 2) & _
" columns) exceeds the rows available (" & Format(Rows.Count, "#, ##0") & ") on this worksheet.", vbCritical, _
"Too Many Entries"
bm_Safe_Exit:
appTGGL
End Sub
Sub appTGGL(Optional bTGGL As Boolean = True)
Application.EnableEvents = bTGGL
Application.ScreenUpdating = bTGGL
End Sub
Before:
After:
Expanding column cells for each column cell

Need to choose files manually instead of directing the vb code to a folderpath

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

Looping a macro that changes date format in column A to column C then continue until it's reached the last used cell in column A

Looping a macro that changes date format in column A to column C then continue until it's reached the last used cell in column A.
I can only get it to change the date in A1 - I need it to loop down until column A is blank:
Sub Macro1()
'
' Macro1 Macro
Dim ShipDate, Temp, DateForImport
Set ShipDate = Range("A1") ' or whatever cell the date is entered into (D30)
For I = 1 To 1
On Error Resume Next
Set Temp = Range("A1")
If Len(Temp.Text) > 1 Then
Set TempDate = Temp
'FORMAT SHIP DATE From MM/DD/YY OR MM/DD/YYYY TO YYYY-MM-DD
'if single digit month or day then fix it
If Len(TempDate.Text) = 10 Then
TempDate = TempDate
Exit For
End If
'case M/DD/YYYY length 9
If Len(TempDate.Text) = 9 And Mid(TempDate, 2, 1) = "/" Then
TempDate = "0" + Right(TempDate, 9)
Exit For
End If
'case MM/D/YYYY length 9
If Len(TempDate.Text) = 9 And Mid(TempDate, 3, 1) = "/" Then
TempDate = Left(TempDate, 3) + "0" + Right(TempDate, 6)
Exit For
End If
'case MM/DD/YY length 8
If Len(TempDate.Text) = 8 And Mid(TempDate, 3, 1) = "/" Then
TempDate = Left(TempDate, 6) + "20" + Right(TempDate, 2)
Exit For
End If
'case M/D/YYYY length 8
If Len(TempDate.Text) = 8 And Mid(TempDate, 2, 1) = "/" Then
TempDate = "0" + Left(TempDate, 2) + "0" + Right(TempDate, 6)
Exit For
End If
'case M/DD/YY length 7
If Len(TempDate.Text) = 7 And Mid(TempDate, 2, 1) = "/" Then
TempDate = "0" + Left(TempDate, 2) + Mid(TempDate, 3, 3) + "20" + Right(TempDate, 2)
Exit For
End If
'case MM/D/YY length 7
If Len(TempDate.Text) = 7 And Mid(TempDate, 3, 1) = "/" Then
TempDate = Left(TempDate, 3) + "0" + Mid(TempDate, 4, 2) + "20" + Right(TempDate, 2)
Exit For
End If
'case M/D/YY length 6
If Len(TempDate.Text) = 6 And Mid(TempDate, 2, 1) = "/" Then
TempDate = "0" + Left(TempDate, 2) + "0" + Mid(TempDate, 3, 2) + "20" + Right(TempDate, 2)
Exit For
End If
'MsgBox "found Ship Date: " + ShipDate
Exit For
End If
Next I
DateForImport = "20" + Right(TempDate, 2) + Left(TempDate, 2) + Mid(TempDate, 4, 2)
Range("C1") = DateForImport
End Sub
You will first need to define your last row as below and then set your temp range. Try substituting this into your macro and run through it.
lRow = WorksheetFunction.Max(Range("A65536").End(xlUp).Row)
With ActiveSheet
For i = lRow To 2 Step -1
On Error Resume Next
Set Temp = Range("A" & i)

Excel VBA: each empty cell in range = 0

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

Resources